Skip to content

Commit

Permalink
fix: extend playground and correct issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 5, 2023
1 parent b55862f commit ed24491
Show file tree
Hide file tree
Showing 29 changed files with 323 additions and 92 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
#body
Expand All @@ -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<Self> {
match ::stef::buf::decode_id(r)? {
#(#variants,)*
id => Err(Error::UnknownVariant(id)),
id => Err(::stef::buf::Error::UnknownVariant(id)),
}
}
}
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 29 additions & 11 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ fn compile_struct(

quote! {
#comment
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub struct #name #generics #fields
}
}
Expand All @@ -101,6 +102,7 @@ fn compile_enum(

quote! {
#comment
#[derive(Clone, Debug, PartialEq, PartialOrd)]
pub enum #name #generics {
#(#variants,)*
}
Expand Down Expand Up @@ -157,7 +159,8 @@ fn compile_const(

quote! {
#comment
const #name: #ty = #value;
#[allow(dead_code)]
pub const #name: #ty = #value;
}
}

Expand Down Expand Up @@ -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<u8>,
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) });
Expand All @@ -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<Self> {
let mut field1: Option<u32> = None;
Expand Down Expand Up @@ -463,38 +470,44 @@ 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<bool> },
}
#[automatically_derived]
impl ::stef::Encode for Sample {
#[allow(clippy::borrow_deref_ref)]
fn encode(&self, w: &mut impl ::stef::BufMut) {
match self {
Self::Variant1 => {
::stef::buf::encode_id(w, 1);
}
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<Self> {
match ::stef::buf::decode_id(r)? {
Expand Down Expand Up @@ -549,7 +562,7 @@ mod tests {
})?,
})
}
id => Err(Error::UnknownVariant(id)),
id => Err(::stef::buf::Error::UnknownVariant(id)),
}
}
}
Expand Down Expand Up @@ -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);
Expand Down
31 changes: 23 additions & 8 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
@@ -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(
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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! {},
}
Expand All @@ -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,)*
Expand Down Expand Up @@ -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
Expand All @@ -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! {},
}
Expand Down Expand Up @@ -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) }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
Expand Down
Loading

0 comments on commit ed24491

Please sign in to comment.