From ed24491a8361574bb295d34aad6fc70ed408777b Mon Sep 17 00:00:00 2001 From: Dominik Nakamura Date: Thu, 5 Oct 2023 16:16:53 +0900 Subject: [PATCH] fix: extend playground and correct issues --- Cargo.lock | 32 +++++----- crates/stef-build/src/decode.rs | 6 +- crates/stef-build/src/definition.rs | 40 +++++++++---- crates/stef-build/src/encode.rs | 31 +++++++--- ...ompiler__compile@attribute-multi.stef.snap | 3 + ...mpiler__compile@attribute-single.stef.snap | 3 + ...compiler__compile@attribute-unit.stef.snap | 3 + ...piler__compile@attributes-min-ws.stef.snap | 3 + .../compiler__compile@attributes.stef.snap | 3 + .../compiler__compile@const-basic.stef.snap | 18 ++++-- .../compiler__compile@const-string.stef.snap | 12 ++-- .../compiler__compile@enum-basic.stef.snap | 16 +++-- .../compiler__compile@enum-generics.stef.snap | 24 +++++--- .../compiler__compile@enum-many-ws.stef.snap | 16 +++-- .../compiler__compile@enum-min-ws.stef.snap | 22 ++++--- .../compiler__compile@module-basic.stef.snap | 10 +++- .../compiler__compile@schema-basic.stef.snap | 20 +++++-- .../compiler__compile@struct-basic.stef.snap | 4 ++ ...ompiler__compile@struct-generics.stef.snap | 12 ++-- ...compiler__compile@struct-many-ws.stef.snap | 8 ++- .../compiler__compile@struct-min-ws.stef.snap | 8 ++- .../compiler__compile@types-basic.stef.snap | 4 ++ .../compiler__compile@types-generic.stef.snap | 6 +- .../compiler__compile@types-nested.stef.snap | 4 ++ .../compiler__compile@types-ref.stef.snap | 8 ++- crates/stef-playground/src/main.rs | 58 ++++++++++++++++++- crates/stef-playground/src/sample.stef | 25 ++++++++ crates/stef/src/buf/decode.rs | 9 +++ crates/stef/src/buf/encode.rs | 7 +++ 29 files changed, 323 insertions(+), 92 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7be2306..96dcdeb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -34,9 +34,9 @@ checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" [[package]] name = "anstream" -version = "0.6.1" +version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6cd65a4b849ace0b7f6daeebcc1a1d111282227ca745458c61dbf670e52a597" +checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44" dependencies = [ "anstyle", "anstyle-parse", @@ -72,9 +72,9 @@ dependencies = [ [[package]] name = "anstyle-wincon" -version = "3.0.0" +version = "3.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0238ca56c96dfa37bdf7c373c8886dd591322500aceeeccdb2216fe06dc2f796" +checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628" dependencies = [ "anstyle", "windows-sys 0.48.0", @@ -349,9 +349,9 @@ checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] name = "errno" -version = "0.3.3" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "136526188508e25c6fef639d7927dfb3e0e3084488bf202267829cf7fc23dbdd" +checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480" dependencies = [ "errno-dragonfly", "libc", @@ -528,9 +528,9 @@ checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" [[package]] name = "linux-raw-sys" -version = "0.4.7" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a9bad9f94746442c783ca431b22403b519cd7fbeed0533fdd6328b2f2212128" +checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db" [[package]] name = "log" @@ -540,9 +540,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" [[package]] name = "memchr" -version = "2.6.3" +version = "2.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f232d6ef707e1956a43342693d2a31e72989554d58299d7a88738cc95b0d35c" +checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167" [[package]] name = "memoffset" @@ -734,9 +734,9 @@ dependencies = [ [[package]] name = "regex" -version = "1.9.5" +version = "1.9.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "697061221ea1b4a94a624f67d0ae2bfe4e22b8a17b6a192afb11046542cc8c47" +checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff" dependencies = [ "aho-corasick", "memchr", @@ -746,9 +746,9 @@ dependencies = [ [[package]] name = "regex-automata" -version = "0.3.8" +version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2f401f4955220693b56f8ec66ee9c78abffd8d1c4f23dc41a23839eb88f0795" +checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9" dependencies = [ "aho-corasick", "memchr", @@ -769,9 +769,9 @@ checksum = "d626bb9dae77e28219937af045c257c28bfd3f69333c512553507f5f9798cb76" [[package]] name = "rustix" -version = "0.38.14" +version = "0.38.17" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "747c788e9ce8e92b12cd485c49ddf90723550b654b32508f979b71a7b1ecda4f" +checksum = "f25469e9ae0f3d0047ca8b93fc56843f38e6774f0914a107ff8b41be8be8e0b7" dependencies = [ "bitflags", "errno", diff --git a/crates/stef-build/src/decode.rs b/crates/stef-build/src/decode.rs index b45b01a..78f6992 100644 --- a/crates/stef-build/src/decode.rs +++ b/crates/stef-build/src/decode.rs @@ -36,6 +36,7 @@ pub fn compile_struct( }; quote! { + #[automatically_derived] impl #generics ::stef::Decode for #name #generics #generics_where { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { #body @@ -58,11 +59,12 @@ pub fn compile_enum( let variants = variants.iter().map(compile_variant); quote! { + #[automatically_derived] impl #generics ::stef::Decode for #name #generics #generics_where { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { #(#variants,)* - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } @@ -217,7 +219,7 @@ fn compile_generics(Generics(types): &Generics<'_>) -> (TokenStream, TokenStream ( quote! { <#(#types,)*> }, - quote! { where #(#types2: ::stef::buf::Decode,)* }, + quote! { where #(#types2: ::std::fmt::Debug + ::stef::buf::Decode,)* }, ) }) .unwrap_or_default() diff --git a/crates/stef-build/src/definition.rs b/crates/stef-build/src/definition.rs index 2a8770d..d6bb5c1 100644 --- a/crates/stef-build/src/definition.rs +++ b/crates/stef-build/src/definition.rs @@ -81,6 +81,7 @@ fn compile_struct( quote! { #comment + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct #name #generics #fields } } @@ -101,6 +102,7 @@ fn compile_enum( quote! { #comment + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum #name #generics { #(#variants,)* } @@ -157,7 +159,8 @@ fn compile_const( quote! { #comment - const #name: #ty = #value; + #[allow(dead_code)] + pub const #name: #ty = #value; } } @@ -390,11 +393,13 @@ mod tests { "#}; let expect = indoc! {r#" /// Hello world! + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub field1: u32, pub field2: Vec, pub field3: (bool, [i16; 4]), } + #[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.field1) }); @@ -408,8 +413,10 @@ mod tests { 3, |w| { ::stef::buf::encode_tuple2(w, &self.field3) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } + #[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut field1: Option = None; @@ -463,12 +470,15 @@ mod tests { "#}; let expect = indoc! {r#" /// Hello world! + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { Variant1, Variant2(u32, u8), Variant3 { field1: String, field2: Vec }, } + #[automatically_derived] impl ::stef::Encode for Sample { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::Variant1 => { @@ -476,25 +486,28 @@ mod tests { } Self::Variant2(n0, n1) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, n0) }); - ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u8(w, n1) }); + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u8(w, *n1) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Variant3 { field1, field2 } => { ::stef::buf::encode_id(w, 3); ::stef::buf::encode_field( w, 1, - |w| { ::stef::buf::encode_string(w, &field1) }, + |w| { ::stef::buf::encode_string(w, &*field1) }, ); ::stef::buf::encode_field( w, 2, - |w| { ::stef::buf::encode_vec(w, &field2) }, + |w| { ::stef::buf::encode_vec(w, &*field2) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } + #[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -549,7 +562,7 @@ mod tests { })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } @@ -588,15 +601,20 @@ mod tests { "#}; let expect = indoc! {r#" /// A bool. - const BOOL: bool = true; + #[allow(dead_code)] + pub const BOOL: bool = true; /// An integer. - const INT: u32 = 100; + #[allow(dead_code)] + pub const INT: u32 = 100; /// A float. - const FLOAT: f64 = 5.0; + #[allow(dead_code)] + pub const FLOAT: f64 = 5.0; /// A string. - const STRING: &str = "hello"; + #[allow(dead_code)] + pub const STRING: &str = "hello"; /// Some bytes. - const BYTES: &[u8] = b"\x01\x02\x03"; + #[allow(dead_code)] + pub const BYTES: &[u8] = b"\x01\x02\x03"; "#}; parse(input, expect); diff --git a/crates/stef-build/src/encode.rs b/crates/stef-build/src/encode.rs index e1b7e52..55a20f1 100644 --- a/crates/stef-build/src/encode.rs +++ b/crates/stef-build/src/encode.rs @@ -1,5 +1,5 @@ use proc_macro2::{Ident, Span, TokenStream}; -use quote::{quote, ToTokens}; +use quote::quote; use stef_parser::{DataType, Enum, Fields, Generics, NamedField, Struct, UnnamedField, Variant}; pub fn compile_struct( @@ -16,6 +16,7 @@ pub fn compile_struct( let fields = compile_struct_fields(fields); quote! { + #[automatically_derived] impl #generics ::stef::Encode for #name #generics #generics_where { fn encode(&self, w: &mut impl ::stef::BufMut) { #fields @@ -42,7 +43,10 @@ fn compile_struct_fields(fields: &Fields<'_>) -> TokenStream { }, ); - quote! { #(#calls)* } + quote! { + #(#calls)* + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } } Fields::Unnamed(unnamed) => { let calls = unnamed @@ -56,7 +60,10 @@ fn compile_struct_fields(fields: &Fields<'_>) -> TokenStream { quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); } }); - quote! { #(#calls)* } + quote! { + #(#calls)* + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } } Fields::Unit => quote! {}, } @@ -76,7 +83,9 @@ pub fn compile_enum( let variants = variants.iter().map(compile_variant); quote! { + #[automatically_derived] impl #generics ::stef::Encode for #name #generics #generics_where { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { #(#variants,)* @@ -145,13 +154,16 @@ fn compile_variant_fields(fields: &Fields<'_>) -> TokenStream { }| { let id = proc_macro2::Literal::u32_unsuffixed(id.0); let name = proc_macro2::Ident::new(name, Span::call_site()); - let ty = compile_data_type(ty, quote! { #name }); + let ty = compile_data_type(ty, quote! { *#name }); quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); } }, ); - quote! { #(#calls)* } + quote! { + #(#calls)* + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } } Fields::Unnamed(unnamed) => { let calls = unnamed @@ -160,12 +172,15 @@ fn compile_variant_fields(fields: &Fields<'_>) -> TokenStream { .map(|(idx, UnnamedField { ty, id })| { let id = proc_macro2::Literal::u32_unsuffixed(id.0); let name = Ident::new(&format!("n{idx}"), Span::call_site()); - let ty = compile_data_type(ty, name.to_token_stream()); + let ty = compile_data_type(ty, quote! { *#name }); quote! { ::stef::buf::encode_field(w, #id, |w| { #ty }); } }); - quote! { #(#calls)* } + quote! { + #(#calls)* + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); + } } Fields::Unit => quote! {}, } @@ -222,7 +237,7 @@ fn compile_data_type(ty: &DataType<'_>, name: TokenStream) -> TokenStream { quote! { ::stef::buf::encode_array(w, &#name) } } DataType::NonZero(_) | DataType::External(_) => { - quote! { #name.encode(w) } + quote! { (#name).encode(w) } } } } 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 e3b9fff..6b72720 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,10 +3,13 @@ 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)] pub struct Sample; +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) {} } +#[automatically_derived] impl ::stef::Decode for Sample { 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 30c6278..5ce3c37 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,10 +3,13 @@ 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)] pub struct Sample; +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) {} } +#[automatically_derived] impl ::stef::Decode for Sample { 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 98b85f2..d1f8db6 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,10 +3,13 @@ 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)] pub struct Sample; +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) {} } +#[automatically_derived] impl ::stef::Decode for Sample { 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 69aa573..3a5fd73 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,10 +3,13 @@ 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)] pub struct Sample; +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) {} } +#[automatically_derived] impl ::stef::Decode for Sample { 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 bfadd0f..eddcfea 100644 --- a/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap +++ b/crates/stef-build/tests/snapshots/compiler__compile@attributes.stef.snap @@ -3,10 +3,13 @@ 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)] pub struct Sample; +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) {} } +#[automatically_derived] impl ::stef::Decode for Sample { 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 b7ba462..e71284c 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,10 +3,16 @@ 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 --- -const BOOL_TRUE: bool = true; -const BOOL_FALSE: bool = false; -const INT: u32 = 100; -const FLOAT: f64 = 5.5; -const STRING: &str = "value"; -const BYTES: &[u8] = b"\x01\x02\x03"; +#[allow(dead_code)] +pub const BOOL_TRUE: bool = true; +#[allow(dead_code)] +pub const BOOL_FALSE: bool = false; +#[allow(dead_code)] +pub const INT: u32 = 100; +#[allow(dead_code)] +pub const FLOAT: f64 = 5.5; +#[allow(dead_code)] +pub const STRING: &str = "value"; +#[allow(dead_code)] +pub const BYTES: &[u8] = b"\x01\x02\x03"; 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 d4ff2a6..c40d778 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,8 +3,12 @@ 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 --- -const SIMPLE: &str = "value"; -const NEWLINE_ESCAPE: &str = "one two three"; -const ESCAPES: &str = "escape basics \r\n \t \u{8} \u{c} \\ \"hello\" \nunicode ❤ emoji ❤ "; -const MULTILINE: &str = "a\n b\n c\n"; +#[allow(dead_code)] +pub const SIMPLE: &str = "value"; +#[allow(dead_code)] +pub const NEWLINE_ESCAPE: &str = "one two three"; +#[allow(dead_code)] +pub const ESCAPES: &str = "escape basics \r\n \t \u{8} \u{c} \\ \"hello\" \nunicode ❤ emoji ❤ "; +#[allow(dead_code)] +pub const MULTILINE: &str = "a\n b\n c\n"; 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 5c3f0fd..dc02864 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 @@ -4,6 +4,7 @@ expression: "/// Sample enum.\nenum Sample {\n One @1,\n /// Second varian input_file: crates/stef-parser/tests/inputs/enum-basic.stef --- /// Sample enum. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { One, /// Second variant @@ -14,7 +15,9 @@ pub enum Sample { field2: bool, }, } +#[automatically_derived] impl ::stef::Encode for Sample { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -22,25 +25,28 @@ impl ::stef::Encode for Sample { } Self::Two(n0, n1) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, n0) }); - ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, n1) }); + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, *n1) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Three { field1, field2 } => { ::stef::buf::encode_id(w, 3); ::stef::buf::encode_field( w, 1, - |w| { ::stef::buf::encode_u32(w, field1) }, + |w| { ::stef::buf::encode_u32(w, *field1) }, ); ::stef::buf::encode_field( w, 2, - |w| { ::stef::buf::encode_bool(w, field2) }, + |w| { ::stef::buf::encode_bool(w, *field2) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -95,7 +101,7 @@ impl ::stef::Decode for Sample { })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } 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 0b44265..acfd9e7 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 @@ -4,11 +4,13 @@ expression: "/// Enum with generics.\nenum Sample {\n One @1,\n input_file: crates/stef-parser/tests/inputs/enum-generics.stef --- /// Enum with generics. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { One, Two(A, B), Three { field1: C, field2: D }, } +#[automatically_derived] impl ::stef::Encode for Sample where A: ::stef::buf::Encode, @@ -16,6 +18,7 @@ where C: ::stef::buf::Encode, D: ::stef::buf::Encode, { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -23,23 +26,26 @@ where } Self::Two(n0, n1) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { n0.encode(w) }); - ::stef::buf::encode_field(w, 2, |w| { n1.encode(w) }); + ::stef::buf::encode_field(w, 1, |w| { (*n0).encode(w) }); + ::stef::buf::encode_field(w, 2, |w| { (*n1).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Three { field1, field2 } => { ::stef::buf::encode_id(w, 3); - ::stef::buf::encode_field(w, 1, |w| { field1.encode(w) }); - ::stef::buf::encode_field(w, 2, |w| { field2.encode(w) }); + ::stef::buf::encode_field(w, 1, |w| { (*field1).encode(w) }); + ::stef::buf::encode_field(w, 2, |w| { (*field2).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } +#[automatically_derived] impl ::stef::Decode for Sample where - A: ::stef::buf::Decode, - B: ::stef::buf::Decode, - C: ::stef::buf::Decode, - D: ::stef::buf::Decode, + A: ::std::fmt::Debug + ::stef::buf::Decode, + B: ::std::fmt::Debug + ::stef::buf::Decode, + C: ::std::fmt::Debug + ::stef::buf::Decode, + D: ::std::fmt::Debug + ::stef::buf::Decode, { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -94,7 +100,7 @@ where })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } 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 4a71456..76832d2 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 @@ -4,12 +4,15 @@ expression: "/// Sample enum.\n enum Sample {\n\n One @1,\n\n input_file: crates/stef-parser/tests/inputs/enum-many-ws.stef --- /// Sample enum. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { One, Two(u32, u64), Three { field1: u32, field2: bool }, } +#[automatically_derived] impl ::stef::Encode for Sample { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -17,25 +20,28 @@ impl ::stef::Encode for Sample { } Self::Two(n0, n1) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, n0) }); - ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, n1) }); + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, *n1) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Three { field1, field2 } => { ::stef::buf::encode_id(w, 3); ::stef::buf::encode_field( w, 1, - |w| { ::stef::buf::encode_u32(w, field1) }, + |w| { ::stef::buf::encode_u32(w, *field1) }, ); ::stef::buf::encode_field( w, 2, - |w| { ::stef::buf::encode_bool(w, field2) }, + |w| { ::stef::buf::encode_bool(w, *field2) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -90,7 +96,7 @@ impl ::stef::Decode for Sample { })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } 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 85e4aa5..43456f1 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,15 +3,18 @@ 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)] pub enum Sample { One, Two(u32, u64, T), Three { field1: u32, field2: bool, field3: T }, } +#[automatically_derived] impl ::stef::Encode for Sample where T: ::stef::buf::Encode, { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -19,30 +22,33 @@ where } Self::Two(n0, n1, n2) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, n0) }); - ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, n1) }); - ::stef::buf::encode_field(w, 3, |w| { n2.encode(w) }); + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, *n1) }); + ::stef::buf::encode_field(w, 3, |w| { (*n2).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Three { field1, field2, field3 } => { ::stef::buf::encode_id(w, 3); ::stef::buf::encode_field( w, 1, - |w| { ::stef::buf::encode_u32(w, field1) }, + |w| { ::stef::buf::encode_u32(w, *field1) }, ); ::stef::buf::encode_field( w, 2, - |w| { ::stef::buf::encode_bool(w, field2) }, + |w| { ::stef::buf::encode_bool(w, *field2) }, ); - ::stef::buf::encode_field(w, 3, |w| { field3.encode(w) }); + ::stef::buf::encode_field(w, 3, |w| { (*field3).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } +#[automatically_derived] impl ::stef::Decode for Sample where - T: ::stef::buf::Decode, + T: ::std::fmt::Debug + ::stef::buf::Decode, { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -111,7 +117,7 @@ where })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } 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 80e981b..0c6d863 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 @@ -6,10 +6,13 @@ input_file: crates/stef-parser/tests/inputs/module-basic.stef pub mod a { /// Inner module pub mod b { + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { One, } + #[automatically_derived] impl ::stef::Encode for Sample { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -18,18 +21,21 @@ pub mod a { } } } + #[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { 1 => Ok(Self::One), - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } } + #[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub value: u32, } + #[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field( @@ -37,8 +43,10 @@ pub mod a { 1, |w| { ::stef::buf::encode_u32(w, self.value) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } + #[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut value: Option = None; 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 fd40972..56cbd73 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 @@ -4,16 +4,20 @@ expression: "/// Basic struct.\nstruct Sample {\n a: u32 @1,\n b: bool @2, input_file: crates/stef-parser/tests/inputs/schema-basic.stef --- /// Basic struct. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub a: u32, pub b: bool, } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.a) }); ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.b) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: Option = None; @@ -41,12 +45,15 @@ impl ::stef::Decode for Sample { } } /// Sample enum. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub enum Sample { One, Two(u32, u64), Three { field1: u32, field2: bool }, } +#[automatically_derived] impl ::stef::Encode for Sample { + #[allow(clippy::borrow_deref_ref)] fn encode(&self, w: &mut impl ::stef::BufMut) { match self { Self::One => { @@ -54,25 +61,28 @@ impl ::stef::Encode for Sample { } Self::Two(n0, n1) => { ::stef::buf::encode_id(w, 2); - ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, n0) }); - ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, n1) }); + ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, *n0) }); + ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_u64(w, *n1) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } Self::Three { field1, field2 } => { ::stef::buf::encode_id(w, 3); ::stef::buf::encode_field( w, 1, - |w| { ::stef::buf::encode_u32(w, field1) }, + |w| { ::stef::buf::encode_u32(w, *field1) }, ); ::stef::buf::encode_field( w, 2, - |w| { ::stef::buf::encode_bool(w, field2) }, + |w| { ::stef::buf::encode_bool(w, *field2) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { match ::stef::buf::decode_id(r)? { @@ -127,7 +137,7 @@ impl ::stef::Decode for Sample { })?, }) } - id => Err(Error::UnknownVariant(id)), + id => Err(::stef::buf::Error::UnknownVariant(id)), } } } 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 f41f79a..e439dd4 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 @@ -4,17 +4,21 @@ expression: "/// Basic struct.\nstruct Sample {\n a: u32 @1,\n /// Second input_file: crates/stef-parser/tests/inputs/struct-basic.stef --- /// Basic struct. +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub a: u32, /// Second field pub b: bool, } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.a) }); ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.b) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: 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 aca994b..05a3107 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 @@ -4,24 +4,28 @@ expression: "/// Generic key-value pair.\nstruct KeyValue {\n key: K @1 input_file: crates/stef-parser/tests/inputs/struct-generics.stef --- /// Generic key-value pair. +#[derive(Clone, Debug, PartialEq, PartialOrd)] 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, { 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_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: ::stef::buf::Decode, - V: ::stef::buf::Decode, + K: ::std::fmt::Debug + ::stef::buf::Decode, + V: ::std::fmt::Debug + ::stef::buf::Decode, { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut key: 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 e5410a9..41402d4 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 @@ -4,10 +4,12 @@ expression: "/// Some comment\n struct Sample<\n A,\n input_file: crates/stef-parser/tests/inputs/struct-many-ws.stef --- /// Some comment +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub a: u32, pub b: bool, } +#[automatically_derived] impl ::stef::Encode for Sample where A: ::stef::buf::Encode, @@ -16,12 +18,14 @@ where fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.a) }); ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.b) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample where - A: ::stef::buf::Decode, - B: ::stef::buf::Decode, + A: ::std::fmt::Debug + ::stef::buf::Decode, + B: ::std::fmt::Debug + ::stef::buf::Decode, { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: 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 ee68143..7ce555e 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,11 +3,13 @@ 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)] pub struct Sample { pub a: u32, pub b: bool, pub c: T, } +#[automatically_derived] impl ::stef::Encode for Sample where T: ::stef::buf::Encode, @@ -15,12 +17,14 @@ where fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_u32(w, self.a) }); ::stef::buf::encode_field(w, 2, |w| { ::stef::buf::encode_bool(w, self.b) }); - ::stef::buf::encode_field(w, 3, |w| { self.c.encode(w) }); + ::stef::buf::encode_field(w, 3, |w| { (self.c).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample where - T: ::stef::buf::Decode, + T: ::std::fmt::Debug + ::stef::buf::Decode, { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut a: 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 6d668c9..6580117 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,6 +3,7 @@ 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)] pub struct Sample { pub f01: bool, pub f02: u8, @@ -26,6 +27,7 @@ pub struct Sample { pub f20: (u32, u32, u32), pub f21: [u32; 12], } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_bool(w, self.f01) }); @@ -81,8 +83,10 @@ impl ::stef::Encode for Sample { 21, |w| { ::stef::buf::encode_array(w, &self.f21) }, ); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut f01: Option = None; 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 96d12e1..9a09c43 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,6 +3,7 @@ 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)] pub struct Sample { pub f1: Vec, pub f2: HashMap, @@ -10,6 +11,7 @@ pub struct Sample { pub f4: Option, pub f5: NonZeroU32, } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_vec(w, &self.f1) }); @@ -24,9 +26,11 @@ impl ::stef::Encode for Sample { |w| { ::stef::buf::encode_hash_set(w, self.f3) }, ); ::stef::buf::encode_field(w, 4, |w| { ::stef::buf::encode_option(w, self.f4) }); - ::stef::buf::encode_field(w, 5, |w| { self.f5.encode(w) }); + ::stef::buf::encode_field(w, 5, |w| { (self.f5).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut f1: Option> = None; 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 7f65c18..1f047f5 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,14 +3,18 @@ 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)] pub struct Sample { pub value: Vec>>>, } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { ::stef::buf::encode_field(w, 1, |w| { ::stef::buf::encode_vec(w, &self.value) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut value: Option>>>> = None; 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 ec347bc..2d88029 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 @@ -3,16 +3,20 @@ source: crates/stef-build/tests/compiler.rs expression: "struct Sample {\n basic: Test123 @1,\n with_generics: KeyValue @2,\n}" input_file: crates/stef-parser/tests/inputs/types-ref.stef --- +#[derive(Clone, Debug, PartialEq, PartialOrd)] pub struct Sample { pub basic: Test123, pub with_generics: KeyValue, } +#[automatically_derived] impl ::stef::Encode for Sample { fn encode(&self, w: &mut impl ::stef::BufMut) { - ::stef::buf::encode_field(w, 1, |w| { self.basic.encode(w) }); - ::stef::buf::encode_field(w, 2, |w| { self.with_generics.encode(w) }); + ::stef::buf::encode_field(w, 1, |w| { (self.basic).encode(w) }); + ::stef::buf::encode_field(w, 2, |w| { (self.with_generics).encode(w) }); + ::stef::buf::encode_u32(w, ::stef::buf::END_MARKER); } } +#[automatically_derived] impl ::stef::Decode for Sample { fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result { let mut basic: Option = None; diff --git a/crates/stef-playground/src/main.rs b/crates/stef-playground/src/main.rs index e2a802c..b899bfa 100644 --- a/crates/stef-playground/src/main.rs +++ b/crates/stef-playground/src/main.rs @@ -1,7 +1,63 @@ +use stef::{Decode, Encode}; + mod sample { include!(concat!(env!("OUT_DIR"), "/sample.rs")); } fn main() { - println!("Hello, world!"); + let mut buf = Vec::new(); + let v = sample::Sample { + a: 5, + b: true, + c: ("Test".into(), -2), + }; + v.encode(&mut buf); + println!("{buf:?}"); + + let v2 = sample::Sample::decode(&mut &*buf).unwrap(); + assert_eq!(v, v2); + + buf.clear(); + let v = sample::Sample2::Unit; + v.encode(&mut buf); + println!("{buf:?}"); + + let v2 = sample::Sample2::decode(&mut &*buf).unwrap(); + assert_eq!(v, v2); + + buf.clear(); + let v = sample::Sample2::Tuple(7, 8); + v.encode(&mut buf); + println!("{buf:?}"); + + let v2 = sample::Sample2::decode(&mut &*buf).unwrap(); + assert_eq!(v, v2); + + buf.clear(); + let v = sample::Sample2::Fields { + name: "this".into(), + valid: true, + dates: vec![ + (2023, 1, 1), + (2023, 10, 5), + (2023, 12, sample::CHRISTMAS_DAY), + ], + }; + v.encode(&mut buf); + println!("{buf:?}"); + + let v2 = sample::Sample2::decode(&mut &*buf).unwrap(); + assert_eq!(v, v2); + + buf.clear(); + let v = sample::SampleGen { + raw: vec![5, 6, 7, 8], + array: [9_i16; 4], + value: 9, + }; + v.encode(&mut buf); + println!("{buf:?}"); + + let v2 = sample::SampleGen::decode(&mut &*buf).unwrap(); + assert_eq!(v, v2); } diff --git a/crates/stef-playground/src/sample.stef b/crates/stef-playground/src/sample.stef index 0950419..367ac14 100644 --- a/crates/stef-playground/src/sample.stef +++ b/crates/stef-playground/src/sample.stef @@ -3,4 +3,29 @@ struct Sample { a: u32 @1, /// Second field b: bool @2, + c: (string, i16) @3, +} + +enum Sample2 { + Unit @1, + Tuple(u32 @1, u32 @2) @2, + Fields { + name: string @1, + valid: bool @2, + dates: vec<(u16, u8, u8)> @3, + } @3, +} + +const CHRISTMAS_MONTH: u8 = 12; +const CHRISTMAS_DAY: u8 = 24; + +/// Using generics +struct SampleGen { + raw: bytes @1, + array: [T; 4] @2, + value: T @3, +} + +enum SampleGen2 { + Value(T @1) @1, } diff --git a/crates/stef/src/buf/decode.rs b/crates/stef/src/buf/decode.rs index d23505d..811b0ae 100644 --- a/crates/stef/src/buf/decode.rs +++ b/crates/stef/src/buf/decode.rs @@ -12,11 +12,13 @@ use crate::varint; pub type Result = std::result::Result; +#[derive(Debug)] pub enum Error { InsufficentData, DecodeInt(varint::DecodeIntError), NonUtf8(std::string::FromUtf8Error), MissingField { id: u32, name: Option<&'static str> }, + UnknownVariant(u32), } impl From for Error { @@ -400,6 +402,13 @@ forward!(u8, u16, u32, u64, u128); forward!(i8, i16, i32, i64, i128); forward!(f32, f64); +impl Decode for String { + #[inline(always)] + fn decode(r: &mut impl Buf) -> Result { + decode_string(r) + } +} + impl Decode for Box { #[inline(always)] fn decode(r: &mut impl Buf) -> Result { diff --git a/crates/stef/src/buf/encode.rs b/crates/stef/src/buf/encode.rs index 93130d7..c2befb4 100644 --- a/crates/stef/src/buf/encode.rs +++ b/crates/stef/src/buf/encode.rs @@ -357,6 +357,13 @@ forward!(u8, u16, u32, u64, u128); forward!(i8, i16, i32, i64, i128); forward!(f32, f64); +impl Encode for String { + #[inline(always)] + fn encode(&self, w: &mut impl BufMut) { + encode_string(w, self) + } +} + impl Encode for Box { #[inline(always)] fn encode(&self, w: &mut impl BufMut) {