Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Yuri6037 committed Sep 27, 2024
2 parents 6a9e4c7 + 17d0f14 commit a6bf7a0
Show file tree
Hide file tree
Showing 49 changed files with 319 additions and 202 deletions.
11 changes: 6 additions & 5 deletions auto-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -53,14 +53,15 @@ pub struct Args {
pub output: Option<PathBuf>,
/// Name of the code generator to use.
#[clap(short = 'g', long = "generator", default_value = "rust")]
pub generator: Generator
pub generator: Generator,
}

fn main() {
let args = Args::parse();
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);
}
6 changes: 3 additions & 3 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
};
}
}
Expand Down
10 changes: 5 additions & 5 deletions cli/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand All @@ -59,7 +59,7 @@ impl<'a, I: ImportSolver, G: bp3d_protoc::gen::Generator> Builder<'a, I, G> {
Self {
context,
generator,
params
params,
}
}
}
18 changes: 12 additions & 6 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/api/build_script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
15 changes: 8 additions & 7 deletions compiler/src/api/config/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Config<'a, T>, toml::de::Error> {
toml::from_str(data)
Expand All @@ -55,15 +55,16 @@ pub fn generate<'a, G: crate::gen::Generator, T, I: ImportSolver, F: Fn(&T) -> O
context: &mut Context<'a>,
config: &Config<T>,
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<str> = if config.package.name.is_empty() {
Cow::Borrowed(protocol)
} 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;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/api/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 10 additions & 10 deletions compiler/src/api/config/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 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<Vec<&'a str>>,
pub disable_write: Option<Vec<&'a str>>,
pub write_async: Option<bool>,
pub union_set_discriminant: Option<bool>,
pub list_wrappers: Option<bool>,
pub struct_to_mut: Option<bool>
pub struct_to_mut: Option<bool>,
}

#[derive(Deserialize)]
#[serde(rename_all="kebab-case")]
#[serde(rename_all = "kebab-case")]
pub struct Params<T> {
pub write_messages: Option<bool>,
pub read_messages: Option<bool>,
Expand All @@ -52,29 +52,29 @@ pub struct Params<T> {
pub use_messages: Option<bool>,
pub use_unions: Option<bool>,
#[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,
pub file_header: Option<&'a Path>,
}

#[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<HashMap<&'a str, Params<T>>>,
pub dependency: Option<Vec<Dependency<'a>>>
pub dependency: Option<Vec<Dependency<'a>>>,
}
72 changes: 48 additions & 24 deletions compiler/src/api/core/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Item<'a>>
items: IndexMap<Item<'a>>,
}

impl<'a> Context<'a> {
fn new(size: usize) -> Self {
Self {
items: IndexMap::with_capacity(size)
items: IndexMap::with_capacity(size),
}
}

Expand All @@ -55,15 +55,15 @@ impl<'a> Context<'a> {
self.items.get(full_name)
}

pub fn iter(&self) -> impl Iterator<Item=&Item<'a>> {
pub fn iter(&self) -> impl Iterator<Item = &Item<'a>> {
self.items.iter()
}
}

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> {
Expand All @@ -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 {
Expand Down Expand Up @@ -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> {
Expand All @@ -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<Item<'b>, Error> {
fn generate_internal<'b>(
&self,
protocol: &'b compiler::Protocol,
params: &Params,
generator_params: &G::Params<'_>,
) -> Result<Item<'b>, 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)?
Expand All @@ -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),
Expand All @@ -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<str>, params: &Params, generator_params: &G::Params<'_>) -> Result<(), Error> {
pub fn generate<'b>(
&'b self,
context: &mut Context<'b>,
full_name: impl AsRef<str>,
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)?);
Expand Down
Loading

0 comments on commit a6bf7a0

Please sign in to comment.