Skip to content

Commit

Permalink
use saturating sub in proc macro
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Dec 10, 2023
1 parent 2a9e153 commit aeb86ae
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
52 changes: 41 additions & 11 deletions derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,12 @@ pub fn _debug_current_token(source: &mut Peekable<impl Iterator<Item = TokenTree
}

pub fn next_lifetime<T: Iterator<Item = TokenTree>>(source: &mut Peekable<T>) -> Option<Lifetime> {
let Some (TokenTree::Punct(punct)) = source.peek() else { return None; };
let '\'' = punct.as_char() else { return None; };
let Some(TokenTree::Punct(punct)) = source.peek() else {
return None;
};
let '\'' = punct.as_char() else {
return None;
};

let _ = source.next();
Some(Lifetime {
Expand Down Expand Up @@ -592,7 +596,15 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>

let Some(_) = next_exact_punct(&mut source, ";") else {
// This is an unbounded array, legal at end for unsized types
return Some(Type { ident: Category::Array { content_type: Box::new(next.clone()), len: None }, wraps: Some(vec![next]), ref_type: None, as_other: None })
return Some(Type {
ident: Category::Array {
content_type: Box::new(next.clone()),
len: None,
},
wraps: Some(vec![next]),
ref_type: None,
as_other: None,
});
};

//need to cover both the const generic and literal case
Expand Down Expand Up @@ -662,7 +674,10 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
source: &mut Peekable<T>,
) -> Option<Type> {
let mut tmp = source.clone();
let (Some(_), Some(_)) = ( next_exact_punct(&mut tmp, "-"), next_exact_punct(&mut tmp, ">")) else {
let (Some(_), Some(_)) = (
next_exact_punct(&mut tmp, "-"),
next_exact_punct(&mut tmp, ">"),
) else {
return None;
};
drop(tmp);
Expand Down Expand Up @@ -719,7 +734,7 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
};
let true = matches!(ident.to_string().as_str(), "fn" | "FnOnce" | "FnMut" | "Fn") else {
return None;
};
};
let tok_str = source.next().unwrap().to_string();

match tok_str.as_str() {
Expand Down Expand Up @@ -770,10 +785,10 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
source: &mut Peekable<T>,
) -> Option<Type> {
let Some(TokenTree::Ident(ident)) = source.peek() else {
return None
return None;
};
let true = matches!(ident.to_string().as_str(), "impl" | "dyn") else {
return None;
return None;
};
match source.next().unwrap().to_string().as_str() {
"impl" => {
Expand Down Expand Up @@ -841,7 +856,14 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
};

let None = next_exact_punct(source, "\'") else {
return Some(Type{ ident: Category::Lifetime { path: next_ident(source).expect("Need lifetime name") }, wraps: None, ref_type: None, as_other: None })
return Some(Type {
ident: Category::Lifetime {
path: next_ident(source).expect("Need lifetime name"),
},
wraps: None,
ref_type: None,
as_other: None,
});
};

let ref_type = match next_exact_punct(&mut source, "&") {
Expand Down Expand Up @@ -909,7 +931,10 @@ fn next_type<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
let mut ty = next_ident(&mut source).unwrap_or_default();
while let Some(TokenTree::Punct(_)) = source.peek() {
let mut tmp = source.clone();
let (Some(_), Some(_)) = ( next_exact_punct(&mut tmp, ":"), next_exact_punct(&mut tmp, ":")) else {
let (Some(_), Some(_)) = (
next_exact_punct(&mut tmp, ":"),
next_exact_punct(&mut tmp, ":"),
) else {
break;
};
drop(tmp);
Expand Down Expand Up @@ -1015,7 +1040,7 @@ fn next_attribute<T: Iterator<Item = TokenTree>>(
// all attributes, even doc-comments, starts with "#"
let next_attr_punct = next_punct(&mut source);
let Some("#") = next_attr_punct.as_deref() else {
return None
return None;
};

let mut attr_group = next_group(&mut source)
Expand Down Expand Up @@ -1213,7 +1238,12 @@ fn next_enum<T: Iterator<Item = TokenTree> + Clone>(mut source: &mut Peekable<T>
let ty = next_type(&mut body);
let Some(ty) = ty else {
variants.push(Field {
ty: Type { ident: Category::None, wraps: None, ref_type: None, as_other: None },
ty: Type {
ident: Category::None,
wraps: None,
ref_type: None,
as_other: None,
},
attributes,
vis: Visibility::Public,
field_name: Some(variant_name),
Expand Down
2 changes: 1 addition & 1 deletion derive/src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub fn derive_ser_json_enum(enum_: &Enum) -> TokenStream {
} => {
let mut items = String::new();
let mut field_names = vec![];
let last = contents.fields.len() - 1;
let last = contents.fields.len().saturating_sub(1);
for (index, field) in contents.fields.iter().enumerate() {
if let Some(name) = &&field.field_name {
let proxied_field = ser_proxy_guard(name, field);
Expand Down
8 changes: 8 additions & 0 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ fn test_trailing_comma() {
A
}
}

// https://github.com/not-fl3/nanoserde/issues/88
#[test]
fn test_empty_brackets() {
#[rustfmt::skip]
#[derive(SerJson, DeJson, SerBin, DeBin, SerRon, DeRon)]
enum Message { Goodbye, Greeting{} }
}

0 comments on commit aeb86ae

Please sign in to comment.