Release 1.1.4

This commit is contained in:
a2x
2023-10-26 15:41:34 +10:00
parent 631668429c
commit 239c872b65
37 changed files with 1905 additions and 1308 deletions

View File

@@ -1,8 +1,9 @@
use std::io::{Result, Write};
use super::FileBuilder;
/// Represents a C++ header file builder.
use std::io::{Result, Write};
/// A structure representing a builder for C++ header files.
/// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)]
pub struct CppFileBuilder;
@@ -12,10 +13,7 @@ impl FileBuilder for CppFileBuilder {
}
fn write_top_level(&mut self, output: &mut dyn Write) -> Result<()> {
write!(output, "#pragma once\n\n")?;
write!(output, "#include <cstddef>\n\n")?;
Ok(())
write!(output, "#pragma once\n\n#include <cstddef>\n\n")
}
fn write_namespace(
@@ -24,13 +22,9 @@ impl FileBuilder for CppFileBuilder {
name: &str,
comment: Option<&str>,
) -> Result<()> {
if let Some(comment) = comment {
write!(output, "namespace {} {{ // {}\n", name, comment)?;
} else {
write!(output, "namespace {} {{\n", name)?;
}
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
Ok(())
write!(output, "namespace {} {{ {}\n", name, comment)
}
fn write_variable(
@@ -39,24 +33,20 @@ impl FileBuilder for CppFileBuilder {
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
match comment {
Some(comment) => write!(
output,
" constexpr std::ptrdiff_t {} = {:#X}; // {}\n",
name, value, comment
),
None => write!(
output,
" constexpr std::ptrdiff_t {} = {:#X};\n",
name, value
),
}
let indentation = " ".repeat(indentation.unwrap_or(4));
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
write!(
output,
"{}constexpr std::ptrdiff_t {} = {:#X}; {}\n",
indentation, name, value, comment
)
}
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()> {
write!(output, "{}", if eof { "}" } else { "}\n\n" })?;
Ok(())
write!(output, "{}", if eof { "}" } else { "}\n\n" })
}
}

View File

@@ -1,8 +1,9 @@
use std::io::{Result, Write};
use super::FileBuilder;
/// Represents a C# file builder.
use std::io::{Result, Write};
/// A structure representing a builder for C# files.
/// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)]
pub struct CSharpFileBuilder;
@@ -21,13 +22,9 @@ impl FileBuilder for CSharpFileBuilder {
name: &str,
comment: Option<&str>,
) -> Result<()> {
if let Some(comment) = comment {
write!(output, "public static class {} {{ // {}\n", name, comment)?;
} else {
write!(output, "public static class {} {{\n", name)?;
}
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
Ok(())
write!(output, "public static class {} {{ {}\n", name, comment)
}
fn write_variable(
@@ -36,20 +33,20 @@ impl FileBuilder for CSharpFileBuilder {
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
match comment {
Some(comment) => write!(
output,
" public const nint {} = {:#X}; // {}\n",
name, value, comment
),
None => write!(output, " public const nint {} = {:#X};\n", name, value),
}
let indentation = " ".repeat(indentation.unwrap_or(4));
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
write!(
output,
"{}public const nint {} = {:#X}; {}\n",
indentation, name, value, comment
)
}
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()> {
write!(output, "{}", if eof { "}" } else { "}\n\n" })?;
Ok(())
write!(output, "{}", if eof { "}" } else { "}\n\n" })
}
}

View File

@@ -1,14 +1,42 @@
use std::io::{Result, Write};
/// Represents a file builder.
/// A trait that defines the file builder operations.
pub trait FileBuilder {
/// Returns the file extension.
/// Returns the extension of the file.
///
/// # Arguments
///
/// * `&mut self` - A mutable reference to the `FileBuilder` struct.
///
/// # Returns
///
/// * `&str` - A string slice containing the extension of the file.
fn extension(&mut self) -> &str;
/// Writes the top level of the file.
/// Write to the top level of the file. The output destination is `output`.
///
/// # Arguments
///
/// * `&mut self` - A mutable reference to the `FileBuilder` struct.
/// * `output` - An object implementing Write trait where the top level will be written.
///
/// # Returns
///
/// * `Result<()>` - A generic Result type indicating the operations outcome.
fn write_top_level(&mut self, output: &mut dyn Write) -> Result<()>;
/// Writes a namespace.
/// Write a namespace to the output.
///
/// # Arguments
///
/// * `&mut self` - A mutable reference to the `FileBuilder` struct.
/// * `output` - An object implementing Write trait where the namespace will be written.
/// * `name` - The name of the namespace.
/// * `comment` - An optional comment. If present, this comment will be included in the output.
///
/// # Returns
///
/// * `Result<()>` - A generic Result type indicating the operations outcome.
fn write_namespace(
&mut self,
output: &mut dyn Write,
@@ -16,15 +44,39 @@ pub trait FileBuilder {
comment: Option<&str>,
) -> Result<()>;
/// Writes a variable.
/// Write a variable to the output.
///
/// # Arguments
///
/// * `&mut self` - A mutable reference to the `FileBuilder` struct.
/// * `output` - An object implementing Write trait where the variable will be written.
/// * `name` - The name of the variable.
/// * `value` - The value of the variable.
/// * `comment` - An optional comment. If present, this comment will be included in the output.
/// * `indentation` - An optional indentation value. If present, the variable will be written with the specified indentation.
///
/// # Returns
///
/// * `Result<()>` - A generic Result type indicating the operations outcome.
fn write_variable(
&mut self,
output: &mut dyn Write,
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()>;
/// Writes a closure.
/// Writes a closure to the output.
///
/// # Arguments
///
/// * `&mut self` - A mutable reference to the `FileBuilder` struct.
/// * `output` - An object implementing Write trait where the closure will be written.
/// * `eof` - A boolean value, if true, indicates that this is the last element to write to the output.
///
/// # Returns
///
/// * `Result<()>` - A generic Result type indicating the operations outcome.
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()>;
}

View File

@@ -1,27 +1,27 @@
use std::{
collections::BTreeMap,
io::{Result, Write},
};
use super::FileBuilder;
use serde::Serialize;
use super::FileBuilder;
use std::collections::BTreeMap;
use std::io::{Result, Write};
/// Represents an offset value in JSON format.
/// Represents a JSON offset value with an optional comment.
#[derive(Debug, PartialEq, Default, Serialize)]
struct JsonOffsetValue {
value: usize,
comment: Option<String>,
}
/// Represents a module in JSON format.
/// 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.
#[derive(Debug, PartialEq, Default, Serialize)]
struct JsonModule {
data: BTreeMap<String, JsonOffsetValue>,
comment: Option<String>,
}
/// Represents a JSON file builder.
/// A structure representing a builder for JSON files.
/// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq, Default)]
pub struct JsonFileBuilder {
data: BTreeMap<String, JsonModule>,
@@ -43,8 +43,8 @@ impl FileBuilder for JsonFileBuilder {
name: &str,
comment: Option<&str>,
) -> Result<()> {
self.current_namespace = name.to_string();
self.data.entry(name.to_string()).or_default().comment = comment.map(str::to_string);
self.current_namespace = name.to_string();
Ok(())
}
@@ -55,6 +55,7 @@ impl FileBuilder for JsonFileBuilder {
name: &str,
value: usize,
comment: Option<&str>,
_indentation: Option<usize>,
) -> Result<()> {
self.data
.entry(self.current_namespace.clone())
@@ -63,7 +64,7 @@ impl FileBuilder for JsonFileBuilder {
.insert(
name.to_string(),
JsonOffsetValue {
value: value,
value,
comment: comment.map(str::to_string),
},
);
@@ -75,7 +76,7 @@ impl FileBuilder for JsonFileBuilder {
if eof {
write!(output, "{}", serde_json::to_string_pretty(&self.data)?)?;
self.data = BTreeMap::new();
self.data.clear();
}
Ok(())

View File

@@ -1,5 +1,3 @@
pub use std::io::{Result, Write};
pub use cpp_file_builder::CppFileBuilder;
pub use csharp_file_builder::CSharpFileBuilder;
pub use file_builder::FileBuilder;
@@ -7,6 +5,8 @@ pub use json_file_builder::JsonFileBuilder;
pub use python_file_builder::PythonFileBuilder;
pub use rust_file_builder::RustFileBuilder;
pub use std::io::{Result, Write};
pub mod cpp_file_builder;
pub mod csharp_file_builder;
pub mod file_builder;
@@ -14,13 +14,23 @@ pub mod json_file_builder;
pub mod python_file_builder;
pub mod rust_file_builder;
/// Represents a file builder enum.
/// `FileBuilder` is an enum that defines different kinds of file builders.
/// Each variant corresponds to a builder for a particular type of file.
#[derive(Debug, PartialEq)]
pub enum FileBuilderEnum {
/// Represents a builder for C++ header files.
CppFileBuilder(CppFileBuilder),
/// Represents a builder for C# files.
CSharpFileBuilder(CSharpFileBuilder),
/// Represents a builder for JSON files.
JsonFileBuilder(JsonFileBuilder),
/// Represents a builder for Python files.
PythonFileBuilder(PythonFileBuilder),
/// Represents a builder for Rust files.
RustFileBuilder(RustFileBuilder),
}
@@ -48,8 +58,10 @@ impl FileBuilder for FileBuilderEnum {
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
self.as_mut().write_variable(output, name, value, comment)
self.as_mut()
.write_variable(output, name, value, comment, indentation)
}
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()> {

View File

@@ -1,8 +1,9 @@
use std::io::{Result, Write};
use super::FileBuilder;
/// Represents a Python file builder.
use std::io::{Result, Write};
/// A structure representing a builder for Python files.
/// The builder implements the `FileBuilder` trait.
#[derive(Debug, PartialEq)]
pub struct PythonFileBuilder;
@@ -21,13 +22,9 @@ impl FileBuilder for PythonFileBuilder {
name: &str,
comment: Option<&str>,
) -> Result<()> {
if let Some(comment) = comment {
write!(output, "class {}: # {}\n", name, comment)?;
} else {
write!(output, "class {}:\n", name)?;
}
let comment = comment.map_or(String::new(), |c| format!("# {}", c));
Ok(())
write!(output, "class {}: {}\n", name, comment)
}
fn write_variable(
@@ -36,11 +33,17 @@ impl FileBuilder for PythonFileBuilder {
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
match comment {
Some(comment) => write!(output, " {} = {:#X} # {}\n", name, value, comment),
None => write!(output, " {} = {:#X}\n", name, value),
}
let indentation = " ".repeat(indentation.unwrap_or(4));
let comment = comment.map_or(String::new(), |c| format!("# {}", c));
write!(
output,
"{}{} = {:#X} {}\n",
indentation, name, value, comment
)
}
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()> {

View File

@@ -1,8 +1,9 @@
use std::io::{Result, Write};
use super::FileBuilder;
/// Represents a Rust file builder.
use std::io::{Result, Write};
/// A structure representing a builder for Rust files.
/// The builder implements the `FileBuilder` trait.
#[derive(Debug, Default, PartialEq)]
pub struct RustFileBuilder;
@@ -15,9 +16,7 @@ impl FileBuilder for RustFileBuilder {
write!(
output,
"#![allow(non_snake_case, non_upper_case_globals)]\n\n"
)?;
Ok(())
)
}
fn write_namespace(
@@ -26,13 +25,9 @@ impl FileBuilder for RustFileBuilder {
name: &str,
comment: Option<&str>,
) -> Result<()> {
if let Some(comment) = comment {
write!(output, "pub mod {} {{ // {}\n", name, comment)?;
} else {
write!(output, "pub mod {} {{\n", name)?;
}
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
Ok(())
write!(output, "pub mod {} {{ {}\n", name, comment)
}
fn write_variable(
@@ -41,20 +36,20 @@ impl FileBuilder for RustFileBuilder {
name: &str,
value: usize,
comment: Option<&str>,
indentation: Option<usize>,
) -> Result<()> {
match comment {
Some(comment) => write!(
output,
" pub const {}: usize = {:#X}; // {}\n",
name, value, comment
),
None => write!(output, " pub const {}: usize = {:#X};\n", name, value),
}
let indentation = " ".repeat(indentation.unwrap_or(4));
let comment = comment.map_or(String::new(), |c| format!("// {}", c));
write!(
output,
"{}pub const {}: usize = {:#X}; {}\n",
indentation, name, value, comment
)
}
fn write_closure(&mut self, output: &mut dyn Write, eof: bool) -> Result<()> {
write!(output, "{}", if eof { "}" } else { "}\n\n" })?;
Ok(())
write!(output, "{}", if eof { "}" } else { "}\n\n" })
}
}