Crate rasn_compiler

source ·
Expand description

The rasn-compiler library is a parser combinator that parses ASN1 specifications and outputs encoding-rule-agnotic bindings for the ASN.1 data elements to be used with the rasn crate. The compiler heavily relies on the great library nom for its basic parsers.

§Example

In order to compile ASN.1 in your build process, invoke the rasn compiler in your build.rs build script.

// build.rs build script
use std::path::PathBuf;
use rasn_compiler::prelude::*;

fn main() {
  struct CustomBackend;

  impl Backend for CustomBackend {
    fn generate_module(
         &self,
         top_level_declarations: Vec<ToplevelDefinition>,
    ) -> Result<GeneratedModule, GeneratorError> {
        Ok(GeneratedModule::empty())
    }
  }

  // Initialize the compiler
  match Compiler::new()
    // optionally provide a custom backend
    .with_backend(CustomBackend)
    // add a single ASN1 source file
    .add_asn_by_path(PathBuf::from("spec_1.asn"))
    // add several ASN1 source files
    .add_asn_sources_by_path(vec![
        PathBuf::from("spec_2.asn"),
        PathBuf::from("spec_3.asn"),
    ].iter())
    // set an output path for the generated rust code
    .set_output_path(PathBuf::from("./asn/generated.rs"))
    // you may also compile literal ASN1 snippets
    .add_asn_literal("My-test-integer ::= INTEGER (1..128)")
    .compile() {
    Ok(warnings /* Vec<Box<dyn Error>> */) => { /* handle compilation warnings */ }
    Err(error /* Box<dyn Error> */) => { /* handle unrecoverable compilation error */ }
  }
}

Modules§

  • The intermediate module provides an intermediate representation for ASN.1 notation. It includes constants for the various ASN.1 keywords and types to represent the single ASN.1 data elements in an intermediate representation from which the generator module produces bindings. The intermediate representation aims to preserve as much information as possible from the original specification, even though some of that information might not actually be relevant for decoding and encoding in any of the common encoding rules (inner type constraints are such an example).
  • Convenience module that collects all necessary imports for using and customizing the compiler.

Structs§

Traits§