Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade syn, Migrate SerializeHierarchy to PathSerialize, PathDeserialize, PathIntrospect #987

Merged
merged 11 commits into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
408 changes: 226 additions & 182 deletions Cargo.lock

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ members = [
"crates/opn",
"crates/parameter_tester",
"crates/parameters",
"crates/path_serde",
"crates/path_serde_derive",
"crates/projection",
"crates/repository",
"crates/serialize_hierarchy",
"crates/serialize_hierarchy_derive",
"crates/source_analyzer",
"crates/spl_network",
"crates/spl_network_messages",
Expand Down Expand Up @@ -142,6 +142,8 @@ opusfile-ng = "0.1.0"
ordered-float = "3.1.0"
parameters = { path = "crates/parameters" }
parking_lot = "0.12.1"
path_serde = { path = "crates/path_serde" }
path_serde_derive = { path = "crates/path_serde_derive" }
petgraph = "0.6.2"
png = "0.17.6"
proc-macro-error = "1.0.4"
Expand All @@ -161,16 +163,14 @@ serde_bytes = "0.11.8"
serde_derive = "1.0.195"
serde_json = "1.0.107"
serde_test = "1.0.152"
serialize_hierarchy = { path = "crates/serialize_hierarchy" }
serialize_hierarchy_derive = { path = "crates/serialize_hierarchy_derive" }
sha2 = "0.10.8"
smallvec = "1.9.0"
source_analyzer = { path = "crates/source_analyzer" }
spl_network = { path = "crates/spl_network" }
spl_network_messages = { path = "crates/spl_network_messages" }
splines = { version = "4.2.0", features = ["serde"] }
structopt = "0.3.26"
syn = { version = "1.0.101", features = ["full", "extra-traits"] }
syn = { version = "2.0.60", features = ["full", "extra-traits"] }
systemd = "0.10.0"
tempfile = "3.3.0"
thiserror = "1.0.37"
Expand Down
68 changes: 27 additions & 41 deletions crates/approx_derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
use proc_macro2::TokenStream;
use proc_macro_error::{abort, proc_macro_error, OptionExt, ResultExt};
use proc_macro_error::{abort, proc_macro_error};
use quote::quote;
use syn::{
parse_macro_input,
punctuated::{self},
Attribute, Data, DeriveInput, Lit, Meta, MetaNameValue, NestedMeta, Type,
};
use syn::{parse_macro_input, Attribute, Data, DeriveInput, Result, Type};

#[proc_macro_derive(AbsDiffEq, attributes(abs_diff_eq))]
#[proc_macro_error]
pub fn abs_diff_eq(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(input as DeriveInput);
generate_abs_diff_eq(input).into()
derive_abs_diff_eq(input)
.unwrap_or_else(syn::Error::into_compile_error)
.into()
}

fn generate_abs_diff_eq(input: DeriveInput) -> TokenStream {
fn derive_abs_diff_eq(input: DeriveInput) -> Result<TokenStream> {
let fields = match input.data {
Data::Struct(data) => data.fields,
Data::Enum(data) => abort!(
Expand All @@ -26,7 +24,7 @@ fn generate_abs_diff_eq(input: DeriveInput) -> TokenStream {
"`AbsDiffEq` can only be derived for `struct`",
),
};
let epsilon = extract_epsilon(&input.attrs).expect_or_abort("`epsilon` not specified");
let epsilon_type = extract_epsilon_type(&input.attrs)?;
let name = input.ident;

let conditions = fields.into_iter().map(|field| {
Expand All @@ -39,9 +37,9 @@ fn generate_abs_diff_eq(input: DeriveInput) -> TokenStream {
}
});

quote! {
Ok(quote! {
impl approx::AbsDiffEq for #name {
type Epsilon = #epsilon;
type Epsilon = #epsilon_type;

fn default_epsilon() -> Self::Epsilon {
Self::Epsilon::default_epsilon()
Expand All @@ -52,40 +50,28 @@ fn generate_abs_diff_eq(input: DeriveInput) -> TokenStream {
true
}
}
}
})
}

fn extract_epsilon(attrs: &[Attribute]) -> Option<Type> {
attrs
.iter()
.filter_map(parse_meta_items)
.flatten()
.find_map(|meta| match meta {
NestedMeta::Meta(Meta::NameValue(MetaNameValue { path, lit, .. }))
if path.is_ident("epsilon") =>
{
let string = match lit {
Lit::Str(string) => string,
_ => abort!(lit, "expected string literal"),
};
let epsilon = string
.parse()
.expect_or_abort("failed to parse epsilon type");
Some(epsilon)
}
_ => None,
})
}
fn extract_epsilon_type(attributes: &[Attribute]) -> Result<Option<Type>> {
let mut epsilon_type = None;

fn parse_meta_items(attribute: &Attribute) -> Option<punctuated::IntoIter<NestedMeta>> {
if !attribute.path.is_ident("abs_diff_eq") {
return None;
}
match attribute.parse_meta() {
Ok(Meta::List(meta)) => Some(meta.nested.into_iter()),
Ok(other) => abort!(other, "expected `#[abs_diff_eq(...)]`",),
Err(error) => abort!(error.span(), error.to_string()),
for attribute in attributes {
if !attribute.path().is_ident("abs_diff_eq") {
continue;
}
attribute.parse_nested_meta(|meta| {
if meta.path.is_ident("epsilon_type") {
let value = meta.value()?;
epsilon_type = Some(value.parse()?);
Ok(())
} else {
Err(meta.error("unknown attribute"))
}
})?;
}

Ok(epsilon_type)
}

#[proc_macro_derive(RelativeEq)]
Expand Down
8 changes: 7 additions & 1 deletion crates/code_generation/src/cyclers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,13 @@ fn generate_cycler_instance(cycler: &Cycler) -> TokenStream {

fn generate_database_struct() -> TokenStream {
quote! {
#[derive(Default, serde::Deserialize, serde::Serialize, serialize_hierarchy::SerializeHierarchy)]
#[derive(
Default,
serde::Serialize,
serde::Deserialize,
path_serde::PathSerialize,
path_serde::PathIntrospect,
)]
pub(crate) struct Database {
pub main_outputs: MainOutputs,
pub additional_outputs: AdditionalOutputs,
Expand Down
24 changes: 20 additions & 4 deletions crates/code_generation/src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,34 @@ use quote::{format_ident, quote};
use source_analyzer::{struct_hierarchy::StructHierarchy, structs::Structs};

pub fn generate_structs(structs: &Structs) -> TokenStream {
let parameters = hierarchy_to_token_stream(
&structs.parameters,
format_ident!("Parameters"),
&quote! {
#[derive(
Clone,
Debug,
Default,
serde::Deserialize,
serde::Serialize,
path_serde::PathSerialize,
path_serde::PathDeserialize,
path_serde::PathIntrospect,
)]
},
);

let derives = quote! {
#[derive(
Clone,
Debug,
Default,
serde::Deserialize,
serde::Serialize,
serialize_hierarchy::SerializeHierarchy,
serde::Deserialize,
path_serde::PathSerialize,
path_serde::PathIntrospect,
)]
};
let parameters =
hierarchy_to_token_stream(&structs.parameters, format_ident!("Parameters"), &derives);
let cyclers = structs
.cyclers
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/communication/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ framework = { workspace = true, optional = true}
futures-util = { workspace = true }
log = { workspace = true }
parameters = { workspace = true }
path_serde = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
serialize_hierarchy = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tokio-tungstenite = { workspace = true }
Expand Down
Loading
Loading