Skip to content

Commit

Permalink
fix: compile more schemas and fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Oct 14, 2023
1 parent d8f5e43 commit ba90911
Show file tree
Hide file tree
Showing 37 changed files with 494 additions and 102 deletions.
30 changes: 17 additions & 13 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
#body
}
Expand Down Expand Up @@ -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<str>::decode(r) },
DataType::BoxBytes => quote! { Box<[u8]>::decode(r) },
DataType::BoxString => quote! { Box::<str>::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));
Expand Down
62 changes: 43 additions & 19 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -60,6 +65,9 @@ fn compile_module(
quote! {
#comment
pub mod #name {
#[allow(unused_imports)]
use ::stef::buf::{Decode, Encode};

#(#definitions)*
}
}
Expand All @@ -81,7 +89,7 @@ fn compile_struct(

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

quote! {
#comment
#[derive(Clone, Debug, PartialEq, PartialOrd)]
#[derive(Clone, Debug, PartialEq)]
pub enum #name #generics {
#(#variants,)*
}
Expand Down Expand Up @@ -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<str> },
Expand Down Expand Up @@ -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);
Expand All @@ -397,16 +410,18 @@ 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<u8>,
pub field3: (bool, [i16; 4]),
}
#[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,
Expand Down Expand Up @@ -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<Self> {
let mut field1: Option<u32> = None;
let mut field2: Option<Vec<u8>> = None;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
"#};
Expand Down
6 changes: 3 additions & 3 deletions crates/stef-build/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
5 changes: 4 additions & 1 deletion crates/stef-build/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
Ok(Self)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ba90911

Please sign in to comment.