Skip to content

Commit

Permalink
feat: create playground crate and fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
dnaka91 committed Sep 29, 2023
1 parent 8f5a4ec commit b55862f
Show file tree
Hide file tree
Showing 32 changed files with 293 additions and 302 deletions.
193 changes: 71 additions & 122 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions crates/stef-benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ readme.workspace = true
homepage.workspace = true
repository.workspace = true
license.workspace = true
include = ["src/**/*"]
publish = false

[[bench]]
Expand All @@ -20,10 +19,10 @@ name = "varint"
harness = false

[dependencies]
mimalloc = "0.1.38"
mimalloc = "0.1.39"
stef-parser = { path = "../stef-parser" }

[dev-dependencies]
criterion = "0.5.1"
indoc = "2.0.3"
indoc = "2.0.4"
serde = "1.0.188"
12 changes: 6 additions & 6 deletions crates/stef-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ license.workspace = true

[dependencies]
miette.workspace = true
proc-macro2 = { version = "1.0.66", default-features = false }
prettyplease = "0.2.15"
proc-macro2 = { version = "1.0.67", default-features = false }
quote = { version = "1.0.33", default-features = false }
stef-parser = { path = "../stef-parser" }
thiserror = "1.0.48"
syn = "2.0.37"
thiserror = "1.0.49"

[dev-dependencies]
indoc = "2.0.3"
insta = { version = "1.31.0", features = ["glob"] }
indoc = "2.0.4"
insta = { version = "1.33.0", features = ["glob"] }
pretty_assertions = "1.4.0"
prettyplease = "0.2.15"
stef = { path = "../stef" }
syn = "2.0.32"
12 changes: 6 additions & 6 deletions crates/stef-build/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn compile_struct(
quote! {
#field_vars

loop{
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
#field_matches
Expand Down Expand Up @@ -90,7 +90,7 @@ fn compile_variant(
#id => {
#field_vars

loop{
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
#field_matches
Expand Down Expand Up @@ -179,10 +179,10 @@ fn compile_field_assigns(fields: &Fields<'_>) -> TokenStream {
if matches!(named.ty, DataType::Option(_)) {
quote! { #name }
} else {
quote! { #name: #name.unwrap_or_else(|| ::stef::buf::Error::MissingField {
quote! { #name: #name.ok_or(::stef::buf::Error::MissingField {
id: #id,
name: Some(#name_lit),
}) }
})? }
}
});

Expand All @@ -196,10 +196,10 @@ fn compile_field_assigns(fields: &Fields<'_>) -> TokenStream {
if matches!(unnamed.ty, DataType::Option(_)) {
quote! { #name }
} else {
quote! { #name.unwrap_or_else(|| ::stef::buf::Error::MissingField {
quote! { #name.ok_or(::stef::buf::Error::MissingField {
id: #id,
name: None,
}) }
})? }
}
});

Expand Down
38 changes: 15 additions & 23 deletions crates/stef-build/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -426,20 +426,20 @@ mod tests {
}
Ok(Self {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
field3: field3
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 3,
name: Some("field3"),
}),
})?,
})
}
}
Expand Down Expand Up @@ -498,15 +498,7 @@ mod tests {
impl ::stef::Decode for Sample {
fn decode(r: &mut impl ::stef::Buf) -> ::stef::buf::Result<Self> {
match ::stef::buf::decode_id(r)? {
1 => {
loop {
match ::stef::buf::decode_id(r)? {
::stef::buf::END_MARKER => break,
_ => continue,
}
}
Ok(Self::Variant1)
}
1 => Ok(Self::Variant1),
2 => {
let mut n0: Option<u32> = None;
let mut n1: Option<u8> = None;
Expand All @@ -521,15 +513,15 @@ mod tests {
Ok(
Self::Variant2(
n0
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
}),
})?,
n1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
}),
})?,
),
)
}
Expand All @@ -546,15 +538,15 @@ mod tests {
}
Ok(Self::Variant3 {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
})
}
id => Err(Error::UnknownVariant(id)),
Expand Down
23 changes: 20 additions & 3 deletions crates/stef-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,36 @@ pub enum Error {
source: std::io::Error,
file: PathBuf,
},
#[error("failed parsing schema from {file:?}: {message}")]
Parse { message: String, file: PathBuf },
}

pub fn compile(schemas: &[impl AsRef<Path>], _includes: &[impl AsRef<Path>]) -> Result<()> {
let out_dir = PathBuf::from(std::env::var_os("OUT_DIR").unwrap());

for schema in schemas {
let input = std::fs::read_to_string(schema).map_err(|e| Error::Read {
let path = schema.as_ref();

let input = std::fs::read_to_string(path).map_err(|e| Error::Read {
source: e,
file: schema.as_ref().to_owned(),
file: path.to_owned(),
})?;

let schema = Schema::parse(&input).unwrap();
let schema = Schema::parse(&input).map_err(|e| Error::Parse {
message: e.to_string(),
file: path.to_owned(),
})?;
let code = definition::compile_schema(&schema);
let code = prettyplease::unparse(&syn::parse2(code).unwrap());

println!("{code}");

let out_file = out_dir.join(format!(
"{}.rs",
path.file_stem().unwrap().to_str().unwrap()
));

std::fs::write(out_file, code).unwrap();
}

Ok(())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ impl ::stef::Decode for Sample {
Ok(
Self::Two(
n0
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
}),
})?,
n1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
}),
})?,
),
)
}
Expand All @@ -84,15 +84,15 @@ impl ::stef::Decode for Sample {
}
Ok(Self::Three {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
})
}
id => Err(Error::UnknownVariant(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@ where
Ok(
Self::Two(
n0
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
}),
})?,
n1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
}),
})?,
),
)
}
Expand All @@ -83,15 +83,15 @@ where
}
Ok(Self::Three {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
})
}
id => Err(Error::UnknownVariant(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ impl ::stef::Decode for Sample {
Ok(
Self::Two(
n0
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
}),
})?,
n1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
}),
})?,
),
)
}
Expand All @@ -79,15 +79,15 @@ impl ::stef::Decode for Sample {
}
Ok(Self::Three {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
})
}
id => Err(Error::UnknownVariant(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ where
Ok(
Self::Two(
n0
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: None,
}),
})?,
n1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: None,
}),
})?,
n2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 3,
name: None,
}),
})?,
),
)
}
Expand All @@ -95,20 +95,20 @@ where
}
Ok(Self::Three {
field1: field1
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("field1"),
}),
})?,
field2: field2
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 2,
name: Some("field2"),
}),
})?,
field3: field3
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 3,
name: Some("field3"),
}),
})?,
})
}
id => Err(Error::UnknownVariant(id)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ pub mod a {
}
Ok(Self {
value: value
.unwrap_or_else(|| ::stef::buf::Error::MissingField {
.ok_or(::stef::buf::Error::MissingField {
id: 1,
name: Some("value"),
}),
})?,
})
}
}
Expand Down
Loading

0 comments on commit b55862f

Please sign in to comment.