diff --git a/api/rust/Cargo.toml b/api/rust/Cargo.toml index 7d2c26f7..6e641395 100644 --- a/api/rust/Cargo.toml +++ b/api/rust/Cargo.toml @@ -9,21 +9,22 @@ repository = "https://github.com/chirpstack/chirpstack" edition = "2021" [features] -default = ["api"] +default = ["api", "json"] api = ["tonic/transport", "tonic-build/transport", "tokio"] +json = ["pbjson", "pbjson-types", "serde"] internal = [] [dependencies] prost = "0.11" prost-types = "0.11" -pbjson = "0.5" -pbjson-types = "0.5" hex = "0.4" rand = "0.8" -tonic = { version = "0.8", features = ["codegen", "prost"], default-features = false } +tonic = { version = "0.8", features = ["codegen", "prost"], default-features = false, optional = true } tokio = { version = "1.25", features = ["macros"], optional = true } -serde = { version = "1.0" } +pbjson = { version = "0.5", optional = true } +pbjson-types = { version = "0.5", optional = true } +serde = { version = "1.0", optional = true } [build-dependencies] tonic-build = { version = "0.8", features = ["prost"], default-features = false } diff --git a/api/rust/build.rs b/api/rust/build.rs index 594175d9..8e4d0a89 100644 --- a/api/rust/build.rs +++ b/api/rust/build.rs @@ -16,12 +16,18 @@ fn main() -> Result<(), Box> { std::fs::create_dir_all(out_dir.join("meta")).unwrap(); std::fs::create_dir_all(out_dir.join("api")).unwrap(); + #[cfg(feature = "json")] + let well_known_types_path = "::pbjson_types"; + + #[cfg(not(feature = "json"))] + let well_known_types_path = "::prost_types"; + // common tonic_build::configure() .out_dir(out_dir.join("common")) .file_descriptor_set_path(out_dir.join("common").join("proto_descriptor.bin")) .compile_well_known_types(true) - .extern_path(".google.protobuf", "::pbjson_types") + .extern_path(".google.protobuf", well_known_types_path) .compile( &[cs_dir.join("common").join("common.proto").to_str().unwrap()], &[ @@ -30,18 +36,21 @@ fn main() -> Result<(), Box> { ], )?; - let descriptor_set = std::fs::read(out_dir.join("common").join("proto_descriptor.bin"))?; - pbjson_build::Builder::new() - .register_descriptors(&descriptor_set)? - .out_dir(out_dir.join("common")) - .build(&[".common"])?; + #[cfg(feature = "json")] + { + let descriptor_set = std::fs::read(out_dir.join("common").join("proto_descriptor.bin"))?; + pbjson_build::Builder::new() + .register_descriptors(&descriptor_set)? + .out_dir(out_dir.join("common")) + .build(&[".common"])?; + } // gw tonic_build::configure() .out_dir(out_dir.join("gw")) .file_descriptor_set_path(out_dir.join("gw").join("proto_descriptor.bin")) .compile_well_known_types(true) - .extern_path(".google.protobuf", "::pbjson_types") + .extern_path(".google.protobuf", well_known_types_path) .extern_path(".common", "crate::common") .compile( &[cs_dir.join("gw").join("gw.proto").to_str().unwrap()], @@ -51,19 +60,22 @@ fn main() -> Result<(), Box> { ], )?; - let descriptor_set = std::fs::read(out_dir.join("gw").join("proto_descriptor.bin"))?; - pbjson_build::Builder::new() - .register_descriptors(&descriptor_set)? - .out_dir(out_dir.join("gw")) - .extern_path(".common", "crate::common") - .build(&[".gw"])?; + #[cfg(feature = "json")] + { + let descriptor_set = std::fs::read(out_dir.join("gw").join("proto_descriptor.bin"))?; + pbjson_build::Builder::new() + .register_descriptors(&descriptor_set)? + .out_dir(out_dir.join("gw")) + .extern_path(".common", "crate::common") + .build(&[".gw"])?; + } // internal tonic_build::configure() .out_dir(out_dir.join("internal")) .file_descriptor_set_path(out_dir.join("internal").join("proto_descriptor.bin")) .compile_well_known_types(true) - .extern_path(".google.protobuf", "::pbjson_types") + .extern_path(".google.protobuf", well_known_types_path) .extern_path(".common", "crate::common") .compile( &[cs_dir @@ -77,19 +89,22 @@ fn main() -> Result<(), Box> { ], )?; - let descriptor_set = std::fs::read(out_dir.join("internal").join("proto_descriptor.bin"))?; - pbjson_build::Builder::new() - .register_descriptors(&descriptor_set)? - .out_dir(out_dir.join("internal")) - .extern_path(".common", "crate::common") - .build(&[".internal"])?; + #[cfg(feature = "json")] + { + let descriptor_set = std::fs::read(out_dir.join("internal").join("proto_descriptor.bin"))?; + pbjson_build::Builder::new() + .register_descriptors(&descriptor_set)? + .out_dir(out_dir.join("internal")) + .extern_path(".common", "crate::common") + .build(&[".internal"])?; + } // integration tonic_build::configure() .out_dir(out_dir.join("integration")) .file_descriptor_set_path(out_dir.join("integration").join("proto_descriptor.bin")) .compile_well_known_types(true) - .extern_path(".google.protobuf", "::pbjson_types") + .extern_path(".google.protobuf", well_known_types_path) .extern_path(".common", "crate::common") .extern_path(".gw", "crate::gw") .compile( @@ -104,14 +119,18 @@ fn main() -> Result<(), Box> { ], )?; - let descriptor_set = std::fs::read(out_dir.join("integration").join("proto_descriptor.bin"))?; - pbjson_build::Builder::new() - .emit_fields() - .register_descriptors(&descriptor_set)? - .out_dir(out_dir.join("integration")) - .extern_path(".common", "crate::common") - .extern_path(".gw", "crate::gw") - .build(&[".integration"])?; + #[cfg(feature = "json")] + { + let descriptor_set = + std::fs::read(out_dir.join("integration").join("proto_descriptor.bin"))?; + pbjson_build::Builder::new() + .emit_fields() + .register_descriptors(&descriptor_set)? + .out_dir(out_dir.join("integration")) + .extern_path(".common", "crate::common") + .extern_path(".gw", "crate::gw") + .build(&[".integration"])?; + } // meta tonic_build::configure() @@ -125,13 +144,16 @@ fn main() -> Result<(), Box> { &[proto_dir.join("chirpstack").to_str().unwrap()], )?; - let descriptor_set = std::fs::read(out_dir.join("meta").join("proto_descriptor.bin"))?; - pbjson_build::Builder::new() - .register_descriptors(&descriptor_set)? - .out_dir(out_dir.join("meta")) - .extern_path(".common", "crate::common") - .extern_path(".gw", "crate::gw") - .build(&[".meta"])?; + #[cfg(feature = "json")] + { + let descriptor_set = std::fs::read(out_dir.join("meta").join("proto_descriptor.bin"))?; + pbjson_build::Builder::new() + .register_descriptors(&descriptor_set)? + .out_dir(out_dir.join("meta")) + .extern_path(".common", "crate::common") + .extern_path(".gw", "crate::gw") + .build(&[".meta"])?; + } // api tonic_build::configure() diff --git a/api/rust/src/common.rs b/api/rust/src/common.rs index 0846ca02..4a4a3014 100644 --- a/api/rust/src/common.rs +++ b/api/rust/src/common.rs @@ -2,7 +2,8 @@ use std::error::Error; use std::fmt; use std::str::FromStr; -tonic::include_proto!("common/common"); +include!(concat!(env!("OUT_DIR"), "/common/common.rs")); +#[cfg(feature = "json")] include!(concat!(env!("OUT_DIR"), "/common/common.serde.rs")); #[allow(clippy::from_over_into)] diff --git a/api/rust/src/gw.rs b/api/rust/src/gw.rs index 5061a72b..1e137e9e 100644 --- a/api/rust/src/gw.rs +++ b/api/rust/src/gw.rs @@ -2,7 +2,8 @@ use rand::Rng; use std::error::Error; use std::str::FromStr; -tonic::include_proto!("gw/gw"); +include!(concat!(env!("OUT_DIR"), "/gw/gw.rs")); +#[cfg(feature = "json")] include!(concat!(env!("OUT_DIR"), "/gw/gw.serde.rs")); #[allow(clippy::from_over_into)] diff --git a/api/rust/src/integration.rs b/api/rust/src/integration.rs index 928099c2..a0e46207 100644 --- a/api/rust/src/integration.rs +++ b/api/rust/src/integration.rs @@ -1,4 +1,5 @@ -tonic::include_proto!("integration/integration"); +include!(concat!(env!("OUT_DIR"), "/integration/integration.rs")); +#[cfg(feature = "json")] include!(concat!( env!("OUT_DIR"), "/integration/integration.serde.rs" diff --git a/api/rust/src/internal.rs b/api/rust/src/internal.rs index 12083a0d..f643e7e1 100644 --- a/api/rust/src/internal.rs +++ b/api/rust/src/internal.rs @@ -1,2 +1,3 @@ -tonic::include_proto!("internal/internal"); +include!(concat!(env!("OUT_DIR"), "/internal/internal.rs")); +#[cfg(feature = "json")] include!(concat!(env!("OUT_DIR"), "/internal/internal.serde.rs")); diff --git a/api/rust/src/meta.rs b/api/rust/src/meta.rs index 72194651..6b5d390b 100644 --- a/api/rust/src/meta.rs +++ b/api/rust/src/meta.rs @@ -1,2 +1,3 @@ -tonic::include_proto!("meta/meta"); +include!(concat!(env!("OUT_DIR"), "/meta/meta.rs")); +#[cfg(feature = "json")] include!(concat!(env!("OUT_DIR"), "/meta/meta.serde.rs"));