Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/generator config #17

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading