From 0762e7f0be675f8c685ddae4526b02bd37ba0569 Mon Sep 17 00:00:00 2001 From: Michael Watzko Date: Tue, 16 Jan 2024 22:22:41 +0100 Subject: [PATCH] Fix default conversion target not being set (rust) and feature flag protobuf --- asn1rs-model/src/gen/mod.rs | 1 + src/cli.rs | 4 ++- src/converter.rs | 60 ++++++------------------------------- src/main.rs | 8 ++--- 4 files changed, 17 insertions(+), 56 deletions(-) diff --git a/asn1rs-model/src/gen/mod.rs b/asn1rs-model/src/gen/mod.rs index 9d95c7b..7d7478c 100644 --- a/asn1rs-model/src/gen/mod.rs +++ b/asn1rs-model/src/gen/mod.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "protobuf")] pub mod protobuf; pub mod rust; diff --git a/src/cli.rs b/src/cli.rs index 12518fd..b755d41 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -20,7 +20,8 @@ pub struct Parameters { short = 't', long = "convert-to", env = "CONVERT_TO", - help = "The target to convert the input files to" + help = "The target to convert the input files to", + default_value = "rust" )] pub conversion_target: ConversionTarget, #[arg(env = "DESTINATION_DIR")] @@ -32,5 +33,6 @@ pub struct Parameters { #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, clap::ValueEnum)] pub enum ConversionTarget { Rust, + #[cfg(feature = "protobuf")] Proto, } diff --git a/src/converter.rs b/src/converter.rs index 52b63c6..a5caab1 100644 --- a/src/converter.rs +++ b/src/converter.rs @@ -1,9 +1,6 @@ -use crate::gen::protobuf::Error as ProtobufGeneratorError; -use crate::gen::protobuf::ProtobufDefGenerator as ProtobufGenerator; use crate::gen::rust::RustCodeGenerator as RustGenerator; use crate::gen::Generator; use crate::model::lor::Error as ResolveError; -use crate::model::protobuf::ToProtobufModel; use crate::model::Model; use crate::model::{Error as ModelError, MultiModuleResolver}; use crate::parser::Tokenizer; @@ -14,14 +11,16 @@ use std::path::Path; #[derive(Debug)] pub enum Error { RustGenerator, - ProtobufGenerator(ProtobufGeneratorError), + #[cfg(feature = "protobuf")] + ProtobufGenerator(crate::gen::protobuf::Error), Model(ModelError), Io(IoError), ResolveError(ResolveError), } -impl From for Error { - fn from(g: ProtobufGeneratorError) -> Self { +#[cfg(feature = "protobuf")] +impl From for Error { + fn from(g: crate::gen::protobuf::Error) -> Self { Error::ProtobufGenerator(g) } } @@ -90,16 +89,19 @@ impl Converter { Ok(files) } + #[cfg(feature = "protobuf")] pub fn to_protobuf>( &self, directory: D, ) -> Result>, Error> { + use crate::model::protobuf::ToProtobufModel; + let models = self.models.try_resolve_all()?; let scope = models.iter().collect::>(); let mut files = HashMap::with_capacity(models.len()); for model in &models { - let mut generator = ProtobufGenerator::default(); + let mut generator = crate::gen::protobuf::ProtobufDefGenerator::default(); generator.add_model(model.to_rust_with_scope(&scope[..]).to_protobuf()); files.insert( @@ -118,47 +120,3 @@ impl Converter { Ok(files) } } - -#[deprecated(note = "Use the Converter instead")] -pub fn convert_to_rust, D: AsRef, A: Fn(&mut RustGenerator)>( - file: F, - dir: D, - custom_adjustments: A, -) -> Result, Error> { - let input = ::std::fs::read_to_string(file)?; - let tokens = Tokenizer.parse(&input); - let model = Model::try_from(tokens)?.try_resolve()?; - let mut generator = RustGenerator::default(); - generator.add_model(model.to_rust()); - - custom_adjustments(&mut generator); - - let output = generator.to_string().map_err(|_| Error::RustGenerator)?; - - let mut files = Vec::new(); - for (file, content) in output { - ::std::fs::write(dir.as_ref().join(&file), content)?; - files.push(file); - } - Ok(files) -} - -#[deprecated(note = "Use the Converter instead")] -pub fn convert_to_proto, D: AsRef>( - file: F, - dir: D, -) -> Result, Error> { - let input = ::std::fs::read_to_string(file)?; - let tokens = Tokenizer.parse(&input); - let model = Model::try_from(tokens)?.try_resolve()?; - let mut generator = ProtobufGenerator::default(); - generator.add_model(model.to_rust().to_protobuf()); - let output = generator.to_string()?; - - let mut files = Vec::new(); - for (file, content) in output { - ::std::fs::write(dir.as_ref().join(&file), content)?; - files.push(file); - } - Ok(files) -} diff --git a/src/main.rs b/src/main.rs index 8c77eb3..792f4f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,12 +21,11 @@ pub mod syn; pub mod cli; pub mod converter; +use crate::cli::ConversionTarget; use asn1rs::converter::Converter; pub use asn1rs_model::gen; pub use asn1rs_model::model; pub use asn1rs_model::parser; -use crate::cli::ConversionTarget; - pub fn main() { let params = ::parse(); @@ -40,11 +39,12 @@ pub fn main() { } let result = match params.conversion_target { - ConversionTarget::Rust => converter.to_rust(¶ms.destination_dir, |rust| { + ConversionTarget::Rust => converter.to_rust(¶ms.destination_dir, |rust| { rust.set_fields_pub(!params.rust_fields_not_public); rust.set_fields_have_getter_and_setter(params.rust_getter_and_setter); }), - ConversionTarget::Proto => converter.to_protobuf(¶ms.destination_dir), + #[cfg(feature = "protobuf")] + ConversionTarget::Proto => converter.to_protobuf(¶ms.destination_dir), }; match result {