Skip to content

Commit

Permalink
feat(value,entry): i128 for integers, autoformat safe KdlEntryFormat (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rcgoodfellow authored Nov 30, 2024
1 parent 11e1192 commit 0595955
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
15 changes: 14 additions & 1 deletion src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,17 @@ impl KdlEntry {

/// Auto-formats this entry.
pub fn autoformat(&mut self) {
self.format = None;
// TODO once MSRV allows:
//self.format.take_if(|f| !f.autoformat_keep);
if !self
.format
.as_ref()
.map(|f| f.autoformat_keep)
.unwrap_or(false)
{
self.format = None
}

if let Some(name) = &mut self.name {
name.autoformat();
}
Expand Down Expand Up @@ -265,6 +275,8 @@ pub struct KdlEntryFormat {
pub after_key: String,
/// Whitespace and comments between an entry's equals sign and its value.
pub after_eq: String,
/// Do not clobber this format during autoformat
pub autoformat_keep: bool,
}

#[cfg(test)]
Expand Down Expand Up @@ -378,6 +390,7 @@ mod test {
after_ty: "".into(),
after_key: "".into(),
after_eq: "".into(),
autoformat_keep: false
}),
ty: Some("\"m\\\"eh\"".parse()?),
value: KdlValue::Integer(0xdeadbeef),
Expand Down
15 changes: 8 additions & 7 deletions src/v2_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1295,7 +1295,7 @@ fn integer_test() {
}

/// `integer := digit (digit | '_')*`
fn integer_base(input: &mut Input<'_>) -> PResult<i64> {
fn integer_base(input: &mut Input<'_>) -> PResult<i128> {
(
digit1,
cut_err(repeat(
Expand All @@ -1321,7 +1321,7 @@ fn hex(input: &mut Input<'_>) -> PResult<KdlValue> {
),
))
.try_map(|(l, r): (&str, Vec<&str>)| {
i64::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 16)
i128::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 16)
.map(|x| x * mult)
.map(KdlValue::Integer)
})
Expand All @@ -1345,8 +1345,9 @@ fn test_hex() {
KdlValue::Integer(0xdeadbeef123)
);
assert!(
hex.parse(new_input("0xABCDEF0123456789abcdef")).is_err(),
"i64 overflow"
hex.parse(new_input("0xABCDEF0123456789abcdef0123456789"))
.is_err(),
"i128 overflow"
);
assert!(hex.parse(new_input("0x_deadbeef123")).is_err());

Expand All @@ -1365,7 +1366,7 @@ fn octal(input: &mut Input<'_>) -> PResult<KdlValue> {
),
))
.try_map(|(l, r): (&str, Vec<&str>)| {
i64::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 8)
i128::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 8)
.map(|x| x * mult)
.map(KdlValue::Integer)
})
Expand Down Expand Up @@ -1395,7 +1396,7 @@ fn binary(input: &mut Input<'_>) -> PResult<KdlValue> {
cut_err(
(alt(("0", "1")), repeat(0.., alt(("0", "1", "_")))).try_map(
move |(x, xs): (&str, Vec<&str>)| {
i64::from_str_radix(&format!("{x}{}", str::replace(&xs.join(""), "_", "")), 2)
i128::from_str_radix(&format!("{x}{}", str::replace(&xs.join(""), "_", "")), 2)
.map(|x| x * mult)
.map(KdlValue::Integer)
},
Expand Down Expand Up @@ -1426,7 +1427,7 @@ fn test_binary() {
assert!(binary.parse(new_input("123")).is_err());
}

fn sign(input: &mut Input<'_>) -> PResult<i64> {
fn sign(input: &mut Input<'_>) -> PResult<i128> {
let sign = opt(alt(('+', '-'))).parse_next(input)?;
let mult = if let Some(sign) = sign {
if sign == '+' {
Expand Down
14 changes: 7 additions & 7 deletions src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum KdlValue {

/// A non-float [KDL
/// Number](https://github.com/kdl-org/kdl/blob/main/SPEC.md#number)
Integer(i64),
Integer(i128),

/// A floating point [KDL
/// Number](https://github.com/kdl-org/kdl/blob/main/SPEC.md#number)
Expand Down Expand Up @@ -76,8 +76,8 @@ impl std::hash::Hash for KdlValue {
*val
};
// Good enough to be close-ish for our purposes.
(val.trunc() as i64).hash(state);
(val.fract() as i64).hash(state);
(val.trunc() as i128).hash(state);
(val.fract() as i128).hash(state);
}
KdlValue::Bool(val) => val.hash(state),
KdlValue::Null => core::mem::discriminant(self).hash(state),
Expand Down Expand Up @@ -121,9 +121,9 @@ impl KdlValue {
}
}

/// Returns `Some(i64)` if the `KdlValue` is a [`KdlValue::Integer`],
/// Returns `Some(i128)` if the `KdlValue` is a [`KdlValue::Integer`],
/// otherwise returns `None`.
pub fn as_integer(&self) -> Option<i64> {
pub fn as_integer(&self) -> Option<i128> {
use KdlValue::*;
match self {
Integer(i) => Some(*i),
Expand Down Expand Up @@ -219,8 +219,8 @@ impl KdlValue {
}
}

impl From<i64> for KdlValue {
fn from(value: i64) -> Self {
impl From<i128> for KdlValue {
fn from(value: i128) -> Self {
KdlValue::Integer(value)
}
}
Expand Down

0 comments on commit 0595955

Please sign in to comment.