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

Lint without default features #59

Merged
merged 3 commits into from
Oct 2, 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
23 changes: 23 additions & 0 deletions struct-patch-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ struct Patch {
fields: Vec<Field>,
}

#[cfg(feature = "op")]
enum Addable {
Disable,
AddTriat,
#[cfg(feature = "op")]
AddFn(Ident),
}

Expand All @@ -43,6 +45,7 @@ struct Field {
ty: Type,
attributes: Vec<TokenStream>,
retyped: bool,
#[cfg(feature = "op")]
addable: Addable,
}

Expand Down Expand Up @@ -400,6 +403,8 @@ impl Field {
let mut attributes = vec![];
let mut field_type = None;
let mut skip = false;

#[cfg(feature = "op")]
let mut addable = Addable::Disable;

for attr in attrs {
Expand Down Expand Up @@ -432,15 +437,31 @@ impl Field {
let expr: LitStr = meta.value()?.parse()?;
field_type = Some(expr.parse()?)
}
#[cfg(feature = "op")]
ADDABLE => {
// #[patch(addable)]
addable = Addable::AddTriat;
}
#[cfg(not(feature = "op"))]
ADDABLE => {
return Err(syn::Error::new(
ident.span(),
"`addable` needs `op` feature"
));
}
#[cfg(feature = "op")]
ADD => {
// #[patch(add=fn)]
let f: Ident = meta.value()?.parse()?;
addable = Addable::AddFn(f);
}
#[cfg(not(feature = "op"))]
ADD => {
return Err(syn::Error::new(
ident.span(),
"`add` needs `op` feature"
));
}
_ => {
return Err(meta.error(format_args!(
"unknown patch field attribute `{}`",
Expand All @@ -460,6 +481,7 @@ impl Field {
retyped: field_type.is_some(),
ty: field_type.unwrap_or(ty),
attributes,
#[cfg(feature = "op")]
addable,
}))
}
Expand Down Expand Up @@ -538,6 +560,7 @@ mod tests {
.unwrap(),
attributes: vec![],
retyped: true,
#[cfg(feature = "op")]
addable: Addable::Disable,
}],
};
Expand Down
24 changes: 13 additions & 11 deletions struct-patch/examples/op.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use struct_patch::Patch;

#[cfg(feature = "op")]
fn str_concat(a: String, b: String) -> String {
format!("{}, {}", a, b)
}
#[derive(Clone, Debug, Default, Patch, PartialEq)]
#[patch(attribute(derive(Clone, Debug, Default)))]
struct Item {
field_complete: bool,
#[patch(addable)]
field_int: usize,
#[patch(add=str_concat)]
field_string: String,
}

#[cfg(feature = "op")]
fn main() {
use struct_patch::Patch;

#[derive(Clone, Debug, Default, Patch, PartialEq)]
#[patch(attribute(derive(Clone, Debug, Default)))]
struct Item {
field_complete: bool,
#[patch(addable)]
field_int: usize,
#[patch(add=str_concat)]
field_string: String,
}

let mut item = Item::default();

let mut patch: ItemPatch = Item::new_empty_patch();
Expand Down
1 change: 1 addition & 0 deletions struct-patch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ mod tests {

#[test]
fn test_derive() {
#[allow(dead_code)]
#[derive(Patch)]
#[patch(attribute(derive(Copy, Clone, PartialEq, Debug)))]
struct Item;
Expand Down