Skip to content

Commit

Permalink
use-anyhow (#121)
Browse files Browse the repository at this point in the history
Signed-off-by: n4n5 <its.just.n4n5@gmail.com>
  • Loading branch information
Its-Just-Nans authored Jun 6, 2024
1 parent e536648 commit dfc57b8
Show file tree
Hide file tree
Showing 52 changed files with 480 additions and 583 deletions.
1 change: 1 addition & 0 deletions asn-compiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ docx-rs = { version = "0.4.5" , optional = true }
regex = { version = "1.6.0" }
log = { version = "0.4" }
env_logger = { version = "0.10" }
anyhow = "1.0.86"

[features]
rs-specs-gen = [ "docx-rs"]
Expand Down
3 changes: 2 additions & 1 deletion asn-compiler/src/bin/extract-asn-spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use std::io::{Read, Write};

use anyhow::Result;
use clap::Parser;

#[derive(Parser, Debug)]
Expand All @@ -14,7 +15,7 @@ struct Cli {
output_file: String,
}

fn main() -> std::io::Result<()> {
fn main() -> Result<()> {
let cli = Cli::parse();

let asn_start = regex::Regex::new("^-- ASN1START$").unwrap();
Expand Down
8 changes: 4 additions & 4 deletions asn-compiler/src/bin/hampi-rs-asn1c.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! A simple utility to tokenize ASN files.
use std::io;

use anyhow::Result;
use clap::Parser;

use asn1_compiler::{
Expand Down Expand Up @@ -36,14 +35,15 @@ struct Cli {
derive: Vec<Derive>,
}

fn main() -> io::Result<()> {
fn main() -> Result<()> {
let mut cli = Cli::parse();

if cli.files.is_empty() {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"No Input files Specified",
));
)
.into());
}

let derives = if cli.derive.contains(&Derive::All) {
Expand Down
25 changes: 12 additions & 13 deletions asn-compiler/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::process::{Command, Stdio};
use topological_sort::TopologicalSort;

use crate::error::Error;
use anyhow::Result;

use crate::parser::asn::structs::module::Asn1Module;

Expand Down Expand Up @@ -79,7 +80,7 @@ impl Asn1Compiler {
/// modules are topologically sorted as well. This makes Error handling for undefined
/// definitions much easier.
// FIXME: Support the case where module is imported by a name different from it's actual name.
pub fn resolve_modules(&mut self) -> Result<(), Error> {
pub fn resolve_modules(&mut self) -> Result<()> {
log::info!("Resolving imports from all modules.");
self.resolve_imports()?;

Expand All @@ -88,7 +89,7 @@ impl Asn1Compiler {
}

/// Generate the code
pub fn generate(&mut self) -> Result<(), Error> {
pub fn generate(&mut self) -> Result<()> {
log::info!("Generating code, writing to file: {}", self.output_filename);

let input_text = self.generator.generate(&self.resolver)?;
Expand All @@ -109,7 +110,7 @@ impl Asn1Compiler {
}

/// Compilation Driver for a String as module(s).
pub fn compile_string(&mut self, modules_string: &str, parse_only: bool) -> Result<(), Error> {
pub fn compile_string(&mut self, modules_string: &str, parse_only: bool) -> Result<()> {
let mut tokens = crate::tokenizer::tokenize_string(modules_string)?;
self.parse_tokens_into_modules(&mut tokens)?;
if !parse_only {
Expand All @@ -121,10 +122,7 @@ impl Asn1Compiler {
}

/// The Actual compilation driver
pub fn compile_files<T: AsRef<Path> + std::fmt::Debug>(
&mut self,
files: &[T],
) -> Result<(), Error> {
pub fn compile_files<T: AsRef<Path> + std::fmt::Debug>(&mut self, files: &[T]) -> Result<()> {
for file in files {
log::info!("Processing file: {:?}", file);
let file = File::open(file).map_err(|e| io_error!("{:#?}", e))?;
Expand All @@ -136,7 +134,7 @@ impl Asn1Compiler {
self.generate()
}

fn parse_tokens_into_modules(&mut self, tokens: &mut Vec<Token>) -> Result<(), Error> {
fn parse_tokens_into_modules(&mut self, tokens: &mut Vec<Token>) -> Result<()> {
log::debug!("Parsing {} tokens.", tokens.len());
let mut modules = crate::parser::parse(tokens)?;
loop {
Expand All @@ -151,7 +149,7 @@ impl Asn1Compiler {
Ok(())
}

fn rustfmt_generated_code(&self, code: &str) -> Result<String, Error> {
fn rustfmt_generated_code(&self, code: &str) -> Result<String> {
log::debug!("Runing `rustfmt` on the generated code.");
let rustfmt_binary = "rustfmt"; // TODO: Get from `env` , 'custom path' etc.
let mut cmd = Command::new(rustfmt_binary);
Expand All @@ -177,13 +175,13 @@ impl Asn1Compiler {
match String::from_utf8(output) {
Ok(formatted_output) => match status.code() {
Some(0) => Ok(formatted_output),
_ => Err(resolve_error!("`rustfmt` failed to write some bindings.")),
_ => Err(resolve_error!("`rustfmt` failed to write some bindings.").into()),
},
_ => Ok(stdin_handle.join().unwrap()),
}
}

fn resolve_imports(&self) -> Result<(), Error> {
fn resolve_imports(&self) -> Result<()> {
log::debug!("Resolving imports.");
for (_, module) in self.modules.iter() {
for (import, module_name) in module.get_imported_defs() {
Expand All @@ -193,7 +191,8 @@ impl Asn1Compiler {
"Module '{}', corresponding to definition '{}' not found!",
module_name.name_as_str(),
import
));
)
.into());
}
}
}
Expand Down Expand Up @@ -226,7 +225,7 @@ impl Asn1Compiler {
out_vec
}

fn resolve_definitions(&mut self) -> Result<(), Error> {
fn resolve_definitions(&mut self) -> Result<()> {
let module_names = self.sorted_modules();
for name in module_names {
let module = self.modules.get_mut(&name).unwrap();
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/bitstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use anyhow::Result;

use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedBitString;

impl Asn1ResolvedBitString {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let struct_name = generator.to_type_ident(name);

let mut ty_attributes = quote! { type = "BITSTRING" };
Expand All @@ -39,7 +35,7 @@ impl Asn1ResolvedBitString {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedBoolean;
use anyhow::Result;

impl Asn1ResolvedBoolean {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let type_name = generator.to_type_ident(name);

let vis = generator.get_visibility_tokens();
Expand All @@ -29,7 +25,7 @@ impl Asn1ResolvedBoolean {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/charstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use anyhow::Result;

use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedCharacterString;

impl Asn1ResolvedCharacterString {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let struct_name = generator.to_type_ident(name);
let char_str_type: proc_macro2::TokenStream =
format!("\"{}\"", self.str_type).parse().unwrap();
Expand Down Expand Up @@ -41,7 +37,7 @@ impl Asn1ResolvedCharacterString {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
12 changes: 4 additions & 8 deletions asn-compiler/src/generator/asn/types/base/enumerated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use anyhow::Result;

use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedEnumerated;

impl Asn1ResolvedEnumerated {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let struct_name = generator.to_type_ident(name);
let inner_type = generator.to_inner_type(self.bits, self.signed);

Expand Down Expand Up @@ -43,7 +39,7 @@ impl Asn1ResolvedEnumerated {
Ok(struct_tokens)
}

fn generate_named_values(&self, generator: &Generator) -> Result<TokenStream, Error> {
fn generate_named_values(&self, generator: &Generator) -> Result<TokenStream> {
let mut tokens = TokenStream::new();
for (name, value) in &self.named_root_values {
let const_name = generator.to_const_ident(name);
Expand All @@ -65,7 +61,7 @@ impl Asn1ResolvedEnumerated {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/integer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use anyhow::Result;

use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedInteger;

impl Asn1ResolvedInteger {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let struct_name = generator.to_type_ident(name);
let inner_type = generator.to_inner_type(self.bits, self.signed);
let (min, max) = self.get_min_max_constraints();
Expand Down Expand Up @@ -68,7 +64,7 @@ impl Asn1ResolvedInteger {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
6 changes: 3 additions & 3 deletions asn-compiler/src/generator/asn/types/base/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ mod real;

use proc_macro2::{Ident, TokenStream};

use crate::error::Error;
use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::ResolvedBaseType;
use anyhow::Result;

impl ResolvedBaseType {
pub(crate) fn generate_for_base_type(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
) -> Result<TokenStream> {
match self {
ResolvedBaseType::Integer(ref i) => i.generate(name, generator),
ResolvedBaseType::Enum(ref e) => e.generate(name, generator),
Expand All @@ -47,7 +47,7 @@ impl ResolvedBaseType {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
match self {
ResolvedBaseType::Integer(ref i) => i.generate_ident_and_aux_type(generator, input),
ResolvedBaseType::Enum(ref e) => e.generate_ident_and_aux_type(generator, input),
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/null.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedNull;
use anyhow::Result;

impl Asn1ResolvedNull {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let type_name = generator.to_type_ident(name);

let vis = generator.get_visibility_tokens();
Expand All @@ -29,7 +25,7 @@ impl Asn1ResolvedNull {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
10 changes: 3 additions & 7 deletions asn-compiler/src/generator/asn/types/base/octetstring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
use proc_macro2::{Ident, TokenStream};
use quote::quote;

use crate::error::Error;
use anyhow::Result;

use crate::generator::Generator;
use crate::resolver::asn::structs::types::base::Asn1ResolvedOctetString;

impl Asn1ResolvedOctetString {
pub(crate) fn generate(
&self,
name: &str,
generator: &mut Generator,
) -> Result<TokenStream, Error> {
pub(crate) fn generate(&self, name: &str, generator: &mut Generator) -> Result<TokenStream> {
let struct_name = generator.to_type_ident(name);

let mut ty_attributes = quote! { type = "OCTET-STRING" };
Expand All @@ -39,7 +35,7 @@ impl Asn1ResolvedOctetString {
&self,
generator: &mut Generator,
input: Option<&String>,
) -> Result<Ident, Error> {
) -> Result<Ident> {
let unique_name = if let Some(unique_name) = input {
unique_name.to_string()
} else {
Expand Down
Loading

0 comments on commit dfc57b8

Please sign in to comment.