From 990e97befc51981f28e850bd9fb0ba03c822d6f1 Mon Sep 17 00:00:00 2001 From: driftluo Date: Fri, 28 Jun 2024 19:12:51 +0800 Subject: [PATCH] feat: rust plugin rename rust keywords with trail underscore --- .github/workflows/ci.yaml | 14 +- README.md | 2 +- examples/ci-tests/Cargo.lock | 50 +-- examples/tests-utils-c/Cargo.toml | 6 +- examples/tests-utils-rust/Cargo.toml | 6 +- tests/Cargo.lock | 49 ++- tools/codegen/Cargo.toml | 4 +- tools/codegen/src/ast/verified/complete.rs | 9 + tools/codegen/src/compiler.rs | 6 +- tools/codegen/src/generator/languages/mod.rs | 2 +- .../src/generator/languages/rust/mod.rs | 2 +- .../src/generator/languages/rust/utilities.rs | 91 +++- .../languages/rust_lazy_reader/generator.rs | 48 +-- .../languages/rust_lazy_reader/mod.rs | 26 +- tools/codegen/src/generator/mod.rs | 1 + tools/codegen/src/utils.rs | 1 + tools/compiler/Cargo.lock | 390 ++++++++++++------ tools/compiler/Cargo.toml | 10 +- tools/compiler/src/cli/compiler-plugin.yaml | 7 - tools/compiler/src/cli/compiler.yaml | 24 -- tools/compiler/src/compiler.rs | 49 ++- tools/compiler/src/lib.rs | 23 +- 22 files changed, 529 insertions(+), 291 deletions(-) delete mode 100644 tools/compiler/src/cli/compiler-plugin.yaml delete mode 100644 tools/compiler/src/cli/compiler.yaml diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 0b3e84c..f40892d 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,9 +1,9 @@ name: CI on: push: - branches: [ master, develop, staging, trying ] + branches: [master, develop, staging, trying] pull_request: - branches: [ master ] + branches: [master] defaults: run: shell: bash @@ -38,19 +38,19 @@ jobs: run: make clippy test-msrv: name: Tests / Build (MSRV) - needs: [ rustfmt, clippy ] + needs: [rustfmt, clippy] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Setup uses: actions-rust-lang/setup-rust-toolchain@v1 with: - toolchain: 1.56.1 # MSRV + toolchain: 1.75.0 # MSRV - name: Run run: make ci-msrv test-crates: name: Tests / Build & Test - needs: [ rustfmt, clippy ] + needs: [rustfmt, clippy] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -62,7 +62,7 @@ jobs: run: make ci-crates test-examples: name: Tests / Run Examples - needs: [ rustfmt, clippy ] + needs: [rustfmt, clippy] runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -75,7 +75,7 @@ jobs: success: name: Success if: ${{ success() }} - needs: [ test-crates, test-examples ] + needs: [test-crates, test-examples] runs-on: ubuntu-latest steps: - name: Succeed diff --git a/README.md b/README.md index 65c0659..2eb60a3 100644 --- a/README.md +++ b/README.md @@ -83,7 +83,7 @@ Implementations in other languages are maintained by respective authors. ## Supported Rust Versions -The minimum supported version is 1.56.1. +The minimum supported version is 1.75.0. The current Molecule version is not guaranteed to build on Rust versions earlier than the minimum supported version. diff --git a/examples/ci-tests/Cargo.lock b/examples/ci-tests/Cargo.lock index a458a5c..d7793e1 100644 --- a/examples/ci-tests/Cargo.lock +++ b/examples/ci-tests/Cargo.lock @@ -106,6 +106,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "errno" version = "0.3.8" @@ -163,20 +169,26 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.12.3" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] name = "indexmap" -version = "1.9.3" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ - "autocfg", + "equivalent", "hashbrown", ] +[[package]] +name = "itoa" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" + [[package]] name = "lazy_static" version = "1.4.0" @@ -189,12 +201,6 @@ version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" -[[package]] -name = "linked-hash-map" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" - [[package]] name = "linux-raw-sys" version = "0.4.12" @@ -540,14 +546,15 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.8.26" +version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ "indexmap", + "itoa", "ryu", "serde", - "yaml-rust", + "unsafe-libyaml", ] [[package]] @@ -657,6 +664,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "version_check" version = "0.9.4" @@ -774,12 +787,3 @@ name = "windows_x86_64_msvc" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" - -[[package]] -name = "yaml-rust" -version = "0.4.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] diff --git a/examples/tests-utils-c/Cargo.toml b/examples/tests-utils-c/Cargo.toml index 13503f8..5d3c051 100644 --- a/examples/tests-utils-c/Cargo.toml +++ b/examples/tests-utils-c/Cargo.toml @@ -5,6 +5,6 @@ authors = ["Nervos Core Dev "] edition = "2018" [dependencies] -codegen = { package ="molecule-codegen", path = "../../tools/codegen" } -tests-loader = { package ="molecule-tests-loader", path = "../tests-loader" } -serde_yaml = "0.8.26" +codegen = { package = "molecule-codegen", path = "../../tools/codegen" } +tests-loader = { package = "molecule-tests-loader", path = "../tests-loader" } +serde_yaml = "0.9" diff --git a/examples/tests-utils-rust/Cargo.toml b/examples/tests-utils-rust/Cargo.toml index 106e782..aa4cd34 100644 --- a/examples/tests-utils-rust/Cargo.toml +++ b/examples/tests-utils-rust/Cargo.toml @@ -8,9 +8,9 @@ edition = "2018" proc-macro = true [dependencies] -codegen = { package ="molecule-codegen", path = "../../tools/codegen" } -tests-loader = { package ="molecule-tests-loader", path = "../tests-loader" } +codegen = { package = "molecule-codegen", path = "../../tools/codegen" } +tests-loader = { package = "molecule-tests-loader", path = "../tests-loader" } syn = "1.0.58" quote = "1.0.8" proc-macro2 = "1.0.24" -serde_yaml = "0.8.26" +serde_yaml = "0.9" diff --git a/tests/Cargo.lock b/tests/Cargo.lock index c854c24..61778d5 100644 --- a/tests/Cargo.lock +++ b/tests/Cargo.lock @@ -64,6 +64,12 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "equivalent" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + [[package]] name = "faster-hex" version = "0.6.1" @@ -86,6 +92,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "hashbrown" +version = "0.14.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" + [[package]] name = "indexmap" version = "1.9.3" @@ -93,7 +105,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" dependencies = [ "autocfg", - "hashbrown", + "hashbrown 0.12.3", +] + +[[package]] +name = "indexmap" +version = "2.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" +dependencies = [ + "equivalent", + "hashbrown 0.14.5", ] [[package]] @@ -155,7 +177,7 @@ dependencies = [ "semver", "serde", "serde_json", - "serde_yaml", + "serde_yaml 0.8.26", ] [[package]] @@ -173,7 +195,7 @@ dependencies = [ "semver", "serde", "serde_json", - "serde_yaml", + "serde_yaml 0.9.34+deprecated", ] [[package]] @@ -326,12 +348,25 @@ version = "0.8.26" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" dependencies = [ - "indexmap", + "indexmap 1.9.3", "ryu", "serde", "yaml-rust", ] +[[package]] +name = "serde_yaml" +version = "0.9.34+deprecated" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" +dependencies = [ + "indexmap 2.2.6", + "itoa", + "ryu", + "serde", + "unsafe-libyaml", +] + [[package]] name = "sha2" version = "0.10.8" @@ -412,6 +447,12 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + [[package]] name = "version_check" version = "0.9.4" diff --git a/tools/codegen/Cargo.toml b/tools/codegen/Cargo.toml index 989e800..056086a 100644 --- a/tools/codegen/Cargo.toml +++ b/tools/codegen/Cargo.toml @@ -11,7 +11,7 @@ categories = [ "parser-implementations", "development-tools::build-utils", "encoding", - "data-structures" + "data-structures", ] license = "MIT" @@ -27,7 +27,7 @@ same-file = "1.0.6" semver = "0.11.0" serde = { version = "1.0.195", features = ["derive", "rc"], optional = true } serde_json = { version = "1.0.111", optional = true } -serde_yaml = { version = "0.8.26", optional = true } +serde_yaml = { version = "0.9", optional = true } [dev-dependencies] tempfile = "=3.6.0" diff --git a/tools/codegen/src/ast/verified/complete.rs b/tools/codegen/src/ast/verified/complete.rs index a5d9efe..d7d5573 100644 --- a/tools/codegen/src/ast/verified/complete.rs +++ b/tools/codegen/src/ast/verified/complete.rs @@ -176,6 +176,15 @@ impl super::Ast { if decls_idx.insert(name, decl).is_some() || !decls_keys.insert(name) { panic!("the name `{}` is used more than once", name); }; + + let decls_keys_clone = decls_keys + .iter() + .cloned() + .map(str::to_lowercase) + .collect::>(); + if decls_keys_clone.len() != decls_keys.len() { + panic!("the name `{}` is used more than once, It seems that only the capitalization is inconsistent", name); + } } let mut decls_result = HashMap::new(); decls_result.insert( diff --git a/tools/codegen/src/compiler.rs b/tools/codegen/src/compiler.rs index 3eb262c..fdf88ef 100644 --- a/tools/codegen/src/compiler.rs +++ b/tools/codegen/src/compiler.rs @@ -102,17 +102,17 @@ impl Compiler { let output = output.as_mut().ok_or("output is not set")?; #[cfg(not(feature = "compiler-plugin"))] - let file_name; + let mut file_name = Default::default(); #[cfg(feature = "compiler-plugin")] let mut file_name = None; let ast = match input { Input::SchemaFile(ref file_path) => { - file_name = file_path + file_path .as_path() .file_name() .and_then(ffi::OsStr::to_str) - .to_owned(); + .clone_into(&mut file_name); parser::Parser::parse(file_path) } #[cfg(feature = "compiler-plugin")] diff --git a/tools/codegen/src/generator/languages/mod.rs b/tools/codegen/src/generator/languages/mod.rs index d44d9e5..875e313 100644 --- a/tools/codegen/src/generator/languages/mod.rs +++ b/tools/codegen/src/generator/languages/mod.rs @@ -3,7 +3,7 @@ use std::{convert::TryFrom, fmt, io}; use crate::ast; mod c; -mod rust; +pub(crate) mod rust; mod rust_lazy_reader; #[derive(Debug, Clone, Copy)] diff --git a/tools/codegen/src/generator/languages/rust/mod.rs b/tools/codegen/src/generator/languages/rust/mod.rs index 04d6177..593ef9c 100644 --- a/tools/codegen/src/generator/languages/rust/mod.rs +++ b/tools/codegen/src/generator/languages/rust/mod.rs @@ -4,7 +4,7 @@ use quote::quote; use crate::{ast, VERSION}; -mod utilities; +pub(crate) mod utilities; mod builder; mod entity; diff --git a/tools/codegen/src/generator/languages/rust/utilities.rs b/tools/codegen/src/generator/languages/rust/utilities.rs index 70ba7ee..13b98c5 100644 --- a/tools/codegen/src/generator/languages/rust/utilities.rs +++ b/tools/codegen/src/generator/languages/rust/utilities.rs @@ -1,15 +1,96 @@ use case::CaseExt; use proc_macro2 as m4; +use std::{collections::HashSet, sync::OnceLock}; + +static RUST_KEYWORDS: OnceLock> = OnceLock::new(); + +fn rust_keyword() -> &'static HashSet<&'static str> { + RUST_KEYWORDS.get_or_init(|| { + IntoIterator::into_iter([ + // strick + "as", + "struct", + "break", + "const", + "continue", + "crate", + "else", + "enum", + "extern", + "false", + "fn", + "for", + "if", + "impl", + "in", + "let", + "loop", + "match", + "mod", + "move", + "mut", + "pub", + "ref", + "return", + "self", + "Self", + "static", + "super", + "trait", + "true", + "type", + "unsafe", + "use", + "where", + "while", + // 2018+ + "async", + "await", + "dyn", + // reserved + "abstract", + "become", + "box", + "do", + "final", + "macro", + "override", + "priv", + "typeof", + "unsized", + "virtual", + "yield", + // 2018+ + "try", + // weak + "macro_rules", + "union", + "'static", + ]) + .collect() + }) +} + +fn sanitize_identifier(name: &str) -> String { + let keywords = rust_keyword(); + let snake_name = name.to_snake(); + if keywords.contains(snake_name.as_str()) { + snake_name + "_" + } else { + snake_name + } +} + pub(super) fn usize_lit(num: usize) -> m4::Literal { m4::Literal::usize_unsuffixed(num) } -pub(super) fn ident_new(ident: &str) -> m4::Ident { +pub(crate) fn ident_new(ident: &str) -> m4::Ident { m4::Ident::new(ident, m4::Span::call_site()) } -pub(super) fn ident_name(name: &str, suffix: &str) -> m4::Ident { +pub(crate) fn ident_name(name: &str, suffix: &str) -> m4::Ident { let span = m4::Span::call_site(); m4::Ident::new(&format!("{}{}", name, suffix).to_camel(), span) } @@ -38,14 +119,14 @@ pub(super) fn builder_name(name: &str) -> m4::Ident { ident_name(name, "Builder") } -pub(super) fn field_name(name: &str) -> m4::Ident { +pub(crate) fn field_name(name: &str) -> m4::Ident { let span = m4::Span::call_site(); - m4::Ident::new(&name.to_snake(), span) + m4::Ident::new(&sanitize_identifier(name), span) } pub(super) fn func_name(name: &str) -> m4::Ident { let span = m4::Span::call_site(); - m4::Ident::new(&name.to_snake(), span) + m4::Ident::new(&sanitize_identifier(name), span) } pub(super) fn entity_iterator_name(name: &str) -> m4::Ident { diff --git a/tools/codegen/src/generator/languages/rust_lazy_reader/generator.rs b/tools/codegen/src/generator/languages/rust_lazy_reader/generator.rs index 7940c7b..dd17732 100644 --- a/tools/codegen/src/generator/languages/rust_lazy_reader/generator.rs +++ b/tools/codegen/src/generator/languages/rust_lazy_reader/generator.rs @@ -1,12 +1,13 @@ -use super::{ident_new, ident_new_camel, LazyReaderGenerator}; +use super::LazyReaderGenerator; use crate::ast::{self, HasName, *}; +use crate::generator::{field_name, ident_name, ident_new}; use proc_macro2::{Literal, TokenStream}; use quote::quote; use std::io; impl ast::Union { fn get_item_name(typ: &TopDecl) -> TokenStream { - let item_name = ident_new_camel(typ.name()); + let item_name = ident_name(typ.name(), ""); let item_type_name = Self::get_type_name(typ); quote! { @@ -41,12 +42,12 @@ impl ast::Union { if let TopDecl::Primitive(_) = v.item().typ().as_ref() { quote!(Cursor) } else { - let name = ident_new_camel(typ.name()); + let name = ident_name(typ.name(), ""); quote!(#name) } } _ => { - let name = ident_new_camel(typ.name()); + let name = ident_name(typ.name(), ""); quote!(#name) } } @@ -55,7 +56,7 @@ impl ast::Union { impl LazyReaderGenerator for ast::Union { fn gen_rust(&self, output: &mut W) -> io::Result<()> { - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); // generate enum: let q = self.items().iter().map(|item| { @@ -78,7 +79,7 @@ impl LazyReaderGenerator for ast::Union { let q = self.items().iter().map(|item| { let item_id = item.id(); let item_name_str = item.typ().name(); - let item_name = ident_new_camel(item_name_str); + let item_name = ident_name(item_name_str, ""); let item_type = Self::get_type_name(item.typ()); let q = match item.typ().as_ref() { @@ -95,7 +96,7 @@ impl LazyReaderGenerator for ast::Union { } }, TopDecl::Option_(o) => { - let item_name = ident_new_camel(o.item().typ().name()); + let item_name = ident_name(o.item().typ().name(), ""); quote! {{ if cur.option_is_none() { None @@ -158,7 +159,7 @@ impl LazyReaderGenerator for ast::Union { // generate verify let verify_items = self.items().iter().enumerate().map(|(_i, item)| { - let item_name = ident_new_camel(item.typ().name()); + let item_name = ident_name(item.typ().name(), ""); match item.typ().as_ref() { TopDecl::Primitive(_) => { quote!( Self::#item_name(_v) => Ok(()), ) @@ -223,7 +224,7 @@ impl LazyReaderGenerator for ast::Array { None, )?; let total_size = self.item_count() * self.item_size(); - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let verify_sub = { let func = verify_typ(self.item().typ().as_ref(), quote!(self.get(i)?)); @@ -259,7 +260,7 @@ impl LazyReaderGenerator for ast::Option_ {} impl LazyReaderGenerator for ast::Struct { fn gen_rust(&self, output: &mut W) -> io::Result<()> { generate_rust_common_table(output, self.name(), self.fields(), Some(self.field_sizes()))?; - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let total_size: usize = self.field_sizes().iter().sum(); let verify_fields = self @@ -296,7 +297,7 @@ impl LazyReaderGenerator for ast::FixVec { None, )?; - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let item_size = self.item_size(); let q = quote! { @@ -341,7 +342,7 @@ impl LazyReaderGenerator for ast::DynVec { quote!(compatible) }; - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let q = quote! { impl #name { pub fn verify(&self, #val_compatible: bool) -> Result<(), Error> { @@ -360,7 +361,7 @@ impl LazyReaderGenerator for ast::Table { fn gen_rust(&self, output: &mut W) -> io::Result<()> { generate_rust_common_table(output, self.name(), self.fields(), None)?; let field_count = self.fields().len(); - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let verify_fields = self .fields() @@ -391,7 +392,7 @@ fn generate_rust_common_array( fixvec: Option<&ast::FixVec>, _dynvec: Option<&ast::DynVec>, ) -> io::Result<()> { - let name = ident_new(plain_name); + let name = ident_name(plain_name, ""); let q = quote! { #[derive(Clone)] pub struct #name { @@ -453,9 +454,9 @@ fn generate_rust_common_array_impl( quote! { dynvec_slice_by_index(index) } }; let convert_code = tc.gen_convert_code(); - let name = ident_new(plain_name); - let iterator_name = ident_new(&format!("{}Iterator", name)); - let iterator_ref_name = ident_new(&format!("{}IteratorRef", name)); + let name = ident_name(plain_name, ""); + let iterator_name = ident_name(plain_name, "Iterator"); + let iterator_ref_name = ident_name(plain_name, "IteratorRef"); let q = quote! { impl #name { pub fn get(&self, index: usize) -> Result<#item_name, Error> { @@ -537,7 +538,7 @@ fn generate_rust_common_table( fields: &[FieldDecl], field_sizes: Option<&[usize]>, ) -> io::Result<()> { - let name = ident_new(plain_name); + let name = ident_name(plain_name, ""); let q = quote! { #[derive(Clone)] pub struct #name { @@ -564,12 +565,11 @@ fn generate_rust_common_table_impl( index: usize, field_sizes: Option<&[usize]>, ) -> io::Result<()> { - let field_name = field.name(); let (transformed_name, tc) = get_rust_type_category(field.typ()); let slice_by = generate_rust_slice_by(index, &field_sizes); let convert_code = tc.gen_convert_code(); - let name = ident_new(plain_name); - let field_name = ident_new(field_name); + let name = ident_name(plain_name, ""); + let field_name = field_name(field.name()); let q = quote! { impl #name { pub fn #field_name(&self) -> Result<#transformed_name, Error> { @@ -687,7 +687,7 @@ impl TypeCategory { fn get_rust_type_category(typ: &TopDecl) -> (TokenStream, TypeCategory) { let name = typ.name(); let mut tc = TypeCategory::Primitive; - let token_name = ident_new(name); + let token_name = ident_name(name, ""); let mut transformed_name = quote! { #token_name }; match typ { // if the field type is array and the field type name is "uint8", "int8" ... @@ -774,7 +774,7 @@ fn generate_rust_slice_by(index: usize, fields_sizes: &Option<&[usize]>) -> Toke } fn verify_typ(typ: &TopDecl, q_val: TokenStream) -> TokenStream { - let type_name = ident_new(typ.name()); + let type_name = ident_name(typ.name(), ""); match typ { TopDecl::Primitive(_) => { quote!() @@ -841,7 +841,7 @@ fn verify_typ(typ: &TopDecl, q_val: TokenStream) -> TokenStream { } fn verify_filed(f: &FieldDecl) -> TokenStream { - let field = ident_new(f.name()); + let field = field_name(f.name()); let typ = f.typ(); verify_typ(typ.as_ref(), quote!(self.#field()?)) } diff --git a/tools/codegen/src/generator/languages/rust_lazy_reader/mod.rs b/tools/codegen/src/generator/languages/rust_lazy_reader/mod.rs index 9e387b9..85dfbf7 100644 --- a/tools/codegen/src/generator/languages/rust_lazy_reader/mod.rs +++ b/tools/codegen/src/generator/languages/rust_lazy_reader/mod.rs @@ -1,6 +1,6 @@ use crate::ast; use crate::ast::HasName; -use proc_macro2::Ident; +use crate::generator::ident_name; use quote::quote; use std::io; @@ -39,31 +39,9 @@ impl super::LanguageGenerator for Generator { } } -fn ident_new(name: &str) -> Ident { - Ident::new(name, proc_macro2::Span::call_site()) -} - -fn ident_new_camel(name: &str) -> Ident { - let mut camel_case = String::new(); - let mut capitalize_next = true; - - for c in name.chars() { - if c == '_' { - capitalize_next = true; - } else if capitalize_next { - camel_case.push(c.to_ascii_uppercase()); - capitalize_next = false; - } else { - camel_case.push(c); - } - } - - ident_new(&camel_case) -} - pub trait LazyReaderGenerator: HasName { fn gen_rust(&self, output: &mut W) -> io::Result<()> { - let name = ident_new(self.name()); + let name = ident_name(self.name(), ""); let q = quote! { pub struct #name { pub cursor : Cursor } diff --git a/tools/codegen/src/generator/mod.rs b/tools/codegen/src/generator/mod.rs index aef206f..802e292 100644 --- a/tools/codegen/src/generator/mod.rs +++ b/tools/codegen/src/generator/mod.rs @@ -7,6 +7,7 @@ use crate::ir::{self, ToIntermediate as _}; mod languages; +pub(crate) use languages::rust::utilities::{field_name, ident_name, ident_new}; pub use languages::Language; #[derive(Debug, Clone, Copy)] diff --git a/tools/codegen/src/utils.rs b/tools/codegen/src/utils.rs index 067f3b9..134c635 100644 --- a/tools/codegen/src/utils.rs +++ b/tools/codegen/src/utils.rs @@ -8,6 +8,7 @@ pub(crate) trait PairsUtils { fn next_string(&mut self) -> String; fn next_usize(&mut self) -> usize; fn next_item(&mut self) -> ast::ItemDecl; + #[allow(dead_code)] fn next_items(&mut self) -> Vec; fn next_custom_union_items(&mut self) -> Vec; fn next_fields(&mut self) -> Vec; diff --git a/tools/compiler/Cargo.lock b/tools/compiler/Cargo.lock index 077c4c6..67922ad 100644 --- a/tools/compiler/Cargo.lock +++ b/tools/compiler/Cargo.lock @@ -1,29 +1,61 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 [[package]] -name = "atty" -version = "0.2.14" +name = "anstream" +version = "0.6.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" dependencies = [ - "hermit-abi", - "libc", - "winapi", + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", ] [[package]] -name = "autocfg" +name = "anstyle-query" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys", +] [[package]] name = "bitflags" -version = "1.3.2" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" +checksum = "b048fb63fd8b5923fc5aa7b340d8e156aec7ec02f0c78fa8a6ddc2613f6f71de" [[package]] name = "block-buffer" @@ -36,9 +68,9 @@ dependencies = [ [[package]] name = "bytes" -version = "1.5.0" +version = "1.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2bd12c1caf447e69cd4528f47f94d203fd2582878ecb9e9465484c4148a8223" +checksum = "514de17de45fdb8dc022b1a7975556c53c86f9f0aa5f534b98977b171857c2c9" [[package]] name = "case" @@ -54,29 +86,36 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "clap" -version = "3.2.4" +version = "4.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6d20de3739b4fb45a17837824f40aa1769cc7655d7a83e68739a77fe7b30c87a" +checksum = "84b3edb18336f4df585bc9aa31dd99c036dfa5dc5e9a2939a722a188f3a8970d" dependencies = [ - "atty", - "bitflags", + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1c09dd5ada6c6c78075d6fd0da3f90d8080651e2d6cc8eb2f1aaa4034ced708" +dependencies = [ + "anstream", + "anstyle", "clap_lex", - "indexmap", - "once_cell", "strsim", - "termcolor", - "textwrap", - "yaml-rust", ] [[package]] name = "clap_lex" -version = "0.2.2" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5538cd660450ebeb4234cfecf8f2284b844ffc4c50531e66d584ad5b91293613" -dependencies = [ - "os_str_bytes", -] +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" [[package]] name = "cpufeatures" @@ -109,9 +148,25 @@ dependencies = [ [[package]] name = "either" -version = "1.6.1" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" + +[[package]] +name = "equivalent" +version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" +checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys", +] [[package]] name = "generic-array" @@ -125,46 +180,58 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.11.2" +version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" +checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" [[package]] -name = "hermit-abi" -version = "0.1.19" +name = "home" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +checksum = "e3d1354bf6b7235cb4a0576c2619fd4ed18183f689b12b006a0ee7329eeff9a5" dependencies = [ - "libc", + "windows-sys", ] [[package]] name = "indexmap" -version = "1.8.2" +version = "2.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6012d540c5baa3589337a98ce73408de9b5a25ec9fc2c6fd6be8f0d39e0ca5a" +checksum = "168fb715dda47215e360912c096649d23d58bf392ac62f73919e831745e40f26" dependencies = [ - "autocfg", + "equivalent", "hashbrown", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + [[package]] name = "itoa" -version = "1.0.2" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d" +checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.155" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" [[package]] -name = "linked-hash-map" -version = "0.5.4" +name = "linux-raw-sys" +version = "0.4.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "molecule" @@ -203,31 +270,26 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.12.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7709cef83f0c1f58f666e746a08b21e0085f7440fa6a29cc194d68aac97a4225" - -[[package]] -name = "os_str_bytes" -version = "6.1.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21326818e99cfe6ce1e524c2a805c189a99b5ae555a35d19f9a284b427d86afa" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" [[package]] name = "pest" -version = "2.5.7" +version = "2.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7b1403e8401ad5dedea73c626b99758535b342502f8d1e361f4a2dd952749122" +checksum = "cd53dff83f26735fdc1ca837098ccf133605d794cdae66acfc2bfac3ec809d95" dependencies = [ + "memchr", "thiserror", "ucd-trie", ] [[package]] name = "pest_derive" -version = "2.5.7" +version = "2.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "be99c4c1d2fc2769b1d00239431d711d08f6efedcecb8b6e30707160aee99c15" +checksum = "26293c9193fbca7b1a3bf9b79dc1e388e927e6cacaa78b4a3ab705a1d3d41459" dependencies = [ "pest", "pest_generator", @@ -235,22 +297,22 @@ dependencies = [ [[package]] name = "pest_generator" -version = "2.5.7" +version = "2.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e56094789873daa36164de2e822b3888c6ae4b4f9da555a1103587658c805b1e" +checksum = "3ec22af7d3fb470a85dd2ca96b7c577a1eb4ef6f1683a9fe9a8c16e136c04687" dependencies = [ "pest", "pest_meta", "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.68", ] [[package]] name = "pest_meta" -version = "2.5.7" +version = "2.7.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6733073c7cff3d8459fda0e42f13a047870242aed8b509fe98000928975f359e" +checksum = "d7a240022f37c361ec1878d646fc5b7d7c4d28d5946e1a80ad5a7a4f4ca0bdcd" dependencies = [ "once_cell", "pest", @@ -259,9 +321,9 @@ dependencies = [ [[package]] name = "proc-macro2" -version = "1.0.76" +version = "1.0.86" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" dependencies = [ "unicode-ident", ] @@ -274,23 +336,36 @@ checksum = "889bff6ebe76dbef2685598944997eb7f15c9854a2e34db853a51c6ac158b9d5" dependencies = [ "proc-macro2", "quote", - "syn 1.0.96", + "syn 1.0.109", ] [[package]] name = "quote" -version = "1.0.35" +version = "1.0.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" dependencies = [ "proc-macro2", ] +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "ryu" -version = "1.0.10" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695" +checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" [[package]] name = "same-file" @@ -321,29 +396,29 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.195" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "7253ab4de971e72fb7be983802300c30b5a7f0c2e56fab8abfc6a214307c0094" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.203" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "500cbc0ebeb6f46627f50f3f5811ccf6bf00643be300b4c3eabc0ef55dc5b5ba" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.68", ] [[package]] name = "serde_json" -version = "1.0.111" +version = "1.0.118" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "176e46fa42316f18edd598015a5166857fc835ec732f5215eac6b7bdbf0a84f4" +checksum = "d947f6b3163d8857ea16c4fa0dd4840d52f3041039a85decd46867eb1abef2e4" dependencies = [ "itoa", "ryu", @@ -352,14 +427,15 @@ dependencies = [ [[package]] name = "serde_yaml" -version = "0.8.26" +version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "578a7433b776b56a35785ed5ce9a7e777ac0598aac5a6dd1b4b18a307c7fc71b" +checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ "indexmap", + "itoa", "ryu", "serde", - "yaml-rust", + "unsafe-libyaml", ] [[package]] @@ -375,15 +451,15 @@ dependencies = [ [[package]] name = "strsim" -version = "0.10.0" +version = "0.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" [[package]] name = "syn" -version = "1.0.96" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0748dd251e24453cb8717f0354206b91557e4ec8703673a4b30208f2abaf1ebf" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", @@ -392,55 +468,40 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.48" +version = "2.0.68" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" +checksum = "901fa70d88b9d6c98022e23b4136f9f3e54e4662c3bc1bd1d84a42a9a0f0c1e9" dependencies = [ "proc-macro2", "quote", "unicode-ident", ] -[[package]] -name = "termcolor" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755" -dependencies = [ - "winapi-util", -] - -[[package]] -name = "textwrap" -version = "0.15.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb" - [[package]] name = "thiserror" -version = "1.0.56" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +checksum = "c546c80d6be4bc6a00c0f01730c08df82eaa7a7a61f11d656526506112cc1709" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.56" +version = "1.0.61" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +checksum = "46c3384250002a6d5af4d114f2845d37b57521033f30d5c3f46c4d70e1197533" dependencies = [ "proc-macro2", "quote", - "syn 2.0.48", + "syn 2.0.68", ] [[package]] name = "typenum" -version = "1.15.0" +version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987" +checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" [[package]] name = "ucd-trie" @@ -450,9 +511,21 @@ checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" [[package]] name = "unicode-ident" -version = "1.0.1" +version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "unsafe-libyaml" +version = "0.2.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "673aac59facbab8a9007c7f6108d11f63b603f7cabff99fabf650fea5c32b861" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" [[package]] name = "version_check" @@ -462,51 +535,100 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "which" -version = "4.4.0" +version = "6.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2441c784c52b289a054b7201fc93253e288f094e2f4be9058343127c4226a269" +checksum = "8211e4f58a2b2805adfbefbc07bab82958fc91e3836339b1ab7ae32465dce0d7" dependencies = [ "either", - "libc", - "once_cell", + "home", + "rustix", + "winsafe", ] [[package]] -name = "winapi" -version = "0.3.9" +name = "winapi-util" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b" dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", + "windows-sys", ] [[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" +name = "windows-sys" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] [[package]] -name = "winapi-util" -version = "0.1.5" +name = "windows-targets" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" dependencies = [ - "winapi", + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", ] [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "windows_aarch64_gnullvm" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" [[package]] -name = "yaml-rust" -version = "0.4.5" +name = "windows_aarch64_msvc" +version = "0.52.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" -dependencies = [ - "linked-hash-map", -] +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" + +[[package]] +name = "winsafe" +version = "0.0.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d135d17ab770252ad95e9a872d365cf3090e3be864a34ab46f48555993efc904" diff --git a/tools/compiler/Cargo.toml b/tools/compiler/Cargo.toml index 24e4532..dd430c1 100644 --- a/tools/compiler/Cargo.toml +++ b/tools/compiler/Cargo.toml @@ -11,7 +11,7 @@ categories = [ "parser-implementations", "development-tools", "encoding", - "data-structures" + "data-structures", ] license = "MIT" @@ -32,9 +32,11 @@ name = "moleculec-rust-lazy-reader" path = "src/compiler-rust-lazy-reader.rs" [dependencies] -clap = { version = "3", features = ["yaml", "cargo"] } -which = "4.3.0" -molecule-codegen = { version = "=0.9.0", path = "../codegen", features = ["compiler-plugin"] } +clap = { version = "4", features = ["cargo", "string"] } +which = "6" +molecule-codegen = { version = "=0.9.0", path = "../codegen", features = [ + "compiler-plugin", +] } [badges] maintenance = { status = "experimental" } diff --git a/tools/compiler/src/cli/compiler-plugin.yaml b/tools/compiler/src/cli/compiler-plugin.yaml deleted file mode 100644 index b09846d..0000000 --- a/tools/compiler/src/cli/compiler-plugin.yaml +++ /dev/null @@ -1,7 +0,0 @@ -name: Moleculec Plugin -author: Nervos Core Dev -args: - - format: - help: Output the supported format for the intermediate data. - long: format - takes_value: false diff --git a/tools/compiler/src/cli/compiler.yaml b/tools/compiler/src/cli/compiler.yaml deleted file mode 100644 index 0b02053..0000000 --- a/tools/compiler/src/cli/compiler.yaml +++ /dev/null @@ -1,24 +0,0 @@ -name: Moleculec -about: Schema compiler for molecule. -author: Nervos Core Dev -args: - - schema-file: - help: Provide a schema file to compile. - long: schema-file - takes_value: true - required: true - - language: - help: | - Specify a language, then generate source code for the specified language and output the generated code to the stdout. - This parameter actually specifies a plugin to use. It should be a simple word, and the compiler will search for a plugin called "moleculec-" in `$PATH`. - If "" is "-", the compiler will dump the intermediate data of schema to standard output. - long: language - takes_value: true - required: true - - format: - help: If "" is "-", this parameter is used to specify a format for the intermediate data. - long: format - takes_value: true - possible_values: - - json - - yaml diff --git a/tools/compiler/src/compiler.rs b/tools/compiler/src/compiler.rs index 0a0f26d..3b0b23d 100644 --- a/tools/compiler/src/compiler.rs +++ b/tools/compiler/src/compiler.rs @@ -1,6 +1,3 @@ -#[macro_use] -extern crate clap; - use std::{convert::TryFrom, process, str}; use molecule_codegen::{Compiler, IntermediateFormat}; @@ -21,17 +18,45 @@ pub(crate) mod config { } pub(crate) fn build_commandline() -> AppConfig { - let yaml = clap::load_yaml!("cli/compiler.yaml"); - let matches = clap::App::from_yaml(yaml) + let matches = clap::Command::new("moleculec") + .name("Moleculec") + .about("Schema compiler for molecule.") + .author("Nervos Core Dev ") .version(clap::crate_version!()) + .arg( + clap::Arg::new("schema-file") + .long("schema-file") + .help("Provide a schema file to compile.") + .required(true) + .action(clap::ArgAction::Set), + ) + .arg( + clap::Arg::new("language") + .long("language") + .help("Specify a language, then generate source code for the specified language and output the generated code to the stdout.\ + \nThis parameter actually specifies a plugin to use. It should be a simple word, and the compiler will search for a plugin called \"moleculec-\" in `$PATH`.\ + \nIf \"\" is \"-\", the compiler will dump the intermediate data of schema to standard output.") + .required(true) + .action(clap::ArgAction::Set), + ) + .arg( + clap::Arg::new("format") + .long("format") + .help(r#"If "" is "-", this parameter is used to specify a format for the intermediate data."#) + .value_parser(["json", "yaml"]) + .action(clap::ArgAction::Set), + ) .get_matches(); - AppConfig::from(&matches) + AppConfig::from(matches) } - impl<'a> From<&'a clap::ArgMatches> for AppConfig { - fn from(matches: &'a clap::ArgMatches) -> Self { - let schema_file = value_t_or_exit!(matches, "schema-file", PathBuf); - let language = value_t_or_exit!(matches, "language", String); + impl From for AppConfig { + fn from(matches: clap::ArgMatches) -> Self { + let schema_file = { + let path = matches.get_one::("schema-file").unwrap(); + PathBuf::from(path) + }; + let language = matches.get_one::("language").unwrap(); if !schema_file.as_path().is_file() { eprintln!( "Error: schema-file [{}] should be a file", @@ -40,7 +65,7 @@ pub(crate) mod config { process::exit(1); } let output_config = if language == "-" { - let format = value_t!(matches, "format", String).unwrap_or_else(|_| { + let format = matches.get_one::("format").unwrap_or_else(|| { eprintln!("Error: since language is \"-\", a format is required"); process::exit(1); }); @@ -52,7 +77,7 @@ pub(crate) mod config { } } } else { - if value_t!(matches, "format", String).is_ok() { + if matches.get_one::("format").is_some() { eprintln!("Error: since language is not \"-\", don't specify format"); process::exit(1); } diff --git a/tools/compiler/src/lib.rs b/tools/compiler/src/lib.rs index 93e8e26..bd99b28 100644 --- a/tools/compiler/src/lib.rs +++ b/tools/compiler/src/lib.rs @@ -16,22 +16,27 @@ pub struct AppConfig { format: IntermediateFormat, } -type RawAppConfig<'a> = (Language, IntermediateFormat, &'a clap::ArgMatches); +type RawAppConfig = (Language, IntermediateFormat, clap::ArgMatches); pub fn build_commandline(lang: Language, format: IntermediateFormat) -> AppConfig { - let yaml = clap::load_yaml!("cli/compiler-plugin.yaml"); - let matches = clap::App::from_yaml(yaml) + let matches = clap::Command::new("moleculec-plugin") .name(format!("Moleculec {} Plugin", lang)) .about("Compiler plugin for molecule to generate code.") .version(clap::crate_version!()) + .arg( + clap::Arg::new("format") + .long("format") + .help("Output the supported format for the intermediate data.") + .action(clap::ArgAction::SetTrue), + ) .get_matches(); - AppConfig::from(&(lang, format, &matches)) + AppConfig::from((lang, format, matches)) } -impl<'a> From<&'a RawAppConfig<'a>> for AppConfig { - fn from(input: &'a RawAppConfig<'a>) -> Self { +impl From for AppConfig { + fn from(input: RawAppConfig) -> Self { let (lang, format, matches) = input; - let action = if matches.is_present("format") { + let action = if matches.get_flag("format") { AppAction::DisplayFormat } else { let mut input = Vec::new(); @@ -43,8 +48,8 @@ impl<'a> From<&'a RawAppConfig<'a>> for AppConfig { }; Self { action, - lang: *lang, - format: *format, + lang, + format, } } }