From 17d0f1430c2933710d7aa7d2cc3723e15d26d84c Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 26 Sep 2024 19:12:26 +0000 Subject: [PATCH] Format Rust code using rustfmt --- auto-cli/src/main.rs | 11 ++-- cli/src/args.rs | 6 +- cli/src/builder.rs | 10 +-- cli/src/main.rs | 18 ++++-- compiler/src/api/build_script.rs | 2 +- compiler/src/api/config/core.rs | 15 +++-- compiler/src/api/config/mod.rs | 2 +- compiler/src/api/config/model.rs | 20 +++--- compiler/src/api/core/generator.rs | 72 ++++++++++++++------- compiler/src/api/core/loader.rs | 41 +++++++----- compiler/src/api/core/mod.rs | 4 +- compiler/src/api/tools/mod.rs | 32 ++++++--- compiler/src/api/tools/rust.rs | 60 ++++++++++------- compiler/src/api/tools/swift.rs | 8 ++- compiler/src/compiler/enum.rs | 2 +- compiler/src/compiler/message.rs | 12 +++- compiler/src/compiler/protocol.rs | 16 +++-- compiler/src/compiler/structure.rs | 36 ++++++----- compiler/src/compiler/union.rs | 4 +- compiler/src/compiler/util/imports.rs | 10 +-- compiler/src/compiler/util/store.rs | 10 +-- compiler/src/gen/base/enum.rs | 5 +- compiler/src/gen/base/map.rs | 2 +- compiler/src/gen/base/message.rs | 19 ++++-- compiler/src/gen/base/message_common.rs | 2 +- compiler/src/gen/base/message_from_slice.rs | 2 +- compiler/src/gen/base/message_write.rs | 2 +- compiler/src/gen/base/structure.rs | 28 +++++--- compiler/src/gen/base/union.rs | 7 +- compiler/src/gen/rust/message.rs | 2 +- compiler/src/gen/rust/message_from_slice.rs | 2 +- compiler/src/gen/rust/message_offsets.rs | 2 +- compiler/src/gen/rust/message_write.rs | 2 +- compiler/src/gen/rust/mod.rs | 8 +-- compiler/src/gen/rust/solver.rs | 2 +- compiler/src/gen/rust/union.rs | 2 +- compiler/src/gen/rust/util.rs | 2 +- compiler/src/gen/swift/imports.rs | 9 ++- compiler/src/gen/swift/mod.rs | 2 +- compiler/src/gen/swift/solver.rs | 2 +- compiler/src/gen/swift/util.rs | 2 +- compiler/src/gen/template/core.rs | 2 +- compiler/src/gen/template/mod.rs | 2 +- compiler/src/gen/template/options.rs | 8 +-- compiler/src/gen/template/parse_tree.rs | 2 +- compiler/src/model/message.rs | 4 +- compiler/src/model/protocol.rs | 4 +- compiler/src/model/structure.rs | 2 +- compiler/src/model/union.rs | 2 +- 49 files changed, 319 insertions(+), 202 deletions(-) diff --git a/auto-cli/src/main.rs b/auto-cli/src/main.rs index a3a12a2..caaac36 100644 --- a/auto-cli/src/main.rs +++ b/auto-cli/src/main.rs @@ -26,10 +26,10 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::path::{Path, PathBuf}; +use bp3d_protoc::api::tools::GenTools; use bp3d_util::result::ResultExt; use clap::{Parser, ValueEnum}; -use bp3d_protoc::api::tools::GenTools; +use std::path::{Path, PathBuf}; #[derive(ValueEnum, Copy, Clone, Debug)] pub enum Generator { @@ -53,7 +53,7 @@ pub struct Args { pub output: Option, /// Name of the code generator to use. #[clap(short = 'g', long = "generator", default_value = "rust")] - pub generator: Generator + pub generator: Generator, } fn main() { @@ -61,6 +61,7 @@ fn main() { let output = args.output.as_deref().unwrap_or(Path::new("./")); match args.generator { Generator::Rust => bp3d_protoc::api::tools::Rust::run_file(args.input, output, |_| {}), - Generator::Swift => bp3d_protoc::api::tools::Swift::run_file(args.input, output, |_| {}) - }.expect_exit("Failed to run make tool", 1); + Generator::Swift => bp3d_protoc::api::tools::Swift::run_file(args.input, output, |_| {}), + } + .expect_exit("Failed to run make tool", 1); } diff --git a/cli/src/args.rs b/cli/src/args.rs index 3602ec5..8266a1a 100644 --- a/cli/src/args.rs +++ b/cli/src/args.rs @@ -26,10 +26,10 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use bp3d_protoc::api::core::generator::Params; +use clap::{Parser, ValueEnum}; use std::ffi::OsString; use std::path::{Path, PathBuf}; -use clap::{Parser, ValueEnum}; -use bp3d_protoc::api::core::generator::Params; #[derive(ValueEnum, Copy, Clone, Debug)] pub enum Generator { @@ -69,7 +69,7 @@ impl Feature { Feature::UseEnums => params.use_enums = true, Feature::UseStructs => params.use_structs = true, Feature::UseMessages => params.use_messages = true, - Feature::UseUnions => params.use_unions = true + Feature::UseUnions => params.use_unions = true, }; } } diff --git a/cli/src/builder.rs b/cli/src/builder.rs index 130cf3c..e48e3d2 100644 --- a/cli/src/builder.rs +++ b/cli/src/builder.rs @@ -26,17 +26,17 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::path::Path; -use bp3d_util::result::ResultExt; +use crate::Args; use bp3d_protoc::api::core::generator::{Context, Generator, Params}; use bp3d_protoc::api::core::loader::Loader; use bp3d_protoc::compiler::util::imports::ImportSolver; -use crate::Args; +use bp3d_util::result::ResultExt; +use std::path::Path; pub struct Builder<'a, I, G> { pub context: Context<'a>, pub generator: Generator<'a, I, G>, - pub params: Params + pub params: Params, } impl<'a, I: ImportSolver, G: bp3d_protoc::gen::Generator> Builder<'a, I, G> { @@ -59,7 +59,7 @@ impl<'a, I: ImportSolver, G: bp3d_protoc::gen::Generator> Builder<'a, I, G> { Self { context, generator, - params + params, } } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 7750fe3..01ebb5d 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -26,24 +26,30 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -mod builder; mod args; +mod builder; +use crate::args::{Args, Generator}; +use crate::builder::Builder; +use bp3d_protoc::api::core::loader::Loader; use bp3d_protoc::gen::{GeneratorRust, GeneratorSwift, RustImportSolver, RustParams, SwiftImportSolver}; use bp3d_util::result::ResultExt; use clap::Parser; -use bp3d_protoc::api::core::loader::Loader; -use crate::args::{Args, Generator}; -use crate::builder::Builder; fn build_swift(loader: Loader, args: &Args) { let mut builder = Builder::new(loader, args, &SwiftImportSolver, GeneratorSwift); - builder.generator.generate_all(&mut builder.context, &builder.params, builder.generator.protocols()).expect_exit("failed to generate protocols", 1); + builder + .generator + .generate_all(&mut builder.context, &builder.params, builder.generator.protocols()) + .expect_exit("failed to generate protocols", 1); } fn build_rust(loader: Loader, args: &Args) { let mut builder = Builder::new(loader, args, &RustImportSolver, GeneratorRust); - builder.generator.generate_all(&mut builder.context, &builder.params, &RustParams::default()).expect_exit("failed to generate protocols", 1); + builder + .generator + .generate_all(&mut builder.context, &builder.params, &RustParams::default()) + .expect_exit("failed to generate protocols", 1); } fn main() { diff --git a/compiler/src/api/build_script.rs b/compiler/src/api/build_script.rs index 4e06ecf..7854fc9 100644 --- a/compiler/src/api/build_script.rs +++ b/compiler/src/api/build_script.rs @@ -26,9 +26,9 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::path::Path; use crate::api::tools::Error; use crate::api::tools::{GenTools, Rust}; +use std::path::Path; /// A simple function to quickly generate protocols in Rust for use with the Cargo build system. /// diff --git a/compiler/src/api/config/core.rs b/compiler/src/api/config/core.rs index df7e69a..5787e47 100644 --- a/compiler/src/api/config/core.rs +++ b/compiler/src/api/config/core.rs @@ -26,14 +26,14 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::borrow::Cow; -use bp3d_debug::trace; -use serde::Deserialize; use crate::api::config::model::Config; -use crate::api::core::loader::Loader; use crate::api::core::generator::{Context, Generator, Params}; -use crate::compiler::util::imports::{ImportSolver, ProtocolStore}; +use crate::api::core::loader::Loader; use crate::api::core::Error; +use crate::compiler::util::imports::{ImportSolver, ProtocolStore}; +use bp3d_debug::trace; +use serde::Deserialize; +use std::borrow::Cow; pub fn parse<'a, T: Deserialize<'a>>(data: &'a str) -> Result, toml::de::Error> { toml::from_str(data) @@ -55,7 +55,8 @@ pub fn generate<'a, G: crate::gen::Generator, T, I: ImportSolver, F: Fn(&T) -> O context: &mut Context<'a>, config: &Config, generator_params_converter: F, - generator_default_params: &G::Params<'_>) -> Result<(), Error> { + generator_default_params: &G::Params<'_>, +) -> Result<(), Error> { if let Some(options) = &config.options { for (protocol, params) in options { let protocol_path: Cow = if config.package.name.is_empty() { @@ -63,7 +64,7 @@ pub fn generate<'a, G: crate::gen::Generator, T, I: ImportSolver, F: Fn(&T) -> O } else { Cow::Owned(format!("{}::{}", config.package.name, protocol)) }; - trace!({path=&*protocol_path}, "Found options for protocol: {}", protocol); + trace!({ path = &*protocol_path }, "Found options for protocol: {}", protocol); let mut p = Params::default(); if let Some(flag) = params.write_messages { p.write_messages = flag; diff --git a/compiler/src/api/config/mod.rs b/compiler/src/api/config/mod.rs index a932e3b..10151e5 100644 --- a/compiler/src/api/config/mod.rs +++ b/compiler/src/api/config/mod.rs @@ -26,5 +26,5 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -pub mod model; pub mod core; +pub mod model; diff --git a/compiler/src/api/config/model.rs b/compiler/src/api/config/model.rs index c94361f..7e78206 100644 --- a/compiler/src/api/config/model.rs +++ b/compiler/src/api/config/model.rs @@ -26,12 +26,12 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use serde::Deserialize; use std::collections::HashMap; use std::path::Path; -use serde::{Deserialize}; #[derive(Deserialize)] -#[serde(rename_all="kebab-case")] +#[serde(rename_all = "kebab-case")] pub struct RustParams<'a> { #[serde(borrow)] pub disable_read: Option>, @@ -39,11 +39,11 @@ pub struct RustParams<'a> { pub write_async: Option, pub union_set_discriminant: Option, pub list_wrappers: Option, - pub struct_to_mut: Option + pub struct_to_mut: Option, } #[derive(Deserialize)] -#[serde(rename_all="kebab-case")] +#[serde(rename_all = "kebab-case")] pub struct Params { pub write_messages: Option, pub read_messages: Option, @@ -52,11 +52,11 @@ pub struct Params { pub use_messages: Option, pub use_unions: Option, #[serde(flatten)] - pub inner: T + pub inner: T, } #[derive(Deserialize)] -#[serde(rename_all="kebab-case")] +#[serde(rename_all = "kebab-case")] pub struct Package<'a> { pub name: &'a str, pub path: &'a Path, @@ -64,17 +64,17 @@ pub struct Package<'a> { } #[derive(Deserialize)] -#[serde(rename_all="kebab-case")] +#[serde(rename_all = "kebab-case")] pub struct Dependency<'a> { pub path: &'a Path, - pub package: &'a str + pub package: &'a str, } #[derive(Deserialize)] -#[serde(rename_all="kebab-case")] +#[serde(rename_all = "kebab-case")] pub struct Config<'a, T> { #[serde(borrow)] pub package: Package<'a>, pub options: Option>>, - pub dependency: Option>> + pub dependency: Option>>, } diff --git a/compiler/src/api/core/generator.rs b/compiler/src/api/core/generator.rs index 5a091e6..37a5876 100644 --- a/compiler/src/api/core/generator.rs +++ b/compiler/src/api/core/generator.rs @@ -26,24 +26,24 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::marker::PhantomData; -use std::path::{Path, PathBuf}; -use bp3d_debug::trace; -use bp3d_util::index_map::IndexMap; -use bp3d_util::path::PathExt; +use crate::api::core::Error; use crate::compiler; use crate::compiler::util::imports::{ImportSolver, ProtocolStore}; use crate::gen::file::FileType; -use crate::api::core::Error; +use bp3d_debug::trace; +use bp3d_util::index_map::IndexMap; +use bp3d_util::path::PathExt; +use std::marker::PhantomData; +use std::path::{Path, PathBuf}; pub struct Context<'a> { - items: IndexMap> + items: IndexMap>, } impl<'a> Context<'a> { fn new(size: usize) -> Self { Self { - items: IndexMap::with_capacity(size) + items: IndexMap::with_capacity(size), } } @@ -55,7 +55,7 @@ impl<'a> Context<'a> { self.items.get(full_name) } - pub fn iter(&self) -> impl Iterator> { + pub fn iter(&self) -> impl Iterator> { self.items.iter() } } @@ -63,7 +63,7 @@ impl<'a> Context<'a> { pub struct Item<'a> { full_name: &'a str, pub name: &'a str, - pub path: PathBuf + pub path: PathBuf, } impl<'a> bp3d_util::index_map::Index for Item<'a> { @@ -81,7 +81,7 @@ pub struct Params { pub use_enums: bool, pub use_structs: bool, pub use_messages: bool, - pub use_unions: bool + pub use_unions: bool, } impl Default for Params { @@ -119,12 +119,15 @@ pub struct Generator<'a, T, G> { impl<'a, T: ImportSolver, G: crate::gen::Generator> Generator<'a, T, G> { pub fn new<'b>(protocols: ProtocolStore<'a, T>, out_directory: &'a Path, _: G) -> (Context<'b>, Self) { - (Context::new(protocols.len()), Self { - protocols, - out_directory, - generator: PhantomData, - file_header: None - }) + ( + Context::new(protocols.len()), + Self { + protocols, + out_directory, + generator: PhantomData, + file_header: None, + }, + ) } pub fn protocols(&self) -> &ProtocolStore<'a, T> { @@ -136,9 +139,15 @@ impl<'a, T: ImportSolver, G: crate::gen::Generator> Generator<'a, T, G> { self } - fn generate_internal<'b>(&self, protocol: &'b compiler::Protocol, params: &Params, generator_params: &G::Params<'_>) -> Result, Error> { + fn generate_internal<'b>( + &self, + protocol: &'b compiler::Protocol, + params: &Params, + generator_params: &G::Params<'_>, + ) -> Result, Error> { trace!({?params}, "Generating protocol {}", protocol.full_name); - let file_header = self.file_header + let file_header = self + .file_header .map(std::fs::read_to_string) .transpose() .map_err(Error::Io)? @@ -165,7 +174,8 @@ impl<'a, T: ImportSolver, G: crate::gen::Generator> Generator<'a, T, G> { } value }); - let iter = files_iter.into_iter() + let iter = files_iter + .into_iter() .map(|v| v.write(&out_path, file_header.as_deref(), G::get_language_extension())) .filter_map(|v| match v { Ok(o) => o.map(Ok), @@ -185,19 +195,33 @@ impl<'a, T: ImportSolver, G: crate::gen::Generator> Generator<'a, T, G> { Ok(Item { full_name: &protocol.full_name, name, - path: proto_path + path: proto_path, }) } - pub fn generate<'b>(&'b self, context: &mut Context<'b>, full_name: impl AsRef, params: &Params, generator_params: &G::Params<'_>) -> Result<(), Error> { + pub fn generate<'b>( + &'b self, + context: &mut Context<'b>, + full_name: impl AsRef, + params: &Params, + generator_params: &G::Params<'_>, + ) -> Result<(), Error> { if context.get(full_name.as_ref()).is_none() { - let protocol = self.protocols.get(full_name.as_ref()).ok_or_else(|| Error::ProtocolNotFound(full_name.as_ref().into()))?; + let protocol = self + .protocols + .get(full_name.as_ref()) + .ok_or_else(|| Error::ProtocolNotFound(full_name.as_ref().into()))?; context.insert(self.generate_internal(protocol, params, generator_params)?); } Ok(()) } - pub fn generate_all<'b>(&'b self, context: &mut Context<'b>, params: &Params, generator_params: &G::Params<'_>) -> Result<(), Error> { + pub fn generate_all<'b>( + &'b self, + context: &mut Context<'b>, + params: &Params, + generator_params: &G::Params<'_>, + ) -> Result<(), Error> { for protocol in self.protocols.iter() { if context.get(&protocol.full_name).is_none() { context.insert(self.generate_internal(protocol, params, generator_params)?); diff --git a/compiler/src/api/core/loader.rs b/compiler/src/api/core/loader.rs index d8dfbce..60d6c2e 100644 --- a/compiler/src/api/core/loader.rs +++ b/compiler/src/api/core/loader.rs @@ -26,16 +26,16 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::borrow::Cow; -use std::path::Path; -use bp3d_debug::{error, trace}; -use crate::{compiler, model}; use crate::api::core::Error; use crate::compiler::util::imports::{ImportSolver, ProtocolStore}; +use crate::{compiler, model}; +use bp3d_debug::{error, trace}; +use std::borrow::Cow; +use std::path::Path; pub struct Loader<'a> { models: Vec<(&'a str, model::Protocol)>, - max_iterations: usize + max_iterations: usize, } impl<'a> Default for Loader<'a> { @@ -48,7 +48,7 @@ impl<'a> Loader<'a> { pub fn new(max_iterations: usize) -> Self { Self { models: Vec::new(), - max_iterations + max_iterations, } } @@ -79,14 +79,21 @@ impl<'a> Loader<'a> { while !self.models.is_empty() && iterations > 0 { let (package, model) = self.models.pop().unwrap(); trace!({imports=?model.imports}, "Solving imports for model {}", model.name); - if model.imports.as_ref().map(|v| v.iter().any(|v| { - let full_name = if package.is_empty() { - Cow::Borrowed(&v.protocol) - } else { - Cow::Owned(format!("{}::{}", package, v.protocol)) - }; - protocols.get(&full_name).is_none() - })).unwrap_or_default() { + if model + .imports + .as_ref() + .map(|v| { + v.iter().any(|v| { + let full_name = if package.is_empty() { + Cow::Borrowed(&v.protocol) + } else { + Cow::Owned(format!("{}::{}", package, v.protocol)) + }; + protocols.get(&full_name).is_none() + }) + }) + .unwrap_or_default() + { self.models.insert(0, (package, model)); iterations -= 1; continue; @@ -95,7 +102,11 @@ impl<'a> Loader<'a> { protocols.insert(proto); } if iterations == 0 && self.models.len() > 0 { - error!("Failed to solve protocol import order in {} iterations, {} model(s) could not be solved...", self.max_iterations, self.models.len()); + error!( + "Failed to solve protocol import order in {} iterations, {} model(s) could not be solved...", + self.max_iterations, + self.models.len() + ); return Err(Error::SolverMaxIterations); } Ok(protocols) diff --git a/compiler/src/api/core/mod.rs b/compiler/src/api/core/mod.rs index 72b98e9..81b1af1 100644 --- a/compiler/src/api/core/mod.rs +++ b/compiler/src/api/core/mod.rs @@ -28,8 +28,8 @@ //! This module contains the core mid-level interface to the low-level protocol compiler/generator. -pub mod loader; -pub mod generator; mod error; +pub mod generator; +pub mod loader; pub use error::Error; diff --git a/compiler/src/api/tools/mod.rs b/compiler/src/api/tools/mod.rs index 96b584e..570b3d1 100644 --- a/compiler/src/api/tools/mod.rs +++ b/compiler/src/api/tools/mod.rs @@ -29,16 +29,16 @@ #[cfg(feature = "gen-rust")] mod rust; +mod error; #[cfg(feature = "gen-swift")] mod swift; -mod error; -pub use error::Error; -use std::path::Path; -use serde::Deserialize; use crate::api::config; use crate::api::core::generator::{Context, Generator}; use crate::compiler::util::imports::ImportSolver; +pub use error::Error; +use serde::Deserialize; +use std::path::Path; pub trait GenTools { type Params<'a>: Deserialize<'a>; @@ -47,9 +47,17 @@ pub trait GenTools { fn new_solver() -> Self::Solver; fn new_generator() -> Self::Generator; - fn generate<'a, 'b>(generator: &'b Generator<'a, Self::Solver, Self::Generator>, context: &mut Context<'b>, config: &config::model::Config>) -> Result<(), Error>; + fn generate<'a, 'b>( + generator: &'b Generator<'a, Self::Solver, Self::Generator>, + context: &mut Context<'b>, + config: &config::model::Config>, + ) -> Result<(), Error>; - fn run(config: &config::model::Config>, out_dir: impl AsRef, post_generation: impl FnOnce(&Context)) -> Result<(), Error> { + fn run( + config: &config::model::Config>, + out_dir: impl AsRef, + post_generation: impl FnOnce(&Context), + ) -> Result<(), Error> { let motherfuckingrust = Self::new_solver(); let protocols = config::core::compile(config, &motherfuckingrust)?; let (mut context, mut generator) = Generator::new(protocols, out_dir.as_ref(), Self::new_generator()); @@ -61,12 +69,20 @@ pub trait GenTools { Ok(()) } - fn run_string(config: impl AsRef, out_dir: impl AsRef, post_generation: impl FnOnce(&Context)) -> Result<(), Error> { + fn run_string( + config: impl AsRef, + out_dir: impl AsRef, + post_generation: impl FnOnce(&Context), + ) -> Result<(), Error> { let config = config::core::parse::>(config.as_ref()).map_err(Error::Config)?; Self::run(&config, out_dir, post_generation) } - fn run_file(config_file: impl AsRef, out_dir: impl AsRef, post_generation: impl FnOnce(&Context)) -> Result<(), Error> { + fn run_file( + config_file: impl AsRef, + out_dir: impl AsRef, + post_generation: impl FnOnce(&Context), + ) -> Result<(), Error> { let str = std::fs::read_to_string(config_file).map_err(Error::Io)?; Self::run_string(str, out_dir, post_generation) } diff --git a/compiler/src/api/tools/rust.rs b/compiler/src/api/tools/rust.rs index cc0a4b4..09da53e 100644 --- a/compiler/src/api/tools/rust.rs +++ b/compiler/src/api/tools/rust.rs @@ -29,8 +29,8 @@ use crate::api::config; use crate::api::config::model; use crate::api::config::model::Config; -use crate::api::tools::Error; use crate::api::core::generator::{Context, Generator}; +use crate::api::tools::Error; use crate::api::tools::GenTools; use crate::gen::{GeneratorRust, RustImportSolver, RustParams}; @@ -49,30 +49,44 @@ impl GenTools for Rust { GeneratorRust } - fn generate<'a, 'b>(generator: &'b Generator<'a, Self::Solver, Self::Generator>, context: &mut Context<'b>, config: &Config>) -> Result<(), Error> { - config::core::generate(generator, context, config, |v| { - if v.disable_write.is_none() && v.disable_read.is_none() && v.write_async.is_none() - && v.union_set_discriminant.is_none() && v.list_wrappers.is_none() - && v.struct_to_mut.is_none() { - return None; - } - let mut params = RustParams::default(); - if let Some(v) = &v.disable_read { - for v in v { - params = params.disable_read(v); + fn generate<'a, 'b>( + generator: &'b Generator<'a, Self::Solver, Self::Generator>, + context: &mut Context<'b>, + config: &Config>, + ) -> Result<(), Error> { + config::core::generate( + generator, + context, + config, + |v| { + if v.disable_write.is_none() + && v.disable_read.is_none() + && v.write_async.is_none() + && v.union_set_discriminant.is_none() + && v.list_wrappers.is_none() + && v.struct_to_mut.is_none() + { + return None; + } + let mut params = RustParams::default(); + if let Some(v) = &v.disable_read { + for v in v { + params = params.disable_read(v); + } } - } - if let Some(v) = &v.disable_write { - for v in v { - params = params.disable_write(v); + if let Some(v) = &v.disable_write { + for v in v { + params = params.disable_write(v); + } } - } - params = params.enable_write_async(v.write_async.unwrap_or_default()); - params = params.enable_union_set_discriminant(v.union_set_discriminant.unwrap_or_default()); - params = params.enable_list_wrappers(v.list_wrappers.unwrap_or_default()); - params = params.enable_struct_to_mut(v.struct_to_mut.unwrap_or_default()); - Some(params) - }, &RustParams::default())?; + params = params.enable_write_async(v.write_async.unwrap_or_default()); + params = params.enable_union_set_discriminant(v.union_set_discriminant.unwrap_or_default()); + params = params.enable_list_wrappers(v.list_wrappers.unwrap_or_default()); + params = params.enable_struct_to_mut(v.struct_to_mut.unwrap_or_default()); + Some(params) + }, + &RustParams::default(), + )?; Ok(()) } } diff --git a/compiler/src/api/tools/swift.rs b/compiler/src/api/tools/swift.rs index 6332ea4..93766db 100644 --- a/compiler/src/api/tools/swift.rs +++ b/compiler/src/api/tools/swift.rs @@ -28,8 +28,8 @@ use crate::api::config; use crate::api::config::model::Config; -use crate::api::tools::Error; use crate::api::core::generator::{Context, Generator}; +use crate::api::tools::Error; use crate::api::tools::GenTools; use crate::gen::{GeneratorSwift, SwiftImportSolver}; @@ -48,7 +48,11 @@ impl GenTools for Swift { GeneratorSwift } - fn generate<'a, 'b>(generator: &'b Generator<'a, Self::Solver, Self::Generator>, context: &mut Context<'b>, config: &Config>) -> Result<(), Error> { + fn generate<'a, 'b>( + generator: &'b Generator<'a, Self::Solver, Self::Generator>, + context: &mut Context<'b>, + config: &Config>, + ) -> Result<(), Error> { let protocols = generator.protocols(); config::core::generate(generator, context, config, |_| None, protocols)?; Ok(()) diff --git a/compiler/src/compiler/enum.rs b/compiler/src/compiler/enum.rs index c978374..f33c41f 100644 --- a/compiler/src/compiler/enum.rs +++ b/compiler/src/compiler/enum.rs @@ -29,8 +29,8 @@ use crate::compiler::structure::FixedFieldType; use crate::compiler::util::store::name_index; use crate::compiler::Error; -use std::collections::HashMap; use crate::model::protocol::Description; +use std::collections::HashMap; #[derive(Clone, Debug)] pub struct Enum { diff --git a/compiler/src/compiler/message.rs b/compiler/src/compiler/message.rs index 020c0f8..71c194e 100644 --- a/compiler/src/compiler/message.rs +++ b/compiler/src/compiler/message.rs @@ -30,6 +30,7 @@ use crate::compiler::error::Error; use crate::compiler::structure::{FixedFieldType, Structure}; use crate::compiler::union::Union; use crate::compiler::util::store::name_index; +use crate::compiler::util::types::{Name, PtrKey}; use crate::compiler::Protocol; use crate::model::message::MessageFieldType; use crate::model::protocol::{Description, Endianness}; @@ -37,7 +38,6 @@ use crate::model::structure::StructFieldType; use std::cell::Cell; use std::fmt::{Display, Formatter}; use std::rc::Rc; -use crate::compiler::util::types::{Name, PtrKey}; #[derive(Clone, Debug)] pub enum Referenced { @@ -117,7 +117,13 @@ pub struct SizedListField { impl Display for SizedListField { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "SizedList<{}, Len = {}, Size = {}>", self.item_type.name(), self.ty, self.size_ty) + write!( + f, + "SizedList<{}, Len = {}, Size = {}>", + self.item_type.name(), + self.ty, + self.size_ty + ) } } @@ -169,7 +175,7 @@ impl Display for FieldType { FieldType::Union(v) => v.fmt(f), FieldType::List(v) => v.fmt(f), FieldType::SizedList(v) => v.fmt(f), - FieldType::Payload => f.write_str("Bytes") + FieldType::Payload => f.write_str("Bytes"), } } } diff --git a/compiler/src/compiler/protocol.rs b/compiler/src/compiler/protocol.rs index be67df5..f14e6f4 100644 --- a/compiler/src/compiler/protocol.rs +++ b/compiler/src/compiler/protocol.rs @@ -26,18 +26,18 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::borrow::Cow; use crate::compiler::error::Error; use crate::compiler::message::Message; use crate::compiler::r#enum::Enum; use crate::compiler::structure::Structure; use crate::compiler::union::Union; -use crate::model::protocol::{Description, Endianness}; -use bp3d_debug::trace; -use std::rc::Rc; use crate::compiler::util::imports::{ImportSolver, ProtocolStore}; use crate::compiler::util::store::ObjectStore; use crate::compiler::util::types::{Name, PtrKey, TypePathMap}; +use crate::model::protocol::{Description, Endianness}; +use bp3d_debug::trace; +use std::borrow::Cow; +use std::rc::Rc; #[derive(Clone, Debug)] pub struct Protocol { @@ -125,7 +125,11 @@ impl Protocol { } } - pub fn from_model(value: crate::model::Protocol, protocols: &ProtocolStore, package: &str) -> Result { + pub fn from_model( + value: crate::model::Protocol, + protocols: &ProtocolStore, + package: &str, + ) -> Result { let full_name = if package.is_empty() { value.name } else { @@ -139,7 +143,7 @@ impl Protocol { structs: ObjectStore::new(), messages: ObjectStore::new(), enums: ObjectStore::new(), - unions: ObjectStore::new() + unions: ObjectStore::new(), }; if let Some(mut imports) = value.imports { let mut solved_imports = Vec::new(); diff --git a/compiler/src/compiler/structure.rs b/compiler/src/compiler/structure.rs index 4b1b818..0899456 100644 --- a/compiler/src/compiler/structure.rs +++ b/compiler/src/compiler/structure.rs @@ -26,17 +26,17 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::fmt::{Display, Formatter}; use crate::compiler::error::Error; use crate::compiler::r#enum::Enum; +use crate::compiler::util::store::name_index; +use crate::compiler::util::try2; +use crate::compiler::util::types::Name; use crate::compiler::Protocol; use crate::model::protocol::{Description, Endianness}; use crate::model::structure::{SimpleType, StructFieldType, StructFieldView}; use bp3d_debug::trace; +use std::fmt::{Display, Formatter}; use std::rc::Rc; -use crate::compiler::util::store::name_index; -use crate::compiler::util::try2; -use crate::compiler::util::types::Name; #[derive(Copy, Clone, Debug, Eq, PartialEq)] pub enum FixedFieldType { @@ -66,7 +66,7 @@ impl Display for FixedFieldType { FixedFieldType::UInt64 => f.write_str("UInt64"), FixedFieldType::Float32 => f.write_str("Float32"), FixedFieldType::Float64 => f.write_str("Float64"), - FixedFieldType::Bool => f.write_str("Bool") + FixedFieldType::Bool => f.write_str("Bool"), } } } @@ -149,7 +149,14 @@ pub struct Location { impl Display for Location { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { - write!(f, "bytes {}..{}, bits {}..{}", self.byte_offset, self.byte_offset + self.byte_size, self.bit_offset, self.bit_offset + self.bit_size) + write!( + f, + "bytes {}..{}, bits {}..{}", + self.byte_offset, + self.byte_offset + self.byte_size, + self.bit_offset, + self.bit_offset + self.bit_size + ) } } @@ -267,7 +274,7 @@ pub struct FixedArrayField { pub ty: FixedFieldType, pub array_len: usize, pub endianness: Endianness, - pub item_bit_size: usize + pub item_bit_size: usize, } impl Display for FixedArrayField { @@ -288,12 +295,11 @@ impl Display for FieldType { match self { FieldType::Fixed(v) => v.fmt(f), FieldType::Array(v) => v.fmt(f), - FieldType::Struct(v) => f.write_str(v.name()) + FieldType::Struct(v) => f.write_str(v.name()), } } } - impl FieldType { pub fn as_fixed(&self) -> Option<&FixedField> { match self { @@ -308,7 +314,7 @@ pub struct Field { pub name: String, pub loc: Location, pub description: Option, - pub ty: FieldType + pub ty: FieldType, } impl Display for Field { @@ -332,7 +338,7 @@ impl Field { name: value.name, ty: FieldType::Struct(r.clone()), loc: Location::from_model(r.bit_size, last_bit_offset), - description: value.description + description: value.description, }, last_bit_offset + r.bit_size, )) @@ -355,10 +361,10 @@ impl Field { endianness: proto.endianness, array_len, ty, - item_bit_size: loc.bit_size / array_len + item_bit_size: loc.bit_size / array_len, }), loc, - description: value.description + description: value.description, }, last_bit_offset + bit_size, )) @@ -369,10 +375,10 @@ impl Field { ty: FieldType::Fixed(FixedField { endianness: proto.endianness, ty, - view + view, }), loc, - description: value.description + description: value.description, }, last_bit_offset + bit_size, )) diff --git a/compiler/src/compiler/union.rs b/compiler/src/compiler/union.rs index 778222a..34261ba 100644 --- a/compiler/src/compiler/union.rs +++ b/compiler/src/compiler/union.rs @@ -28,10 +28,10 @@ use crate::compiler::message::{Referenced, SizeInfo}; use crate::compiler::structure::{Field, FieldType, FieldView, FixedField, Structure}; -use crate::compiler::{Error, Protocol}; -use std::rc::Rc; use crate::compiler::util::store::name_index; +use crate::compiler::{Error, Protocol}; use crate::model::protocol::Description; +use std::rc::Rc; #[derive(Clone, Debug)] pub struct UnionField { diff --git a/compiler/src/compiler/util/imports.rs b/compiler/src/compiler/util/imports.rs index ee6ece8..7d9fd80 100644 --- a/compiler/src/compiler/util/imports.rs +++ b/compiler/src/compiler/util/imports.rs @@ -26,9 +26,9 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::fmt::{Debug, Formatter}; -use bp3d_util::index_map::IndexMap; use crate::compiler::Protocol; +use bp3d_util::index_map::IndexMap; +use std::fmt::{Debug, Formatter}; pub trait ImportSolver { fn get_full_type_path(&self, protocol: &Protocol, type_name: &str) -> Option; @@ -42,14 +42,14 @@ impl ImportSolver for () { pub struct ProtocolStore<'a, T> { map: IndexMap, - solver: &'a T + solver: &'a T, } impl<'a, T: ImportSolver> ProtocolStore<'a, T> { pub fn new(solver: &'a T) -> Self { Self { map: IndexMap::new(), - solver + solver, } } @@ -73,7 +73,7 @@ impl<'a, T: ImportSolver> ProtocolStore<'a, T> { self.solver.get_full_type_path(protocol, type_name) } - pub fn iter(&self) -> impl Iterator { + pub fn iter(&self) -> impl Iterator { self.map.iter() } } diff --git a/compiler/src/compiler/util/store.rs b/compiler/src/compiler/util/store.rs index 6f38e21..d7b2137 100644 --- a/compiler/src/compiler/util/store.rs +++ b/compiler/src/compiler/util/store.rs @@ -26,27 +26,27 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use bp3d_util::index_map::IndexMap; use std::collections::HashMap; use std::rc::Rc; -use bp3d_util::index_map::IndexMap; #[derive(Clone, Debug)] pub struct ObjectStore { objects: Vec>, objects_by_name: IndexMap>, - objects_imports: HashMap> + objects_imports: HashMap>, } -impl> ObjectStore { +impl> ObjectStore { pub fn new() -> Self { Self { objects: Vec::new(), objects_by_name: IndexMap::new(), - objects_imports: HashMap::new() + objects_imports: HashMap::new(), } } - pub fn iter(&self) -> impl Iterator> { + pub fn iter(&self) -> impl Iterator> { self.objects.iter() } diff --git a/compiler/src/gen/base/enum.rs b/compiler/src/gen/base/enum.rs index f39bda6..3f49c9a 100644 --- a/compiler/src/gen/base/enum.rs +++ b/compiler/src/gen/base/enum.rs @@ -34,7 +34,10 @@ use itertools::Itertools; pub fn generate<'variable, U: Utilities>(mut template: Template<'_, 'variable>, e: &'variable Enum) -> String { template .var("name", &e.name) - .var("description", e.description.as_ref().map(U::gen_description).unwrap_or("".into())) + .var( + "description", + e.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ) .var_d("largest", e.largest) .var("repr_type", U::get_field_type(e.repr_type)); let mut code = e diff --git a/compiler/src/gen/base/map.rs b/compiler/src/gen/base/map.rs index 165ea03..5613dac 100644 --- a/compiler/src/gen/base/map.rs +++ b/compiler/src/gen/base/map.rs @@ -26,8 +26,8 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::borrow::Cow; use crate::compiler::util::types::{Name, PtrKey, TypeMapper, TypePathMap}; +use std::borrow::Cow; pub struct DefaultTypeMapper; diff --git a/compiler/src/gen/base/message.rs b/compiler/src/gen/base/message.rs index 6ede6a8..90c83b6 100644 --- a/compiler/src/gen/base/message.rs +++ b/compiler/src/gen/base/message.rs @@ -28,11 +28,11 @@ use crate::compiler::message::{Field, FieldType, Message, Referenced}; use crate::compiler::structure::FixedFieldType; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::template::Template; use crate::model::protocol::Endianness; use itertools::Itertools; -use crate::compiler::util::types::TypeMapper; pub enum StringType { Varchar, @@ -58,9 +58,7 @@ pub fn gen_msg_field_decl( type_path_map: &TypePathMapper, ) -> String { let mut scope = template.scope(); - scope.var("name", &field.name) - .var("description", "") - .var_d("info", field); + scope.var("name", &field.name).var("description", "").var_d("info", field); let msg_type = match &field.ty { FieldType::Fixed(ty) => U::get_field_type(ty.ty).into(), FieldType::Ref(v) => match v { @@ -105,8 +103,12 @@ pub fn gen_message_array_type_decls( msg.fields .iter() .filter_map(|field| { - scope.var("name", &field.name) - .var("description", field.description.as_ref().map(U::gen_description).unwrap_or("".into())) + scope + .var("name", &field.name) + .var( + "description", + field.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ) .var_d("info", field); match &field.ty { FieldType::Array(v) => Some( @@ -142,7 +144,10 @@ pub fn generate<'variable, U: Utilities, T: TypeMapper>( type_path_map: &TypePathMapper, ) -> String { template.var("msg_name", &msg.name); - template.var("msg_description", msg.description.as_ref().map(U::gen_description).unwrap_or("".into())); + template.var( + "msg_description", + msg.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ); let fields = msg.fields.iter().map(|v| gen_msg_field_decl::(v, &template, type_path_map)).join(""); template.scope().var("fields", fields).render("", &["decl"]).unwrap() } diff --git a/compiler/src/gen/base/message_common.rs b/compiler/src/gen/base/message_common.rs index 0f7abd9..dfb32cb 100644 --- a/compiler/src/gen/base/message_common.rs +++ b/compiler/src/gen/base/message_common.rs @@ -27,11 +27,11 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::{Field, FieldType, Message, Referenced}; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::base::message::{StringType, Utilities}; use crate::gen::template::Template; use std::borrow::Cow; -use crate::compiler::util::types::TypeMapper; fn gen_optional<'a, U: Utilities>(optional: bool, type_name: impl Into>) -> Cow<'a, str> { if optional { diff --git a/compiler/src/gen/base/message_from_slice.rs b/compiler/src/gen/base/message_from_slice.rs index f21ea65..f919b44 100644 --- a/compiler/src/gen/base/message_from_slice.rs +++ b/compiler/src/gen/base/message_from_slice.rs @@ -27,12 +27,12 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::{Field, Message}; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::base::message::Utilities; use crate::gen::base::message_common::generate_field_type_inline; use crate::gen::template::Template; use itertools::Itertools; -use crate::compiler::util::types::TypeMapper; fn gen_field_from_slice_impl( msg: &Message, diff --git a/compiler/src/gen/base/message_write.rs b/compiler/src/gen/base/message_write.rs index 0334c7f..b9eef46 100644 --- a/compiler/src/gen/base/message_write.rs +++ b/compiler/src/gen/base/message_write.rs @@ -27,12 +27,12 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::{Field, Message}; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::base::message::Utilities; use crate::gen::base::message_common::generate_field_type_inline; use crate::gen::template::Template; use itertools::Itertools; -use crate::compiler::util::types::TypeMapper; fn gen_field_write_impl( msg: &Message, diff --git a/compiler/src/gen/base/structure.rs b/compiler/src/gen/base/structure.rs index d89b5e1..6a0a87c 100644 --- a/compiler/src/gen/base/structure.rs +++ b/compiler/src/gen/base/structure.rs @@ -26,14 +26,14 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::borrow::Cow; use crate::compiler::structure::{Field, FieldType, FieldView, FixedField, FixedFieldType, Structure}; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::hook::{Render, TemplateHooks}; use crate::gen::template::{Scope, Template}; use crate::model::protocol::{Description, Endianness}; use itertools::Itertools; -use crate::compiler::util::types::TypeMapper; +use std::borrow::Cow; pub trait Utilities { fn get_field_type(field_type: FixedFieldType) -> &'static str; @@ -45,7 +45,7 @@ pub trait Utilities { fn gen_description(desc: &Description) -> Cow { match desc { Description::Single(v) => Cow::Borrowed(v), - Description::Multi(v) => Cow::Owned(v.join(" ")) + Description::Multi(v) => Cow::Owned(v.join(" ")), } } } @@ -60,7 +60,10 @@ fn gen_field_getter( .var_d("start", field.loc.byte_offset) .var_d("end", field.loc.byte_offset + field.loc.byte_size) .var("name", &field.name) - .var("description", field.description.as_ref().map(U::gen_description).unwrap_or("".into())) + .var( + "description", + field.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ) .var_d("info", field); match &field.ty { FieldType::Fixed(v) => { @@ -105,7 +108,10 @@ fn gen_field_setter( .var_d("start", field.loc.byte_offset) .var_d("end", field.loc.byte_offset + field.loc.byte_size) .var("name", &field.name) - .var("description", field.description.as_ref().map(U::gen_description).unwrap_or("".into())) + .var( + "description", + field.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ) .var_d("info", field); match &field.ty { FieldType::Fixed(v) => { @@ -246,10 +252,14 @@ pub fn generate<'variable, U: Utilities, T: TypeMapper>( ) -> String { let mut template = templates.template; let mut field_template = templates.field_template; - field_template.var("struct_name", &s.name) - .var("struct_description", s.description.as_ref().map(U::gen_description).unwrap_or("".into())); - template.var("name", &s.name).var_d("byte_size", s.byte_size) - .var("struct_description", s.description.as_ref().map(U::gen_description).unwrap_or("".into())); + field_template.var("struct_name", &s.name).var( + "struct_description", + s.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ); + template.var("name", &s.name).var_d("byte_size", s.byte_size).var( + "struct_description", + s.description.as_ref().map(U::gen_description).unwrap_or("".into()), + ); let mut code = template.render("", &["decl", "new", "fixed_size", "write_to", "from_slice"]).unwrap(); for frag in hooks.get_fragments("ext") { code += &template.render_frag(frag).unwrap(); diff --git a/compiler/src/gen/base/union.rs b/compiler/src/gen/base/union.rs index c8cc48b..707f043 100644 --- a/compiler/src/gen/base/union.rs +++ b/compiler/src/gen/base/union.rs @@ -28,11 +28,11 @@ use crate::compiler::message::Referenced; use crate::compiler::union::{DiscriminantField, Union}; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::hook::TemplateHooks; use crate::gen::template::Template; use itertools::Itertools; -use crate::compiler::util::types::TypeMapper; pub trait Utilities: crate::gen::base::structure::Utilities { fn gen_discriminant_path(discriminant: &DiscriminantField) -> String; @@ -167,7 +167,10 @@ pub fn generate<'variable, U: Utilities, T: TypeMapper>( hooks: &TemplateHooks, ) -> String { template - .var("discriminant_raw_type", U::get_field_type(u.discriminant.get_leaf_fixed().ty)) + .var( + "discriminant_raw_type", + U::get_field_type(u.discriminant.get_leaf_fixed().ty), + ) .var("union_name", &u.name) .var("discriminant_path_mut", U::gen_discriminant_path_mut(&u.discriminant)) .var("discriminant_path", U::gen_discriminant_path(&u.discriminant)) diff --git a/compiler/src/gen/rust/message.rs b/compiler/src/gen/rust/message.rs index 4c9638b..98e0813 100644 --- a/compiler/src/gen/rust/message.rs +++ b/compiler/src/gen/rust/message.rs @@ -27,11 +27,11 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::Message; +use crate::compiler::util::types::TypePathMap; use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; use crate::gen::base::message::{gen_message_array_type_decls, generate}; use crate::gen::rust::util::RustUtils; use crate::gen::template::Template; -use crate::compiler::util::types::TypePathMap; use crate::gen::RustParams; const TEMPLATE: &[u8] = include_bytes!("./message.template"); diff --git a/compiler/src/gen/rust/message_from_slice.rs b/compiler/src/gen/rust/message_from_slice.rs index 74eb86e..087a6df 100644 --- a/compiler/src/gen/rust/message_from_slice.rs +++ b/compiler/src/gen/rust/message_from_slice.rs @@ -27,12 +27,12 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::Message; +use crate::compiler::util::types::TypePathMap; use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; use crate::gen::base::message_from_slice::generate; use crate::gen::rust::util::{gen_where_clause, RustUtils}; use crate::gen::template::Template; use itertools::Itertools; -use crate::compiler::util::types::TypePathMap; const TEMPLATE: &[u8] = include_bytes!("./message.from_slice.template"); diff --git a/compiler/src/gen/rust/message_offsets.rs b/compiler/src/gen/rust/message_offsets.rs index 7b4847a..aead0a6 100644 --- a/compiler/src/gen/rust/message_offsets.rs +++ b/compiler/src/gen/rust/message_offsets.rs @@ -27,12 +27,12 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::{Field, FieldType, Message, Referenced}; +use crate::compiler::util::types::TypePathMap; use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; use crate::gen::base::message_from_slice::generate_from_slice_impl; use crate::gen::rust::util::{gen_where_clause, RustUtils}; use crate::gen::template::Template; use itertools::Itertools; -use crate::compiler::util::types::TypePathMap; const TEMPLATE: &[u8] = include_bytes!("./message.offsets.template"); diff --git a/compiler/src/gen/rust/message_write.rs b/compiler/src/gen/rust/message_write.rs index 6cd80d2..b1a12f6 100644 --- a/compiler/src/gen/rust/message_write.rs +++ b/compiler/src/gen/rust/message_write.rs @@ -27,13 +27,13 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::message::Message; +use crate::compiler::util::types::TypePathMap; use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; use crate::gen::base::message_write::generate; use crate::gen::rust::util::{gen_where_clause, RustUtils}; use crate::gen::template::Template; use crate::gen::RustParams; use itertools::Itertools; -use crate::compiler::util::types::TypePathMap; const TEMPLATE: &[u8] = include_bytes!("./message.write.template"); diff --git a/compiler/src/gen/rust/mod.rs b/compiler/src/gen/rust/mod.rs index 6d83d5e..f6f84f0 100644 --- a/compiler/src/gen/rust/mod.rs +++ b/compiler/src/gen/rust/mod.rs @@ -31,14 +31,13 @@ mod message; mod message_from_slice; mod message_offsets; mod message_write; +mod solver; pub mod structure; mod union; mod util; -mod solver; pub use solver::RustImportSolver; -use std::collections::HashSet; use crate::compiler::Protocol; use crate::gen::file::B; use crate::gen::rust::message::gen_message_decl; @@ -52,9 +51,10 @@ use crate::gen::{ file::{File, FileType}, Generator, }; +use bp3d_debug::trace; use bp3d_util::simple_error; +use std::collections::HashSet; use std::path::Path; -use bp3d_debug::trace; simple_error! { pub Error { @@ -69,7 +69,7 @@ pub struct Params<'a> { enable_union_set_discriminant: bool, enable_list_wrappers: bool, disable_read: HashSet<&'a str>, - disable_write: HashSet<&'a str> + disable_write: HashSet<&'a str>, } impl<'a> Params<'a> { diff --git a/compiler/src/gen/rust/solver.rs b/compiler/src/gen/rust/solver.rs index 4cb93df..44eafd3 100644 --- a/compiler/src/gen/rust/solver.rs +++ b/compiler/src/gen/rust/solver.rs @@ -26,8 +26,8 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use crate::compiler::Protocol; use crate::compiler::util::imports::ImportSolver; +use crate::compiler::Protocol; pub struct RustImportSolver; diff --git a/compiler/src/gen/rust/union.rs b/compiler/src/gen/rust/union.rs index e22cefd..4b025b0 100644 --- a/compiler/src/gen/rust/union.rs +++ b/compiler/src/gen/rust/union.rs @@ -27,6 +27,7 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::union::{DiscriminantField, Union}; +use crate::compiler::util::types::{Name, TypePathMap}; use crate::gen::base::map::{DefaultTypeMapper, TypePathMapper}; use crate::gen::base::union::{generate, Utilities}; use crate::gen::hook::TemplateHooks; @@ -34,7 +35,6 @@ use crate::gen::rust::util::RustUtils; use crate::gen::template::{Options, Template}; use crate::gen::RustParams; use itertools::Itertools; -use crate::compiler::util::types::{Name, TypePathMap}; const TEMPLATE: &[u8] = include_bytes!("./union.template"); diff --git a/compiler/src/gen/rust/util.rs b/compiler/src/gen/rust/util.rs index 0c0cefa..2fd6bc6 100644 --- a/compiler/src/gen/rust/util.rs +++ b/compiler/src/gen/rust/util.rs @@ -28,13 +28,13 @@ use crate::compiler::message::{Field, FieldType, Message}; use crate::compiler::structure::FixedFieldType; +use crate::compiler::util::types::TypeMapper; use crate::gen::base::map::TypePathMapper; use crate::gen::base::message::StringType; use crate::gen::template::Template; use crate::model::protocol::Endianness; use itertools::Itertools; use std::borrow::Cow; -use crate::compiler::util::types::TypeMapper; macro_rules! gen_value_type { ($prefix: literal, $ty: expr, $suffix: literal) => { diff --git a/compiler/src/gen/swift/imports.rs b/compiler/src/gen/swift/imports.rs index 510f90f..88a7477 100644 --- a/compiler/src/gen/swift/imports.rs +++ b/compiler/src/gen/swift/imports.rs @@ -26,16 +26,19 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use itertools::Itertools; use crate::compiler::util::imports::ProtocolStore; use crate::gen::template::Template; use crate::gen::SwiftImportSolver; +use itertools::Itertools; const TEMPLATE: &[u8] = include_bytes!("./imports.template"); pub fn gen_imports(solver: &ProtocolStore) -> String { let mut template = Template::compile(TEMPLATE).unwrap(); - let import_list = solver.iter().filter(|v| !v.package().is_empty()) - .map(|v| format!("import {};", v.package())).join("\n"); + let import_list = solver + .iter() + .filter(|v| !v.package().is_empty()) + .map(|v| format!("import {};", v.package())) + .join("\n"); template.var("import_list", import_list).render("", &["imports"]).unwrap() } diff --git a/compiler/src/gen/swift/mod.rs b/compiler/src/gen/swift/mod.rs index 6947094..b895e0f 100644 --- a/compiler/src/gen/swift/mod.rs +++ b/compiler/src/gen/swift/mod.rs @@ -55,10 +55,10 @@ simple_error! { } } +use crate::compiler::util::imports::ProtocolStore; use crate::gen::file::B; use crate::gen::swift::imports::gen_imports; pub use solver::SwiftImportSolver; -use crate::compiler::util::imports::ProtocolStore; pub struct GeneratorSwift; diff --git a/compiler/src/gen/swift/solver.rs b/compiler/src/gen/swift/solver.rs index c20325c..801b4a0 100644 --- a/compiler/src/gen/swift/solver.rs +++ b/compiler/src/gen/swift/solver.rs @@ -26,9 +26,9 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use crate::compiler::util::imports::ImportSolver; use crate::compiler::Protocol; use crate::gen::template::util::CaseConversion; -use crate::compiler::util::imports::ImportSolver; pub struct SwiftImportSolver; diff --git a/compiler/src/gen/swift/util.rs b/compiler/src/gen/swift/util.rs index 36053c7..74b0221 100644 --- a/compiler/src/gen/swift/util.rs +++ b/compiler/src/gen/swift/util.rs @@ -27,12 +27,12 @@ // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. use crate::compiler::structure::{Field, FixedFieldType}; +use crate::compiler::util::types::TypeMapper; use crate::compiler::Protocol; use crate::gen::base::message::StringType; use crate::gen::template::util::CaseConversion; use crate::model::protocol::Endianness; use std::borrow::Cow; -use crate::compiler::util::types::TypeMapper; pub struct SwiftTypeMapper<'a> { proto_name: Cow<'a, str>, diff --git a/compiler/src/gen/template/core.rs b/compiler/src/gen/template/core.rs index bcf20cb..f618b88 100644 --- a/compiler/src/gen/template/core.rs +++ b/compiler/src/gen/template/core.rs @@ -26,12 +26,12 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use crate::gen::template::options::Options; use crate::gen::template::parse_tree::{Component, Fragment, FragmentMode, Token}; use crate::gen::template::Error; use itertools::Itertools; use std::borrow::Cow; use std::collections::HashMap; -use crate::gen::template::options::Options; pub struct Template<'fragment, 'variable> { fragments: HashMap>, diff --git a/compiler/src/gen/template/mod.rs b/compiler/src/gen/template/mod.rs index 53d058c..0dd6048 100644 --- a/compiler/src/gen/template/mod.rs +++ b/compiler/src/gen/template/mod.rs @@ -29,9 +29,9 @@ mod core; mod error; mod functions; +mod options; mod parse_tree; pub mod util; -mod options; pub use core::Scope; pub use core::Template; diff --git a/compiler/src/gen/template/options.rs b/compiler/src/gen/template/options.rs index 2c042a9..d2b5800 100644 --- a/compiler/src/gen/template/options.rs +++ b/compiler/src/gen/template/options.rs @@ -26,19 +26,19 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use std::collections::HashSet; use crate::gen::template::functions::FunctionMap; +use std::collections::HashSet; pub struct Options<'a> { function_map: FunctionMap<'a>, - disabled_fragments: HashSet<&'a str> + disabled_fragments: HashSet<&'a str>, } impl<'a> Default for Options<'a> { fn default() -> Self { Self { function_map: FunctionMap::default(), - disabled_fragments: HashSet::new() + disabled_fragments: HashSet::new(), } } } @@ -47,7 +47,7 @@ impl<'a> Options<'a> { pub fn new(function_map: FunctionMap<'a>) -> Self { Self { function_map, - disabled_fragments: HashSet::new() + disabled_fragments: HashSet::new(), } } diff --git a/compiler/src/gen/template/parse_tree.rs b/compiler/src/gen/template/parse_tree.rs index 4bf7880..8cbcd74 100644 --- a/compiler/src/gen/template/parse_tree.rs +++ b/compiler/src/gen/template/parse_tree.rs @@ -26,9 +26,9 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +use crate::gen::template::functions::FunctionMap; use crate::gen::template::Error; use std::borrow::Cow; -use crate::gen::template::functions::FunctionMap; pub struct Variable<'a> { pub name: &'a str, diff --git a/compiler/src/model/message.rs b/compiler/src/model/message.rs index 73cb954..2374034 100644 --- a/compiler/src/model/message.rs +++ b/compiler/src/model/message.rs @@ -26,8 +26,8 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use serde::Deserialize; use crate::model::protocol::Description; +use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "snake_case")] @@ -59,7 +59,7 @@ pub struct MessageField { pub name: String, pub info: MessageFieldType, pub optional: Option, - pub description: Option + pub description: Option, } #[derive(Clone, Debug, Deserialize)] diff --git a/compiler/src/model/protocol.rs b/compiler/src/model/protocol.rs index 33719a3..a72e6cd 100644 --- a/compiler/src/model/protocol.rs +++ b/compiler/src/model/protocol.rs @@ -38,7 +38,7 @@ use std::fmt::{Display, Formatter}; #[serde(untagged)] pub enum Description { Single(String), - Multi(Vec) + Multi(Vec), } #[derive(Copy, Clone, Debug, PartialEq, Eq, Deserialize)] @@ -52,7 +52,7 @@ impl Display for Endianness { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Endianness::Little => f.write_str("little"), - Endianness::Big => f.write_str("big") + Endianness::Big => f.write_str("big"), } } } diff --git a/compiler/src/model/structure.rs b/compiler/src/model/structure.rs index 0adea84..41b3e97 100644 --- a/compiler/src/model/structure.rs +++ b/compiler/src/model/structure.rs @@ -26,8 +26,8 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use serde::Deserialize; use crate::model::protocol::Description; +use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] #[serde(rename_all = "kebab-case")] diff --git a/compiler/src/model/union.rs b/compiler/src/model/union.rs index 9543824..4ae490b 100644 --- a/compiler/src/model/union.rs +++ b/compiler/src/model/union.rs @@ -26,8 +26,8 @@ // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -use serde::Deserialize; use crate::model::protocol::Description; +use serde::Deserialize; #[derive(Clone, Debug, Deserialize)] pub struct UnionField {