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: Export CLI as part of library #45

Merged
merged 1 commit into from
Oct 7, 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
93 changes: 2 additions & 91 deletions rasn-compiler/src/bin.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,3 @@
#![cfg(feature = "cli")]
use std::path::PathBuf;

use clap::{arg, command, Parser};
use colored::Colorize;
use rasn_compiler::prelude::*;
use walkdir::WalkDir;

#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct CompilerArgs {
/// Specify a directory for the compiler to search for ASN1 modules.
/// The compiler will search recursively for `.asn` and `.asn1` files
#[arg(short, long)]
directory: Option<PathBuf>,

/// Add an ASN1 module by path. Multiple modules can be added by appending "-m PATH_TO_MODULE"
#[arg(short, long = "module", num_args(0..))]
module_files: Vec<PathBuf>,

/// Set the output path for the generated rust module
#[arg(short, long, default_value = ".")]
output_path: PathBuf,

/// Specify which compiler backend to use:
/// - "rasn" [DEFAULT]: generates rust-bindings for the rasn framework
/// - "typescript": generates typescript type definitions
#[arg(short, long, default_value = "rasn")]
backend: String,
}

pub fn main() {
let args = CompilerArgs::parse();

// Read module paths
let mut modules = args.module_files;

// Scan directory, if given
if let Some(dir) = args.directory {
for entry in WalkDir::new(dir)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
{
let file_name = entry.file_name().to_string_lossy();

if file_name.ends_with(".asn") || file_name.ends_with(".asn1") {
println!("Found ASN1 module {} in directory", file_name);
modules.push(entry.into_path());
}
}
}

if modules.is_empty() {
panic!(
"{}",
"Please provide either a valid path to a module or to a directory containing modules."
.red()
)
}

let results = if args.backend == "typescript" {
Compiler::<TypescriptBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
} else {
Compiler::<RasnBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
};

match results {
Ok(warnings) => {
for warning in warnings {
println!(
"{}\n{}",
"Rasn compiler warning:".yellow(),
warning.to_string().yellow()
)
}
}
Err(error) => {
println!(
"{}\n{}",
"Rasn compiler error:".red(),
error.to_string().red()
)
}
}
fn main() {
rasn_compiler::cli::CompilerArgs::drive();
}
94 changes: 94 additions & 0 deletions rasn-compiler/src/cli.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use std::path::PathBuf;

use crate::prelude::*;

use clap::{arg, command, Parser};
use colored::Colorize;
use walkdir::WalkDir;

#[derive(clap::Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct CompilerArgs {
/// Specify a directory for the compiler to search for ASN1 modules.
/// The compiler will search recursively for `.asn` and `.asn1` files
#[arg(short, long)]
directory: Option<PathBuf>,

/// Add an ASN1 module by path. Multiple modules can be added by appending "-m PATH_TO_MODULE"
#[arg(short, long = "module", num_args(0..))]
module_files: Vec<PathBuf>,

/// Set the output path for the generated rust module
#[arg(short, long, default_value = ".")]
output_path: PathBuf,

/// Specify which compiler backend to use:
/// - "rasn" [DEFAULT]: generates rust-bindings for the rasn framework
/// - "typescript": generates typescript type definitions
#[arg(short, long, default_value = "rasn")]
backend: String,
}

impl CompilerArgs {
pub fn drive() {
let args = CompilerArgs::parse();

// Read module paths
let mut modules = args.module_files;

// Scan directory, if given
if let Some(dir) = args.directory {
for entry in WalkDir::new(dir)
.follow_links(true)
.into_iter()
.filter_map(|e| e.ok())
{
let file_name = entry.file_name().to_string_lossy();

if file_name.ends_with(".asn") || file_name.ends_with(".asn1") {
println!("Found ASN1 module {} in directory", file_name);
modules.push(entry.into_path());
}
}
}

if modules.is_empty() {
panic!(
"{}",
"Please provide either a valid path to a module or to a directory containing modules."
.red()
)
}

let results = if args.backend == "typescript" {
Compiler::<TypescriptBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
} else {
Compiler::<RasnBackend, _>::new()
.add_asn_sources_by_path(modules.into_iter())
.set_output_path(args.output_path)
.compile()
};

match results {
Ok(warnings) => {
for warning in warnings {
println!(
"{}\n{}",
"Rasn compiler warning:".yellow(),
warning.to_string().yellow()
)
}
}
Err(error) => {
println!(
"{}\n{}",
"Rasn compiler error:".red(),
error.to_string().red()
)
}
}
}
}
3 changes: 3 additions & 0 deletions rasn-compiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ mod lexer;
mod tests;
mod validator;

#[cfg(feature = "cli")]
pub mod cli;

use std::{
cell::RefCell,
collections::BTreeMap,
Expand Down
Loading