Add a nasty hack to use re-exported crates. Makes me sad.

This commit is contained in:
Alec Thilenius 2023-05-02 14:52:42 -07:00
parent 2016c0dcc2
commit bc3c7380dd
5 changed files with 24 additions and 9 deletions

View file

@ -1,6 +1,6 @@
[package]
name = "axum-connect-build"
version = "0.1.3"
version = "0.1.4"
authors = ["Alec Thilenius <alec@thilenius.com>"]
edition = "2021"
categories = [

View file

@ -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");
}
}

View file

@ -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(())
}

View file

@ -1,6 +1,6 @@
[package]
name = "axum-connect"
version = "0.1.5"
version = "0.1.6"
authors = ["Alec Thilenius <alec@thilenius.com>"]
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"

View file

@ -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;