Skip to content

Commit

Permalink
Fix default conversion target not being set (rust) and feature flag p…
Browse files Browse the repository at this point in the history
…rotobuf
  • Loading branch information
kellerkindt committed Jan 16, 2024
1 parent fd9df9f commit 0762e7f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 56 deletions.
1 change: 1 addition & 0 deletions asn1rs-model/src/gen/mod.rs
@@ -1,3 +1,4 @@
#[cfg(feature = "protobuf")]
pub mod protobuf;
pub mod rust;

Expand Down
4 changes: 3 additions & 1 deletion src/cli.rs
Expand Up @@ -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")]
Expand All @@ -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,
}
60 changes: 9 additions & 51 deletions 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;
Expand All @@ -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<ProtobufGeneratorError> for Error {
fn from(g: ProtobufGeneratorError) -> Self {
#[cfg(feature = "protobuf")]
impl From<crate::gen::protobuf::Error> for Error {
fn from(g: crate::gen::protobuf::Error) -> Self {
Error::ProtobufGenerator(g)
}
}
Expand Down Expand Up @@ -90,16 +89,19 @@ impl Converter {
Ok(files)
}

#[cfg(feature = "protobuf")]
pub fn to_protobuf<D: AsRef<Path>>(
&self,
directory: D,
) -> Result<HashMap<String, Vec<String>>, Error> {
use crate::model::protobuf::ToProtobufModel;

let models = self.models.try_resolve_all()?;
let scope = models.iter().collect::<Vec<_>>();
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(
Expand All @@ -118,47 +120,3 @@ impl Converter {
Ok(files)
}
}

#[deprecated(note = "Use the Converter instead")]
pub fn convert_to_rust<F: AsRef<Path>, D: AsRef<Path>, A: Fn(&mut RustGenerator)>(
file: F,
dir: D,
custom_adjustments: A,
) -> Result<Vec<String>, 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<F: AsRef<Path>, D: AsRef<Path>>(
file: F,
dir: D,
) -> Result<Vec<String>, 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)
}
8 changes: 4 additions & 4 deletions src/main.rs
Expand Up @@ -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 = <cli::Parameters as clap::Parser>::parse();
Expand All @@ -40,11 +39,12 @@ pub fn main() {
}

let result = match params.conversion_target {
ConversionTarget::Rust => converter.to_rust(&params.destination_dir, |rust| {
ConversionTarget::Rust => converter.to_rust(&params.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(&params.destination_dir),
#[cfg(feature = "protobuf")]
ConversionTarget::Proto => converter.to_protobuf(&params.destination_dir),
};

match result {
Expand Down

0 comments on commit 0762e7f

Please sign in to comment.