Skip to content

Commit

Permalink
Merge pull request #17 from librasn/feat/generator-config
Browse files Browse the repository at this point in the history
Feat/generator config
  • Loading branch information
6d7a authored Apr 23, 2024
2 parents 169e9d3 + d8f6521 commit 03c5cd6
Show file tree
Hide file tree
Showing 11 changed files with 2,393 additions and 2,054 deletions.
2 changes: 1 addition & 1 deletion rasn-compiler-tests/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ macro_rules! e2e_pdu {
fn $suite() {
rasn_compiler_derive::asn1!($asn1);
assert_eq!(
rasn_compiler::Compiler::new()
rasn_compiler::Compiler::<rasn_compiler::prelude::RasnBackend, _>::new()
.add_asn_literal(&format!("TestModule DEFINITIONS AUTOMATIC TAGS::= BEGIN {} END", $asn1))
.compile_to_string()
.unwrap()
Expand Down
28 changes: 0 additions & 28 deletions rasn-compiler-tests/tests/edge_cases.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,6 @@
#![allow(non_camel_case_types)]
use rasn_compiler_tests::e2e_pdu;

#[test]
fn t() {
println!(
"{:?}",
rasn_compiler::Compiler::new()
.add_asn_literal(
r#"TestModule DEFINITIONS AUTOMATIC TAGS::= BEGIN
Restricted ::= Distinguished (second|fourth..sixth|eighth)
Distinguished ::= INTEGER {
first(1),
second(2),
third(3),
fourth(4),
fifth(5),
sixth(6),
seventh(7),
eighth(8),
ninth(9),
tenth(10),
} (1..10)
END"#
)
.compile_to_string()
.unwrap()
);
}

e2e_pdu!(
distinguished_value_range,
r#" Restricted ::= Distinguished (second|fourth..sixth|eighth)
Expand Down
27 changes: 15 additions & 12 deletions rasn-compiler-tests/tests/parse_test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use rasn_compiler::Compiler;
use rasn_compiler::{prelude::RasnBackend, Compiler};

#[test]
#[ignore]
Expand All @@ -10,7 +10,7 @@ fn parses_modules() {
for entry in read_dir.flatten() {
let path = entry.path();
println!("{:?}", &path);
if let Err(e) = Compiler::new()
if let Err(e) = Compiler::<RasnBackend, _>::new()
.add_asn_by_path(path.clone())
.compile_to_string()
{
Expand Down Expand Up @@ -48,15 +48,18 @@ Failed to parse {failed} modules with the following errors:
fn compile_etsi() {
println!(
"{:?}",
Compiler::new()
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_class.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_common.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_const.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_container.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_ies.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_pdus.asn")
.add_asn_by_path("../rasn-compiler/test_asn1/s1ap.asn")
.set_output_path("./tests")
.compile()
Compiler::<RasnBackend, _>::new_with_config(rasn_compiler::prelude::RasnConfig {
opaque_open_types: false,
default_wildcard_imports: true
})
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_class.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_common.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_const.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_container.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_ies.asn")
// .add_asn_by_path("../rasn-compiler/test_asn1/ngap_pdus.asn")
.add_asn_by_path("../rasn-compiler/test_asn1/Simple.asn")
.set_output_path("./tests")
.compile()
);
}
24 changes: 20 additions & 4 deletions rasn-compiler/src/generator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//! decoding and encoding of the parsed and validated ASN1 data elements.
//! The `generator` uses string templates for generating rust code.

use std::error::Error;
use std::{error::Error, fmt::Debug};

use proc_macro2::TokenStream;

use crate::intermediate::ToplevelDefinition;

Expand All @@ -14,7 +16,8 @@ pub mod rasn;
/// Implementors of the `Backend` trait can be used
/// as a backend to the compiler in order to create bindings
/// for other frameworks and languages than the default backend.
pub trait Backend: Sized {
pub trait Backend: Sized + Default {
type Config: Sized + Default + Debug;
/// generates bindings for an ASN.1 module
/// ### Params
/// - `top_level_declarations` vector of [TopLevelDeclaration]s that are defined in the ASN.1 module
Expand All @@ -23,9 +26,22 @@ pub trait Backend: Sized {
top_level_declarations: Vec<ToplevelDefinition>,
) -> Result<GeneratedModule, GeneratorError>;

fn format_bindings(bindings: &String) -> Result<String, Box<dyn Error>> {
Ok(bindings.clone())
/// generates bindings for a single ASN.1 item
/// ### Params
/// - `tld` [TopLevelDeclaration] for which the bindings should be generated
fn generate(&self, tld: ToplevelDefinition) -> Result<TokenStream, GeneratorError>;

/// Formats the bindings using the language- or framework-specific linters.
/// For example, the Rust backend uses rustfmt for formatting bindings.
fn format_bindings(bindings: &str) -> Result<String, Box<dyn Error>> {
Ok(bindings.to_owned())
}

/// Returns a reference to the backend's config
fn config(&self) -> &Self::Config;

/// Creates a backend from its config
fn from_config(config: Self::Config) -> Self;
}

pub struct GeneratedModule {
Expand Down
Loading

0 comments on commit 03c5cd6

Please sign in to comment.