Skip to content

Commit

Permalink
Improved macro check
Browse files Browse the repository at this point in the history
Reduced odds of making a mistake while reporting deactivated dynamic field features.
Also added feature flags to tests.
  • Loading branch information
LoopyAshy committed Oct 23, 2022
1 parent 0f82f57 commit a563617
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 17 deletions.
3 changes: 3 additions & 0 deletions crates/rune-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ description = """
Helper macros for Rune.
"""

[features]
dynamic_fields = []

[dependencies]
syn = { version = "1.0.82", features = ["full"] }
quote = "1.0.10"
Expand Down
12 changes: 1 addition & 11 deletions crates/rune-macros/src/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,7 @@ impl Derive {

let meta_fields = match attrs.meta_fields {
Some(meta_fields) => {
if cfg!(feature = "dynamic_fields") {
quote! {#meta_fields}
} else {
return Err(vec![syn::Error::new(
meta_fields.span(),
format!(
"attempted to set dynamic_fields to `{}` while the feature `dynamic_fields` is disabled.",
meta_fields
),
)]);
}
quote! {#meta_fields}
}
None => quote! {Never},
};
Expand Down
36 changes: 33 additions & 3 deletions crates/rune-macros/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,9 +504,39 @@ impl Context {
})) if path == DYNAMIC_FIELDS => {
attrs.meta_fields = Some(match s.value().as_str() {
"never" => format_ident!("Never"),
"only" => format_ident!("Only"),
"first" => format_ident!("First"),
"last" => format_ident!("Last"),
"only" => {
if cfg!(feature = "dynamic_fields") {
format_ident!("Only")
} else {
self.errors.push(syn::Error::new_spanned(
s,
"attempted to set dynamic_fields to `only` while the feature `dynamic_fields` is disabled."
));
return None;
}
}
"first" => {
if cfg!(feature = "dynamic_fields") {
format_ident!("First")
} else {
self.errors.push(syn::Error::new_spanned(
s,
"attempted to set dynamic_fields to `first` while the feature `dynamic_fields` is disabled."
));
return None;
}
}
"last" => {
if cfg!(feature = "dynamic_fields") {
format_ident!("Last")
} else {
self.errors.push(syn::Error::new_spanned(
s,
"attempted to set dynamic_fields to `last` while the feature `dynamic_fields` is disabled."
));
return None;
}
}
_ => {
self.errors.push(syn::Error::new_spanned(
s,
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ default = ["emit"]
emit = ["codespan-reporting"]
bench = []
workspace = ["toml", "toml-spanned-value", "semver", "relative-path", "serde-hashkey"]
dynamic_fields = []
dynamic_fields = ["rune-macros/dynamic_fields"]

[dependencies]
thiserror = "1.0.30"
Expand Down
3 changes: 1 addition & 2 deletions tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ path = "test.rs"
[features]
default = ["full"]
full = ["rune-modules/full"]
dynamic_fields = ["rune/dynamic_fields"]

[dependencies]
thiserror = "1.0.30"
futures-executor = "0.3.0"

rune = { path = "../crates/rune" }
rune = { path = "../crates/rune", features = ["dynamic_fields"] }
rune-modules = { path = "../crates/rune-modules", features = ["capture-io"] }

0 comments on commit a563617

Please sign in to comment.