Added ability to specify file builders

Thanks @pablo67340 (#26)
This commit is contained in:
a2x 2023-11-01 10:01:39 +10:00
parent 56f3255151
commit e06df502e9
8 changed files with 54 additions and 26 deletions

View File

@ -5,6 +5,7 @@ charset = utf-8
end_of_line = lf end_of_line = lf
indent_size = 4 indent_size = 4
indent_style = space indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
[*.json] [*.json]

View File

@ -4,7 +4,7 @@ use std::io::{Result, Write};
/// A structure representing a builder for C++ header files. /// A structure representing a builder for C++ header files.
/// The builder implements the `FileBuilder` trait. /// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct CppFileBuilder; pub struct CppFileBuilder;
impl FileBuilder for CppFileBuilder { impl FileBuilder for CppFileBuilder {

View File

@ -4,7 +4,7 @@ use std::io::{Result, Write};
/// A structure representing a builder for C# files. /// A structure representing a builder for C# files.
/// The builder implements the `FileBuilder` trait. /// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct CSharpFileBuilder; pub struct CSharpFileBuilder;
impl FileBuilder for CSharpFileBuilder { impl FileBuilder for CSharpFileBuilder {

View File

@ -6,7 +6,7 @@ use std::collections::BTreeMap;
use std::io::{Result, Write}; use std::io::{Result, Write};
/// Represents a JSON offset value with an optional comment. /// Represents a JSON offset value with an optional comment.
#[derive(Debug, PartialEq, Default, Serialize)] #[derive(Clone, Debug, Default, PartialEq, Serialize)]
struct JsonOffsetValue { struct JsonOffsetValue {
value: usize, value: usize,
comment: Option<String>, comment: Option<String>,
@ -14,7 +14,7 @@ struct JsonOffsetValue {
/// Represents a JSON module, which contains data in the form of a `BTreeMap` of string keys and /// Represents a JSON module, which contains data in the form of a `BTreeMap` of string keys and
/// `JsonOffsetValue` values, as well as an optional comment. /// `JsonOffsetValue` values, as well as an optional comment.
#[derive(Debug, PartialEq, Default, Serialize)] #[derive(Clone, Debug, Default, PartialEq, Serialize)]
struct JsonModule { struct JsonModule {
data: BTreeMap<String, JsonOffsetValue>, data: BTreeMap<String, JsonOffsetValue>,
comment: Option<String>, comment: Option<String>,
@ -22,7 +22,7 @@ struct JsonModule {
/// A structure representing a builder for JSON files. /// A structure representing a builder for JSON files.
/// The builder implements the `FileBuilder` trait. /// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq, Default)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct JsonFileBuilder { pub struct JsonFileBuilder {
data: BTreeMap<String, JsonModule>, data: BTreeMap<String, JsonModule>,
current_namespace: String, current_namespace: String,

View File

@ -16,7 +16,7 @@ pub mod rust_file_builder;
/// `FileBuilder` is an enum that defines different kinds of file builders. /// `FileBuilder` is an enum that defines different kinds of file builders.
/// Each variant corresponds to a builder for a particular type of file. /// Each variant corresponds to a builder for a particular type of file.
#[derive(Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum FileBuilderEnum { pub enum FileBuilderEnum {
/// Represents a builder for C++ header files. /// Represents a builder for C++ header files.
CppFileBuilder(CppFileBuilder), CppFileBuilder(CppFileBuilder),

View File

@ -4,7 +4,7 @@ use std::io::{Result, Write};
/// A structure representing a builder for Python files. /// A structure representing a builder for Python files.
/// The builder implements the `FileBuilder` trait. /// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct PythonFileBuilder; pub struct PythonFileBuilder;
impl FileBuilder for PythonFileBuilder { impl FileBuilder for PythonFileBuilder {

View File

@ -4,7 +4,7 @@ use std::io::{Result, Write};
/// A structure representing a builder for Rust files. /// A structure representing a builder for Rust files.
/// The builder implements the `FileBuilder` trait. /// The builder implements the `FileBuilder` trait.
#[derive(Debug, Default, PartialEq)] #[derive(Clone, Debug, Default, PartialEq)]
pub struct RustFileBuilder; pub struct RustFileBuilder;
impl FileBuilder for RustFileBuilder { impl FileBuilder for RustFileBuilder {

View File

@ -42,17 +42,30 @@ struct Args {
#[arg(short, long)] #[arg(short, long)]
schemas: bool, schemas: bool,
/// Enable verbose output. /// List of file builders to use.
#[arg(short, long)] /// Valid values are: `.cs`, `.hpp`, `.json`, `.py`, `.rs`.
verbose: bool, #[arg(
short,
long,
value_parser = parse_extension,
value_delimiter = ',',
default_values = [".cs", ".hpp", ".json", ".py", ".rs"],
)]
builders: Vec<FileBuilderEnum>,
/// Output folder. /// Indentation level for generated files.
/// Defaults to 4 spaces.
#[arg(long, default_value_t = 4)]
indent: usize,
/// Output directory for generated files.
/// Defaults to `generated`.
#[arg(long, default_value = "generated")] #[arg(long, default_value = "generated")]
output: String, output: String,
/// Indentation level. /// Enable verbose output.
#[arg(long, default_value = "4")] #[arg(short, long)]
indent: usize, verbose: bool,
} }
fn main() -> Result<()> { fn main() -> Result<()> {
@ -60,9 +73,10 @@ fn main() -> Result<()> {
interfaces, interfaces,
offsets, offsets,
schemas, schemas,
verbose, mut builders,
output,
indent, indent,
output,
verbose,
} = Args::parse(); } = Args::parse();
let log_level = if verbose { let log_level = if verbose {
@ -83,14 +97,6 @@ fn main() -> Result<()> {
process.initialize()?; process.initialize()?;
let mut builders: Vec<FileBuilderEnum> = vec![
FileBuilderEnum::CppFileBuilder(CppFileBuilder),
FileBuilderEnum::CSharpFileBuilder(CSharpFileBuilder),
FileBuilderEnum::JsonFileBuilder(JsonFileBuilder::default()),
FileBuilderEnum::PythonFileBuilder(PythonFileBuilder),
FileBuilderEnum::RustFileBuilder(RustFileBuilder),
];
let all = !(interfaces || offsets || schemas); let all = !(interfaces || offsets || schemas);
if schemas || all { if schemas || all {
@ -106,9 +112,30 @@ fn main() -> Result<()> {
} }
info!( info!(
"<on-green>Done!</> <green>Time elapsed: {:?}</>", "<on-green>Done!</> <green>Time elapsed: <b>{:?}</></>",
now.elapsed() now.elapsed()
); );
Ok(()) Ok(())
} }
/// Parses the given file extension and returns the corresponding `FileBuilderEnum`.
///
/// # Arguments
///
/// * `extension` - A string slice that represents the file extension.
///
/// # Returns
///
/// * `Ok(FileBuilderEnum)` - If the extension is valid, returns the corresponding `FileBuilderEnum`.
/// * `Err(&'static str)` - If the extension is invalid, returns an error message.
fn parse_extension(extension: &str) -> Result<FileBuilderEnum, &'static str> {
match extension {
".cs" => Ok(FileBuilderEnum::CSharpFileBuilder(CSharpFileBuilder)),
".hpp" => Ok(FileBuilderEnum::CppFileBuilder(CppFileBuilder)),
".json" => Ok(FileBuilderEnum::JsonFileBuilder(JsonFileBuilder::default())),
".py" => Ok(FileBuilderEnum::PythonFileBuilder(PythonFileBuilder)),
".rs" => Ok(FileBuilderEnum::RustFileBuilder(RustFileBuilder)),
_ => Err("Invalid extension"),
}
}