diff --git a/axum-connect-build/Cargo.toml b/axum-connect-build/Cargo.toml index a43f920..b090599 100644 --- a/axum-connect-build/Cargo.toml +++ b/axum-connect-build/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum-connect-build" -version = "0.1.3" +version = "0.1.4" authors = ["Alec Thilenius "] edition = "2021" categories = [ diff --git a/axum-connect-build/src/gen.rs b/axum-connect-build/src/gen.rs index a878e41..4fcb4f0 100644 --- a/axum-connect-build/src/gen.rs +++ b/axum-connect-build/src/gen.rs @@ -69,9 +69,4 @@ impl ServiceGenerator for AxumConnectServiceGenerator { fn generate(&mut self, service: Service, buf: &mut String) { self.generate_service(service, buf); } - - fn finalize(&mut self, buf: &mut String) { - // Add serde import (because that's less effort than hacking pbjson). - buf.push_str("\nuse axum_connect::serde;\n"); - } } diff --git a/axum-connect-build/src/lib.rs b/axum-connect-build/src/lib.rs index 64eb813..cf84b5f 100644 --- a/axum-connect-build/src/lib.rs +++ b/axum-connect-build/src/lib.rs @@ -1,7 +1,10 @@ use std::{ + cell::RefCell, env, io::{BufWriter, Write}, + ops::Deref, path::{Path, PathBuf}, + rc::Rc, }; use gen::AxumConnectServiceGenerator; @@ -74,7 +77,7 @@ pub fn axum_connect_codegen(settings: AxumConnectGenSettings) -> anyhow::Result< // Standard prost configuration conf.compile_well_known_types(); conf.file_descriptor_set_path(&descriptor_path); - conf.extern_path(".google.protobuf", "::pbjson_types"); + conf.extern_path(".google.protobuf", "::axum_connect::pbjson_types"); conf.service_generator(Box::new(AxumConnectServiceGenerator::new())); // Arg configuration @@ -91,10 +94,15 @@ pub fn axum_connect_codegen(settings: AxumConnectGenSettings) -> anyhow::Result< let mut output: PathBuf = PathBuf::from(env::var("OUT_DIR").unwrap()); output.push("FILENAME"); + // TODO: This is a nasty hack. Get rid of it. Idk how without dumping Prost and pbjson though. + let files = Rc::new(RefCell::new(vec![])); + + let files_c = files.clone(); let writers = pbjson_build::Builder::new() .register_descriptors(&descriptor_set)? .generate(&["."], move |package| { output.set_file_name(format!("{}.rs", package)); + files_c.deref().borrow_mut().push(output.clone()); let file = std::fs::OpenOptions::new().append(true).open(&output)?; @@ -105,5 +113,14 @@ pub fn axum_connect_codegen(settings: AxumConnectGenSettings) -> anyhow::Result< writer.flush()?; } + // Now second part of the nasty hack, replace a few namespaces with re-exported ones. + for file in files.take().into_iter() { + let contents = std::fs::read_to_string(&file)?; + let contents = contents.replace("pbjson::", "axum_connect::pbjson::"); + let contents = contents.replace("prost::", "axum_connect::prost::"); + let contents = contents.replace("serde::", "axum_connect::serde::"); + std::fs::write(&file, contents)?; + } + Ok(()) } diff --git a/axum-connect/Cargo.toml b/axum-connect/Cargo.toml index 5085a97..6d0cd58 100644 --- a/axum-connect/Cargo.toml +++ b/axum-connect/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "axum-connect" -version = "0.1.5" +version = "0.1.6" authors = ["Alec Thilenius "] edition = "2021" categories = [ @@ -19,6 +19,7 @@ async-trait = "0.1.64" axum = "0.6.9" futures = "0.3.26" pbjson = "0.5.1" +pbjson-types = "0.5.1" prost = "0.11.9" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" diff --git a/axum-connect/src/lib.rs b/axum-connect/src/lib.rs index 0b5c9ce..bab640c 100644 --- a/axum-connect/src/lib.rs +++ b/axum-connect/src/lib.rs @@ -4,7 +4,9 @@ pub mod parts; pub mod response; pub mod router; -// Re-export both prost and serde. +// Re-export several crates +pub use pbjson; +pub use pbjson_types; pub use prost; pub use serde;