From cfe2dafe5e5e585ea5c060a4ceceb606ec688d27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Wed, 12 Mar 2025 12:03:27 +0100 Subject: [PATCH] Allow skipping incrementing when using skip --- src/parse.rs | 22 +++++++++++++++++----- tests/basics.rs | 3 +++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/parse.rs b/src/parse.rs index 243b3bc..814031f 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -107,10 +107,13 @@ fn fields_from_ast( fields: &syn::punctuated::Punctuated, ) -> Result> { // serde::internals::ast.rs:L183 + let mut index = 0; fields .iter() - .enumerate() - .map(|(index, field)| { + .map(|field| { + let current_index = index; + index += 1; + let mut skip_serializing_if = Skip::None; for attr in &field.attrs { if attr.path().is_ident("serde") { @@ -125,9 +128,18 @@ fn fields_from_ast( skip_serializing_if = Skip::If(syn::parse2(tokens)?); Ok(()) } else if meta.path.is_ident("skip") { - if meta.value().is_ok() { - return Err(meta.error("`skip` does not expect any value")); + if meta.input.peek(syn::token::Paren) { + meta.parse_nested_meta(|skip_meta| { + if !skip_meta.path.is_ident("no_increment") { + Err(skip_meta + .error("`skip` only accepts `no_increment` as value")) + } else { + index -= 1; + Ok(()) + } + })?; } + if !skip_serializing_if.is_none() { return Err(meta .error("Multiple attributes for skip_serializing_if or skip")); @@ -155,7 +167,7 @@ fn fields_from_ast( return Err(Error::new_spanned(fields, "Tuple struct are not supported")); } }, - index, + index: current_index, // TODO: make this... more concise? handle errors? the thing with the spans? skip_serializing_if, }) diff --git a/tests/basics.rs b/tests/basics.rs index 6909bcf..faa44d1 100644 --- a/tests/basics.rs +++ b/tests/basics.rs @@ -59,6 +59,8 @@ mod some_keys { pub number: i32, #[serde(skip)] pub ignored: i32, + #[serde(skip(no_increment))] + pub ignored2: i32, pub bytes: &'a ByteArray<7>, pub string: &'b str, #[serde(skip_serializing_if = "Option::is_none")] @@ -111,6 +113,7 @@ mod some_keys { let value = SomeRefKeys { number: -7, ignored: 0, + ignored2: 0, bytes: &BYTE_ARRAY, string: "so serde", option: None,