From ba9091181ca93c8e94cf6638d5d23705f725c14a Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Sat, 14 Oct 2023 18:29:30 +0900 Subject: [PATCH] fix: compile more schemas and fix errors --- crates/stef-build/src/decode.rs | 30 +++--- crates/stef-build/src/definition.rs | 62 +++++++---- crates/stef-build/src/encode.rs | 6 +- crates/stef-build/src/lib.rs | 5 +- .../compiler__compile@alias-basic.stef.snap | 2 + ...ompiler__compile@attribute-multi.stef.snap | 7 +- ...mpiler__compile@attribute-single.stef.snap | 7 +- ...compiler__compile@attribute-unit.stef.snap | 7 +- ...piler__compile@attributes-min-ws.stef.snap | 7 +- .../compiler__compile@attributes.stef.snap | 7 +- .../compiler__compile@const-basic.stef.snap | 2 + .../compiler__compile@const-string.stef.snap | 2 + .../compiler__compile@enum-basic.stef.snap | 4 +- .../compiler__compile@enum-generics.stef.snap | 4 +- .../compiler__compile@enum-many-ws.stef.snap | 4 +- .../compiler__compile@enum-min-ws.stef.snap | 4 +- .../compiler__compile@import-basic.stef.snap | 2 + .../compiler__compile@module-basic.stef.snap | 13 ++- .../compiler__compile@schema-basic.stef.snap | 23 ++-- .../compiler__compile@struct-basic.stef.snap | 7 +- ...ompiler__compile@struct-generics.stef.snap | 7 +- ...compiler__compile@struct-many-ws.stef.snap | 7 +- .../compiler__compile@struct-min-ws.stef.snap | 7 +- .../compiler__compile@struct-tuple.stef.snap | 7 +- .../compiler__compile@types-basic.stef.snap | 11 +- .../compiler__compile@types-generic.stef.snap | 25 +++-- .../compiler__compile@types-nested.stef.snap | 21 +++- .../compiler__compile@types-ref.stef.snap | 95 ++++++++++++++++- .../tests/inputs/schema-basic.stef | 4 +- .../stef-parser/tests/inputs/types-ref.stef | 9 ++ .../parser__parse@schema-basic.stef.snap | 4 +- .../parser__parse@types-ref.stef.snap | 79 ++++++++++++++ .../parser__print@schema-basic.stef.snap | 4 +- .../parser__print@types-ref.stef.snap | 9 ++ crates/stef-playground/build.rs | 1 + crates/stef-playground/schemas | 1 + crates/stef-playground/src/lib.rs | 100 ++++++++++++++++++ 37 files changed, 494 insertions(+), 102 deletions(-) create mode 120000 crates/stef-playground/schemas diff --git a/crates/stef-build/src/decode.rs b/crates/stef-build/src/decode.rs index 7fc478b..44a7527 100644 --- a/crates/stef-build/src/decode.rs +++ b/crates/stef-build/src/decode.rs @@ -38,6 +38,7 @@ pub fn compile_struct( quote! { #[automatically_derived] impl #generics ::stef::Decode for #name #generics #generics_where { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { #body } @@ -269,20 +270,23 @@ fn compile_data_type(ty: &DataType<'_>) -> TokenStream { quote! { ::stef::buf::decode_option(r, |r| { #ty }) } } DataType::NonZero(ty) => match **ty { - DataType::U8 => quote! { NonZeroU8::decode(r) }, - DataType::U16 => quote! { NonZeroU16::decode(r) }, - DataType::U32 => quote! { NonZeroU32::decode(r) }, - DataType::U64 => quote! { NonZeroU64::decode(r) }, - DataType::U128 => quote! { NonZeroU128::decode(r) }, - DataType::I8 => quote! { NonZeroI8::decode(r) }, - DataType::I16 => quote! { NonZeroI16::decode(r) }, - DataType::I32 => quote! { NonZeroI32::decode(r) }, - DataType::I64 => quote! { NonZeroI64::decode(r) }, - DataType::I128 => quote! { NonZeroI128::decode(r) }, - _ => quote! { todo!() }, + DataType::U8 => quote! { ::std::num::NonZeroU8::decode(r) }, + DataType::U16 => quote! { ::std::num::NonZeroU16::decode(r) }, + DataType::U32 => quote! { ::std::num::NonZeroU32::decode(r) }, + DataType::U64 => quote! { ::std::num::NonZeroU64::decode(r) }, + DataType::U128 => quote! { ::std::num::NonZeroU128::decode(r) }, + DataType::I8 => quote! { ::std::num::NonZeroI8::decode(r) }, + DataType::I16 => quote! { ::std::num::NonZeroI16::decode(r) }, + DataType::I32 => quote! { ::std::num::NonZeroI32::decode(r) }, + DataType::I64 => quote! { ::std::num::NonZeroI64::decode(r) }, + DataType::I128 => quote! { ::std::num::NonZeroI128::decode(r) }, + _ => quote! { + let _r = r; + todo!(); + }, }, - DataType::BoxString => quote! { Box::decode(r) }, - DataType::BoxBytes => quote! { Box<[u8]>::decode(r) }, + DataType::BoxString => quote! { Box::::decode(r) }, + DataType::BoxBytes => quote! { Box::<[u8]>::decode(r) }, DataType::Tuple(types) => match types.len() { 2..=12 => { let types = types.iter().map(|ty| compile_data_type(ty)); diff --git a/crates/stef-build/src/definition.rs b/crates/stef-build/src/definition.rs index 3aa817b..3660183 100644 --- a/crates/stef-build/src/definition.rs +++ b/crates/stef-build/src/definition.rs @@ -10,7 +10,12 @@ use super::{decode, encode}; pub fn compile_schema(Schema { definitions }: &Schema<'_>) -> TokenStream { let definitions = definitions.iter().map(compile_definition); - quote! { #(#definitions)* } + quote! { + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; + + #(#definitions)* + } } fn compile_definition(definition: &Definition<'_>) -> TokenStream { @@ -60,6 +65,9 @@ fn compile_module( quote! { #comment pub mod #name { + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; + #(#definitions)* } } @@ -81,7 +89,7 @@ fn compile_struct( quote! { #comment - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[derive(Clone, Debug, PartialEq)] pub struct #name #generics #fields } } @@ -102,7 +110,7 @@ fn compile_enum( quote! { #comment - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[derive(Clone, Debug, PartialEq)] pub enum #name #generics { #(#variants,)* } @@ -267,27 +275,27 @@ pub(super) fn compile_data_type(ty: &DataType<'_>) -> TokenStream { DataType::HashMap(kv) => { let k = compile_data_type(&kv.0); let v = compile_data_type(&kv.1); - quote! { HashMap<#k, #v> } + quote! { ::std::collections::HashMap<#k, #v> } } DataType::HashSet(ty) => { let ty = compile_data_type(ty); - quote! { HashSet<#ty> } + quote! { ::std::collections::HashSet<#ty> } } DataType::Option(ty) => { let ty = compile_data_type(ty); quote! { Option<#ty> } } DataType::NonZero(ty) => match **ty { - DataType::U8 => quote! { NonZeroU8 }, - DataType::U16 => quote! { NonZeroU16 }, - DataType::U32 => quote! { NonZeroU32 }, - DataType::U64 => quote! { NonZeroU64 }, - DataType::U128 => quote! { NonZeroU128 }, - DataType::I8 => quote! { NonZeroI8 }, - DataType::I16 => quote! { NonZeroI16 }, - DataType::I32 => quote! { NonZeroI32 }, - DataType::I64 => quote! { NonZeroI64 }, - DataType::I128 => quote! { NonZeroI128 }, + DataType::U8 => quote! { ::std::num::NonZeroU8 }, + DataType::U16 => quote! { ::std::num::NonZeroU16 }, + DataType::U32 => quote! { ::std::num::NonZeroU32 }, + DataType::U64 => quote! { ::std::num::NonZeroU64 }, + DataType::U128 => quote! { ::std::num::NonZeroU128 }, + DataType::I8 => quote! { ::std::num::NonZeroI8 }, + DataType::I16 => quote! { ::std::num::NonZeroI16 }, + DataType::I32 => quote! { ::std::num::NonZeroI32 }, + DataType::I64 => quote! { ::std::num::NonZeroI64 }, + DataType::I128 => quote! { ::std::num::NonZeroI128 }, _ => compile_data_type(ty), }, DataType::BoxString => quote! { Box }, @@ -379,8 +387,13 @@ mod tests { mod sample {} "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// Hello world! - pub mod sample {} + pub mod sample { + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; + } "#}; parse(input, expect); @@ -397,8 +410,10 @@ mod tests { } "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// Hello world! - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[derive(Clone, Debug, PartialEq)] pub struct Sample { pub field1: u32, pub field2: Vec, @@ -406,7 +421,7 @@ mod tests { } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -441,6 +456,7 @@ mod tests { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut field1: Option = None; let mut field2: Option> = None; @@ -507,8 +523,10 @@ mod tests { } "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// Hello world! - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[derive(Clone, Debug, PartialEq)] pub enum Sample { Variant1, Variant2(u32, u8), @@ -631,6 +649,8 @@ mod tests { type Sample = String; "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// Hello world! #[allow(dead_code)] pub type Sample = String; @@ -654,6 +674,8 @@ mod tests { const BYTES: bytes = [1, 2, 3]; "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// A bool. #[allow(dead_code)] pub const BOOL: bool = true; @@ -681,6 +703,8 @@ mod tests { use other::module::Type; "#}; let expect = indoc! {r#" + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; use other::module; use other::module::Type; "#}; diff --git a/crates/stef-build/src/encode.rs b/crates/stef-build/src/encode.rs index 81ba0de..f762070 100644 --- a/crates/stef-build/src/encode.rs +++ b/crates/stef-build/src/encode.rs @@ -18,7 +18,7 @@ pub fn compile_struct( quote! { #[automatically_derived] impl #generics ::stef::Encode for #name #generics #generics_where { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { #fields } @@ -279,7 +279,7 @@ fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream { quote! { v } }, ); - quote! { ::stef::buf::encode_hash_map(w, #name, |w, k| { #ty_k; }, |w, v| { #ty_v; }) } + quote! { ::stef::buf::encode_hash_map(w, &#name, |w, k| { #ty_k; }, |w, v| { #ty_v; }) } } DataType::HashSet(ty) => { let ty = compile_data_type( @@ -290,7 +290,7 @@ fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream { quote! { v } }, ); - quote! { ::stef::buf::encode_hash_set(w, #name, |w, v| { #ty; }) } + quote! { ::stef::buf::encode_hash_set(w, &#name, |w, v| { #ty; }) } } DataType::Option(ty) => { let ty = compile_data_type( diff --git a/crates/stef-build/src/lib.rs b/crates/stef-build/src/lib.rs index 329a820..034b244 100644 --- a/crates/stef-build/src/lib.rs +++ b/crates/stef-build/src/lib.rs @@ -1,7 +1,10 @@ #![deny(rust_2018_idioms, clippy::all, clippy::pedantic)] #![allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] -use std::{path::{Path, PathBuf}, convert::AsRef}; +use std::{ + convert::AsRef, + path::{Path, PathBuf}, +}; use stef_parser::Schema; use thiserror::Error; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap index f1a90fe..697b96d 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@alias-basic.stef.snap @@ -3,6 +3,8 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Sample type alias.\ntype Sample = u32;" input_file: crates/stef-parser/tests/inputs/alias-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Sample type alias. #[allow(dead_code)] pub type Sample = u32; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap index 1fc0424..4d89385 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-multi.stef.snap @@ -3,15 +3,18 @@ source: crates/stef-build/tests/compiler.rs expression: "#[validate(min = 1, max = 100)]\nstruct Sample" input_file: crates/stef-parser/tests/inputs/attribute-multi.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample; #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) {} } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap index a7a3392..b4c521f 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-single.stef.snap @@ -3,15 +3,18 @@ source: crates/stef-build/tests/compiler.rs expression: "#[deprecated = \"don't use\"]\nstruct Sample" input_file: crates/stef-parser/tests/inputs/attribute-single.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample; #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) {} } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap index b2f0792..a150655 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attribute-unit.stef.snap @@ -3,15 +3,18 @@ source: crates/stef-build/tests/compiler.rs expression: "#[deprecated]\nstruct Sample" input_file: crates/stef-parser/tests/inputs/attribute-unit.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample; #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) {} } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap index 31c67c5..085d26d 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes-min-ws.stef.snap @@ -3,15 +3,18 @@ source: crates/stef-build/tests/compiler.rs expression: "#[deprecated=\"don't use\",compress]\n#[validate(in_range(min=100,max=200),non_empty)]\nstruct Sample" input_file: crates/stef-parser/tests/inputs/attributes-min-ws.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample; #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) {} } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap index aacddac..26291b3 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap @@ -3,15 +3,18 @@ source: crates/stef-build/tests/compiler.rs expression: "#[deprecated = \"don't use\", compress]\n#[validate(\n in_range(min = 100, max = 200),\n non_empty,\n)]\nstruct Sample" input_file: crates/stef-parser/tests/inputs/attributes.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample; #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) {} } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { Ok(Self) } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@const-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@const-basic.stef.snap index e71284c..6703342 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@const-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@const-basic.stef.snap @@ -3,6 +3,8 @@ source: crates/stef-build/tests/compiler.rs expression: "const BOOL_TRUE: bool = true;\nconst BOOL_FALSE: bool = false;\nconst INT: u32 = 100;\nconst FLOAT: f64 = 5.5;\nconst STRING: string = \"value\";\nconst BYTES: bytes = [1, 2, 3];" input_file: crates/stef-parser/tests/inputs/const-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; #[allow(dead_code)] pub const BOOL_TRUE: bool = true; #[allow(dead_code)] diff --git a/crates/stef-build/tests/snapshots/compiler__compile@const-string.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@const-string.stef.snap index c40d778..6505834 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@const-string.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@const-string.stef.snap @@ -3,6 +3,8 @@ source: crates/stef-build/tests/compiler.rs expression: "const SIMPLE: string = \"value\";\n\nconst NEWLINE_ESCAPE: string = \"one \\\n two \\\n three\\\n\";\n\nconst ESCAPES: string = \"escape basics \\r\\n \\t \\b \\f \\\\ \\\"\\\n hello\\\" \\n\\\n unicode \\u{2764} \\\n emoji ❤ \\\n\";\n\nconst MULTILINE: string = \"a\n b\n c\n\";" input_file: crates/stef-parser/tests/inputs/const-string.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; #[allow(dead_code)] pub const SIMPLE: &str = "value"; #[allow(dead_code)] diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap index dc02864..deb2102 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-basic.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Sample enum.\nenum Sample {\n One @1,\n /// Second variant\n Two(u32 @1, u64 @2) @2,\n Three {\n field1: u32 @1,\n /// Second field of third variant\n field2: bool @2,\n } @3,\n}" input_file: crates/stef-parser/tests/inputs/enum-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Sample enum. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub enum Sample { One, /// Second variant diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap index acfd9e7..f4c8a51 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-generics.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Enum with generics.\nenum Sample {\n One @1,\n Two(A @1, B @2) @2,\n Three {\n field1: C @1,\n field2: D @2,\n } @3,\n}" input_file: crates/stef-parser/tests/inputs/enum-generics.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Enum with generics. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub enum Sample { One, Two(A, B), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap index 76832d2..eab573c 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-many-ws.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Sample enum.\n enum Sample {\n\n One @1,\n\n Two ( u32 @1, u64 @2) @2,\n\n Three {\n\n field1: u32 @1,\n\n field2: bool @2,\n\n } @3,\n\n }" input_file: crates/stef-parser/tests/inputs/enum-many-ws.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Sample enum. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub enum Sample { One, Two(u32, u64), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap index 43456f1..fbc635b 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@enum-min-ws.stef.snap @@ -3,7 +3,9 @@ source: crates/stef-build/tests/compiler.rs expression: "enum Sample{One@1,Two(u32@1,u64@2,T@3)@2,Three{field1:u32@1,field2:bool@2,field3:T@3}@3}" input_file: crates/stef-parser/tests/inputs/enum-min-ws.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub enum Sample { One, Two(u32, u64, T), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@import-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@import-basic.stef.snap index 0ca9a1a..8847423 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@import-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@import-basic.stef.snap @@ -3,6 +3,8 @@ source: crates/stef-build/tests/compiler.rs expression: "use other::schema::Sample;\nuse second::submodule;" input_file: crates/stef-parser/tests/inputs/import-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; use other::schema::Sample; use second::submodule; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap index b57fe1c..66e27bf 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@module-basic.stef.snap @@ -3,10 +3,16 @@ source: crates/stef-build/tests/compiler.rs expression: "mod a {\n /// Inner module\n mod b {\n enum Sample {\n One @1,\n }\n }\n\n struct Sample {\n value: u32 @1,\n }\n}" input_file: crates/stef-parser/tests/inputs/module-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; pub mod a { + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; /// Inner module pub mod b { - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[allow(unused_imports)] + use ::stef::buf::{Decode, Encode}; + #[derive(Clone, Debug, PartialEq)] pub enum Sample { One, } @@ -31,13 +37,13 @@ pub mod a { } } } - #[derive(Clone, Debug, PartialEq, PartialOrd)] + #[derive(Clone, Debug, PartialEq)] pub struct Sample { pub value: u32, } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -51,6 +57,7 @@ pub mod a { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut value: Option = None; loop { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap index 7853546..723ed4c 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@schema-basic.stef.snap @@ -1,17 +1,19 @@ --- source: crates/stef-build/tests/compiler.rs -expression: "/// Basic struct.\nstruct Sample {\n a: u32 @1,\n b: bool @2,\n}\n\n/// Sample enum.\nenum Sample {\n One @1,\n Two(u32 @1, u64 @2) @2,\n Three {\n field1: u32 @1,\n field2: bool @2,\n } @3,\n}" +expression: "/// Basic struct.\nstruct SampleStruct {\n a: u32 @1,\n b: bool @2,\n}\n\n/// Sample enum.\nenum SampleEnum {\n One @1,\n Two(u32 @1, u64 @2) @2,\n Three {\n field1: u32 @1,\n field2: bool @2,\n } @3,\n}" input_file: crates/stef-parser/tests/inputs/schema-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Basic struct. -#[derive(Clone, Debug, PartialEq, PartialOrd)] -pub struct Sample { +#[derive(Clone, Debug, PartialEq)] +pub struct SampleStruct { pub a: u32, pub b: bool, } #[automatically_derived] -impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] +impl ::stef::Encode for SampleStruct { + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -31,7 +33,8 @@ impl ::stef::Encode for Sample { } } #[automatically_derived] -impl ::stef::Decode for Sample { +impl ::stef::Decode for SampleStruct { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; @@ -58,14 +61,14 @@ impl ::stef::Decode for Sample { } } /// Sample enum. -#[derive(Clone, Debug, PartialEq, PartialOrd)] -pub enum Sample { +#[derive(Clone, Debug, PartialEq)] +pub enum SampleEnum { One, Two(u32, u64), Three { field1: u32, field2: bool }, } #[automatically_derived] -impl ::stef::Encode for Sample { +impl ::stef::Encode for SampleEnum { #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { @@ -96,7 +99,7 @@ impl ::stef::Encode for Sample { } } #[automatically_derived] -impl ::stef::Decode for Sample { +impl ::stef::Decode for SampleEnum { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap index 69a4ae3..3914138 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-basic.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Basic struct.\nstruct Sample {\n a: u32 @1,\n /// Second field\n b: bool @2,\n}" input_file: crates/stef-parser/tests/inputs/struct-basic.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Basic struct. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub a: u32, /// Second field @@ -12,7 +14,7 @@ pub struct Sample { } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -33,6 +35,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap index ad0b06b..6947216 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-generics.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Generic key-value pair.\nstruct KeyValue {\n key: K @1,\n value: V @2,\n}" input_file: crates/stef-parser/tests/inputs/struct-generics.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Generic key-value pair. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub struct KeyValue { pub key: K, pub value: V, @@ -15,7 +17,7 @@ where K: ::stef::buf::Encode, V: ::stef::buf::Encode, { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -40,6 +42,7 @@ where K: ::std::fmt::Debug + ::stef::buf::Decode, V: ::std::fmt::Debug + ::stef::buf::Decode, { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut key: Option = None; let mut value: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap index 2c32760..1486080 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-many-ws.stef.snap @@ -3,8 +3,10 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Some comment\n struct Sample<\n A,\n B\n > {\n\n a: u32 @1,\n b: bool @2,\n\n }" input_file: crates/stef-parser/tests/inputs/struct-many-ws.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Some comment -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub a: u32, pub b: bool, @@ -15,7 +17,7 @@ where A: ::stef::buf::Encode, B: ::stef::buf::Encode, { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -40,6 +42,7 @@ where A: ::std::fmt::Debug + ::stef::buf::Decode, B: ::std::fmt::Debug + ::stef::buf::Decode, { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap index a3e8bdf..86cb27f 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-min-ws.stef.snap @@ -3,7 +3,9 @@ source: crates/stef-build/tests/compiler.rs expression: "struct Sample{a:u32@1,b:bool@2,c:T@3}" input_file: crates/stef-parser/tests/inputs/struct-min-ws.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub a: u32, pub b: bool, @@ -14,7 +16,7 @@ impl ::stef::Encode for Sample where T: ::stef::buf::Encode, { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -45,6 +47,7 @@ impl ::stef::Decode for Sample where T: ::std::fmt::Debug + ::stef::buf::Decode, { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; let mut b: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap index aa7499f..d5d9d87 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@struct-tuple.stef.snap @@ -3,12 +3,14 @@ source: crates/stef-build/tests/compiler.rs expression: "/// Basic struct.\nstruct Sample(u32 @1, bool @2)" input_file: crates/stef-parser/tests/inputs/struct-tuple.stef --- +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; /// Basic struct. -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[derive(Clone, Debug, PartialEq)] pub struct Sample(u32, bool); #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.0) }); ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.1) }); @@ -17,6 +19,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut n0: Option = None; let mut n1: Option = None; diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap index ccf5182..a6fce57 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-basic.stef.snap @@ -3,7 +3,9 @@ source: crates/stef-build/tests/compiler.rs expression: "struct Sample {\n f01: bool @1,\n f02: u8 @2,\n f03: u16 @3,\n f04: u32 @4,\n f05: u64 @5,\n f06: u128 @6,\n f07: i8 @7,\n f08: i16 @8,\n f09: i32 @9,\n f10: i64 @10,\n f11: i128 @11,\n f12: f32 @12,\n f13: f64 @13,\n f14: string @14,\n f15: &string @15,\n f16: bytes @16,\n f17: &bytes @17,\n f18: box @18,\n f19: box @19,\n f20: (u32, u32, u32) @20,\n f21: [u32; 12] @21,\n}" input_file: crates/stef-parser/tests/inputs/types-basic.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub f01: bool, pub f02: u8, @@ -29,7 +31,7 @@ pub struct Sample { } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -191,6 +193,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut f01: Option = None; let mut f02: Option = None; @@ -233,8 +236,8 @@ impl ::stef::Decode for Sample { 15 => f15 = Some(::stef::buf::decode_string(r)?), 16 => f16 = Some(::stef::buf::decode_bytes(r)?), 17 => f17 = Some(::stef::buf::decode_bytes(r)?), - 18 => f18 = Some(Box < str > ::decode(r)?), - 19 => f19 = Some(Box < [u8] > ::decode(r)?), + 18 => f18 = Some(Box::::decode(r)?), + 19 => f19 = Some(Box::<[u8]>::decode(r)?), 20 => { f20 = Some( { diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap index 10a7f9f..d3569a6 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-generic.stef.snap @@ -3,17 +3,19 @@ source: crates/stef-build/tests/compiler.rs expression: "struct Sample {\n f1: vec @1,\n f2: hash_map @2,\n f3: hash_set @3,\n f4: option @4,\n f5: non_zero @5,\n}" input_file: crates/stef-parser/tests/inputs/types-generic.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub f1: Vec, - pub f2: HashMap, - pub f3: HashSet, + pub f2: ::std::collections::HashMap, + pub f3: ::std::collections::HashSet, pub f4: Option, - pub f5: NonZeroU32, + pub f5: ::std::num::NonZeroU32, } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -34,7 +36,7 @@ impl ::stef::Encode for Sample { |w| { ::stef::buf::encode_hash_map( w, - self.f2, + &self.f2, |w, k| { ::stef::buf::encode_u32(w, *k); }, @@ -50,7 +52,7 @@ impl ::stef::Encode for Sample { |w| { ::stef::buf::encode_hash_set( w, - self.f3, + &self.f3, |w, v| { ::stef::buf::encode_u32(w, *v); }, @@ -77,12 +79,13 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut f1: Option> = None; - let mut f2: Option> = None; - let mut f3: Option> = None; + let mut f2: Option<::std::collections::HashMap> = None; + let mut f3: Option<::std::collections::HashSet> = None; let mut f4: Option = None; - let mut f5: Option = None; + let mut f5: Option<::std::num::NonZeroU32> = None; loop { match ::stef::buf::decode_id(r)? { ::stef::buf::END_MARKER => break, @@ -109,7 +112,7 @@ impl ::stef::Decode for Sample { ); } 4 => f4 = Some(::stef::buf::decode_u32(r)?), - 5 => f5 = Some(NonZeroU32::decode(r)?), + 5 => f5 = Some(::std::num::NonZeroU32::decode(r)?), _ => continue, } } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap index 6855ce7..f739caf 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-nested.stef.snap @@ -3,13 +3,15 @@ source: crates/stef-build/tests/compiler.rs expression: "struct Sample {\n value: vec>>>> @1,\n}" input_file: crates/stef-parser/tests/inputs/types-nested.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample { - pub value: Vec>>>, + pub value: Vec>>>, } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -35,8 +37,9 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { - let mut value: Option>>>> = None; + let mut value: Option>>>> = None; loop { match ::stef::buf::decode_id(r)? { ::stef::buf::END_MARKER => break, @@ -44,7 +47,15 @@ impl ::stef::Decode for Sample { value = Some( ::stef::buf::decode_vec( r, - |r| { ::stef::buf::decode_option(r, |r| { todo!() }) }, + |r| { + ::stef::buf::decode_option( + r, + |r| { + let _r = r; + todo!(); + }, + ) + }, )?, ); } diff --git a/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap b/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap index 9f4b753..ab3ab40 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@types-ref.stef.snap @@ -1,16 +1,18 @@ --- source: crates/stef-build/tests/compiler.rs -expression: "struct Sample {\n basic: Test123 @1,\n with_generics: KeyValue @2,\n}" +expression: "struct Sample {\n basic: Test123 @1,\n with_generics: KeyValue @2,\n}\n\nenum Test123 {\n Value @1,\n}\n\nstruct KeyValue {\n key: K @1,\n value: V @2,\n}" input_file: crates/stef-parser/tests/inputs/types-ref.stef --- -#[derive(Clone, Debug, PartialEq, PartialOrd)] +#[allow(unused_imports)] +use ::stef::buf::{Decode, Encode}; +#[derive(Clone, Debug, PartialEq)] pub struct Sample { pub basic: Test123, pub with_generics: KeyValue, } #[automatically_derived] impl ::stef::Encode for Sample { - #[allow(clippy::needless_borrow)] + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( w, @@ -31,6 +33,7 @@ impl ::stef::Encode for Sample { } #[automatically_derived] impl ::stef::Decode for Sample { + #[allow(clippy::type_complexity)] fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut basic: Option = None; let mut with_generics: Option> = None; @@ -56,4 +59,90 @@ impl ::stef::Decode for Sample { }) } } +#[derive(Clone, Debug, PartialEq)] +pub enum Test123 { + Value, +} +#[automatically_derived] +impl ::stef::Encode for Test123 { + #[allow(clippy::borrow_deref_ref)] + fn encode(&self, w: &mut impl ::stef::BufMut) { + match self { + Self::Value => { + ::stef::buf::encode_id(w, 1); + } + } + } +} +#[automatically_derived] +impl ::stef::Decode for Test123 { + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + match ::stef::buf::decode_id(r)? { + 1 => Ok(Self::Value), + id => Err(::stef::buf::Error::UnknownVariant(id)), + } + } +} +#[derive(Clone, Debug, PartialEq)] +pub struct KeyValue { + pub key: K, + pub value: V, +} +#[automatically_derived] +impl ::stef::Encode for KeyValue +where + K: ::stef::buf::Encode, + V: ::stef::buf::Encode, +{ + #[allow(clippy::needless_borrow, clippy::explicit_auto_deref)] + fn encode(&self, w: &mut impl ::stef::BufMut) { + ::stef::buf::encode_field( + w, + 1, + |w| { + (self.key).encode(w); + }, + ); + ::stef::buf::encode_field( + w, + 2, + |w| { + (self.value).encode(w); + }, + ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } +} +#[automatically_derived] +impl ::stef::Decode for KeyValue +where + K: ::std::fmt::Debug + ::stef::buf::Decode, + V: ::std::fmt::Debug + ::stef::buf::Decode, +{ + #[allow(clippy::type_complexity)] + fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { + let mut key: Option = None; + let mut value: Option = None; + loop { + match ::stef::buf::decode_id(r)? { + ::stef::buf::END_MARKER => break, + 1 => key = Some(K::decode(r)?), + 2 => value = Some(V::decode(r)?), + _ => continue, + } + } + Ok(Self { + key: key + .ok_or(::stef::buf::Error::MissingField { + id: 1, + name: Some("key"), + })?, + value: value + .ok_or(::stef::buf::Error::MissingField { + id: 2, + name: Some("value"), + })?, + }) + } +} diff --git a/crates/stef-parser/tests/inputs/schema-basic.stef b/crates/stef-parser/tests/inputs/schema-basic.stef index 105f96b..483ce6c 100644 --- a/crates/stef-parser/tests/inputs/schema-basic.stef +++ b/crates/stef-parser/tests/inputs/schema-basic.stef @@ -1,11 +1,11 @@ /// Basic struct. -struct Sample { +struct SampleStruct { a: u32 @1, b: bool @2, } /// Sample enum. -enum Sample { +enum SampleEnum { One @1, Two(u32 @1, u64 @2) @2, Three { diff --git a/crates/stef-parser/tests/inputs/types-ref.stef b/crates/stef-parser/tests/inputs/types-ref.stef index fc80ca5..6c4c234 100644 --- a/crates/stef-parser/tests/inputs/types-ref.stef +++ b/crates/stef-parser/tests/inputs/types-ref.stef @@ -2,3 +2,12 @@ struct Sample { basic: Test123 @1, with_generics: KeyValue @2, } + +enum Test123 { + Value @1, +} + +struct KeyValue { + key: K @1, + value: V @2, +} diff --git a/crates/stef-parser/tests/snapshots/parser__parse@schema-basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@schema-basic.stef.snap index b9b9f0b..f3bcf69 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@schema-basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@schema-basic.stef.snap @@ -15,7 +15,7 @@ Schema { attributes: Attributes( [], ), - name: "Sample", + name: "SampleStruct", generics: Generics( [], ), @@ -55,7 +55,7 @@ Schema { attributes: Attributes( [], ), - name: "Sample", + name: "SampleEnum", generics: Generics( [], ), diff --git a/crates/stef-parser/tests/snapshots/parser__parse@types-ref.stef.snap b/crates/stef-parser/tests/snapshots/parser__parse@types-ref.stef.snap index 7f2414a..4a73f6a 100644 --- a/crates/stef-parser/tests/snapshots/parser__parse@types-ref.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__parse@types-ref.stef.snap @@ -58,5 +58,84 @@ Schema { ), }, ), + Enum( + Enum { + comment: Comment( + [], + ), + attributes: Attributes( + [], + ), + name: "Test123", + generics: Generics( + [], + ), + variants: [ + Variant { + comment: Comment( + [], + ), + name: "Value", + fields: Unit, + id: Id( + 1, + ), + }, + ], + }, + ), + Struct( + Struct { + comment: Comment( + [], + ), + attributes: Attributes( + [], + ), + name: "KeyValue", + generics: Generics( + [ + "K", + "V", + ], + ), + fields: Named( + [ + NamedField { + comment: Comment( + [], + ), + name: "key", + ty: External( + ExternalType { + path: [], + name: "K", + generics: [], + }, + ), + id: Id( + 1, + ), + }, + NamedField { + comment: Comment( + [], + ), + name: "value", + ty: External( + ExternalType { + path: [], + name: "V", + generics: [], + }, + ), + id: Id( + 2, + ), + }, + ], + ), + }, + ), ], } diff --git a/crates/stef-parser/tests/snapshots/parser__print@schema-basic.stef.snap b/crates/stef-parser/tests/snapshots/parser__print@schema-basic.stef.snap index e5d3005..6d285fe 100644 --- a/crates/stef-parser/tests/snapshots/parser__print@schema-basic.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__print@schema-basic.stef.snap @@ -4,13 +4,13 @@ expression: "Schema :: parse(input.as_str()).unwrap()" input_file: crates/stef-parser/tests/inputs/schema-basic.stef --- /// Basic struct. -struct Sample { +struct SampleStruct { a: u32 @1, b: bool @2, } /// Sample enum. -enum Sample { +enum SampleEnum { One @1, Two(u32 @1, u64 @2) @2, Three { diff --git a/crates/stef-parser/tests/snapshots/parser__print@types-ref.stef.snap b/crates/stef-parser/tests/snapshots/parser__print@types-ref.stef.snap index 8fac8d2..031443c 100644 --- a/crates/stef-parser/tests/snapshots/parser__print@types-ref.stef.snap +++ b/crates/stef-parser/tests/snapshots/parser__print@types-ref.stef.snap @@ -8,4 +8,13 @@ struct Sample { with_generics: KeyValue @2, } +enum Test123 { + Value @1, +} + +struct KeyValue { + key: K @1, + value: V @2, +} + diff --git a/crates/stef-playground/build.rs b/crates/stef-playground/build.rs index 15c1db0..e8e4c26 100644 --- a/crates/stef-playground/build.rs +++ b/crates/stef-playground/build.rs @@ -1,3 +1,4 @@ fn main() { stef_build::compile(&["src/sample.stef"], &["src/"]).unwrap(); + stef_build::compile(&["schemas/*.stef"], &["schemas/"]).unwrap(); } diff --git a/crates/stef-playground/schemas b/crates/stef-playground/schemas new file mode 120000 index 0000000..402bde7 --- /dev/null +++ b/crates/stef-playground/schemas @@ -0,0 +1 @@ +../stef-parser/tests/inputs \ No newline at end of file diff --git a/crates/stef-playground/src/lib.rs b/crates/stef-playground/src/lib.rs index c12eb64..95e86b6 100644 --- a/crates/stef-playground/src/lib.rs +++ b/crates/stef-playground/src/lib.rs @@ -2,6 +2,106 @@ mod sample { include!(concat!(env!("OUT_DIR"), "/sample.rs")); } +mod schemas { + mod alias_basic { + include!(concat!(env!("OUT_DIR"), "/alias-basic.rs")); + } + + mod attribute_multi { + include!(concat!(env!("OUT_DIR"), "/attribute-multi.rs")); + } + + mod attribute_single { + include!(concat!(env!("OUT_DIR"), "/attribute-single.rs")); + } + + mod attribute_unit { + include!(concat!(env!("OUT_DIR"), "/attribute-unit.rs")); + } + + mod attributes_min_ws { + include!(concat!(env!("OUT_DIR"), "/attributes-min-ws.rs")); + } + + mod attributes { + include!(concat!(env!("OUT_DIR"), "/attributes.rs")); + } + + mod const_basic { + include!(concat!(env!("OUT_DIR"), "/const-basic.rs")); + } + + mod const_string { + include!(concat!(env!("OUT_DIR"), "/const-string.rs")); + } + + mod enum_basic { + include!(concat!(env!("OUT_DIR"), "/enum-basic.rs")); + } + + mod enum_generics { + include!(concat!(env!("OUT_DIR"), "/enum-generics.rs")); + } + + mod enum_many_ws { + include!(concat!(env!("OUT_DIR"), "/enum-many-ws.rs")); + } + + mod enum_min_ws { + include!(concat!(env!("OUT_DIR"), "/enum-min-ws.rs")); + } + + // TODO: implement imports + // mod import_basic { + // include!(concat!(env!("OUT_DIR"), "/import-basic.rs")); + // } + + mod module_basic { + include!(concat!(env!("OUT_DIR"), "/module-basic.rs")); + } + + mod schema_basic { + include!(concat!(env!("OUT_DIR"), "/schema-basic.rs")); + } + + mod struct_basic { + include!(concat!(env!("OUT_DIR"), "/struct-basic.rs")); + } + + mod struct_generics { + include!(concat!(env!("OUT_DIR"), "/struct-generics.rs")); + } + + // TODO: fix unused generic parameters + // mod struct_many_ws { + // include!(concat!(env!("OUT_DIR"), "/struct-many-ws.rs")); + // } + + mod struct_min_ws { + include!(concat!(env!("OUT_DIR"), "/struct-min-ws.rs")); + } + + mod struct_tuple { + include!(concat!(env!("OUT_DIR"), "/struct-tuple.rs")); + } + + mod types_basic { + include!(concat!(env!("OUT_DIR"), "/types-basic.rs")); + } + + mod types_generic { + include!(concat!(env!("OUT_DIR"), "/types-generic.rs")); + } + + mod types_nested { + include!(concat!(env!("OUT_DIR"), "/types-nested.rs")); + } + + mod types_ref { + include!(concat!(env!("OUT_DIR"), "/types-ref.rs")); + } +} + #[cfg(test)] mod tests { use std::fmt::Debug;