Skip to content

Commit

Permalink
fix sometimes a negative integer cannot be parsed correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Dec 16, 2023
1 parent edf532f commit 1787b51
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "educe"
version = "0.5.8"
version = "0.5.9"
authors = ["Magic Len <[email protected]>"]
edition = "2021"
rust-version = "1.60"
Expand Down
34 changes: 20 additions & 14 deletions src/common/int.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
use syn::{spanned::Spanned, Expr, Lit, Meta, MetaNameValue};
use syn::{spanned::Spanned, Expr, Lit, Meta, MetaNameValue, UnOp};

use super::path::path_to_string;

#[inline]
pub(crate) fn meta_name_value_2_isize(name_value: &MetaNameValue) -> syn::Result<isize> {
if let Expr::Lit(lit) = &name_value.value {
match &lit.lit {
match &name_value.value {
Expr::Lit(lit) => match &lit.lit {
Lit::Str(lit) => {
return lit
.value()
.parse::<isize>()
.map_err(|error| syn::Error::new(lit.span(), error))
},
Lit::Int(lit) => {
return lit
.base10_digits()
.parse::<isize>()
.map_err(|error| syn::Error::new(lit.span(), error))
},
Lit::Int(lit) => return lit.base10_parse(),
_ => (),
}
},
Expr::Unary(unary) => {
if let UnOp::Neg(_) = unary.op {
if let Expr::Lit(lit) = unary.expr.as_ref() {
if let Lit::Int(lit) = &lit.lit {
let s = format!("-{}", lit.base10_digits());

return s
.parse::<isize>()
.map_err(|error| syn::Error::new(lit.span(), error));
}
}
}
},
_ => (),
}

Err(syn::Error::new(
Expand All @@ -39,10 +48,7 @@ pub(crate) fn meta_2_isize(meta: &Meta) -> syn::Result<isize> {
Lit::Str(lit) => {
lit.value().parse::<isize>().map_err(|error| syn::Error::new(lit.span(), error))
},
Lit::Int(lit) => lit
.base10_digits()
.parse::<isize>()
.map_err(|error| syn::Error::new(lit.span(), error)),
Lit::Int(lit) => lit.base10_parse(),
_ => Err(syn::Error::new(lit.span(), "not an integer")),
}
},
Expand Down

0 comments on commit 1787b51

Please sign in to comment.