Skip to content

Commit 0595955

Browse files
authored
feat(value,entry): i128 for integers, autoformat safe KdlEntryFormat (#91)
1 parent 11e1192 commit 0595955

File tree

3 files changed

+29
-15
lines changed

3 files changed

+29
-15
lines changed

src/entry.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,17 @@ impl KdlEntry {
163163

164164
/// Auto-formats this entry.
165165
pub fn autoformat(&mut self) {
166-
self.format = None;
166+
// TODO once MSRV allows:
167+
//self.format.take_if(|f| !f.autoformat_keep);
168+
if !self
169+
.format
170+
.as_ref()
171+
.map(|f| f.autoformat_keep)
172+
.unwrap_or(false)
173+
{
174+
self.format = None
175+
}
176+
167177
if let Some(name) = &mut self.name {
168178
name.autoformat();
169179
}
@@ -265,6 +275,8 @@ pub struct KdlEntryFormat {
265275
pub after_key: String,
266276
/// Whitespace and comments between an entry's equals sign and its value.
267277
pub after_eq: String,
278+
/// Do not clobber this format during autoformat
279+
pub autoformat_keep: bool,
268280
}
269281

270282
#[cfg(test)]
@@ -378,6 +390,7 @@ mod test {
378390
after_ty: "".into(),
379391
after_key: "".into(),
380392
after_eq: "".into(),
393+
autoformat_keep: false
381394
}),
382395
ty: Some("\"m\\\"eh\"".parse()?),
383396
value: KdlValue::Integer(0xdeadbeef),

src/v2_parser.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,7 +1295,7 @@ fn integer_test() {
12951295
}
12961296

12971297
/// `integer := digit (digit | '_')*`
1298-
fn integer_base(input: &mut Input<'_>) -> PResult<i64> {
1298+
fn integer_base(input: &mut Input<'_>) -> PResult<i128> {
12991299
(
13001300
digit1,
13011301
cut_err(repeat(
@@ -1321,7 +1321,7 @@ fn hex(input: &mut Input<'_>) -> PResult<KdlValue> {
13211321
),
13221322
))
13231323
.try_map(|(l, r): (&str, Vec<&str>)| {
1324-
i64::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 16)
1324+
i128::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 16)
13251325
.map(|x| x * mult)
13261326
.map(KdlValue::Integer)
13271327
})
@@ -1345,8 +1345,9 @@ fn test_hex() {
13451345
KdlValue::Integer(0xdeadbeef123)
13461346
);
13471347
assert!(
1348-
hex.parse(new_input("0xABCDEF0123456789abcdef")).is_err(),
1349-
"i64 overflow"
1348+
hex.parse(new_input("0xABCDEF0123456789abcdef0123456789"))
1349+
.is_err(),
1350+
"i128 overflow"
13501351
);
13511352
assert!(hex.parse(new_input("0x_deadbeef123")).is_err());
13521353

@@ -1365,7 +1366,7 @@ fn octal(input: &mut Input<'_>) -> PResult<KdlValue> {
13651366
),
13661367
))
13671368
.try_map(|(l, r): (&str, Vec<&str>)| {
1368-
i64::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 8)
1369+
i128::from_str_radix(&format!("{l}{}", str::replace(&r.join(""), "_", "")), 8)
13691370
.map(|x| x * mult)
13701371
.map(KdlValue::Integer)
13711372
})
@@ -1395,7 +1396,7 @@ fn binary(input: &mut Input<'_>) -> PResult<KdlValue> {
13951396
cut_err(
13961397
(alt(("0", "1")), repeat(0.., alt(("0", "1", "_")))).try_map(
13971398
move |(x, xs): (&str, Vec<&str>)| {
1398-
i64::from_str_radix(&format!("{x}{}", str::replace(&xs.join(""), "_", "")), 2)
1399+
i128::from_str_radix(&format!("{x}{}", str::replace(&xs.join(""), "_", "")), 2)
13991400
.map(|x| x * mult)
14001401
.map(KdlValue::Integer)
14011402
},
@@ -1426,7 +1427,7 @@ fn test_binary() {
14261427
assert!(binary.parse(new_input("123")).is_err());
14271428
}
14281429

1429-
fn sign(input: &mut Input<'_>) -> PResult<i64> {
1430+
fn sign(input: &mut Input<'_>) -> PResult<i128> {
14301431
let sign = opt(alt(('+', '-'))).parse_next(input)?;
14311432
let mult = if let Some(sign) = sign {
14321433
if sign == '+' {

src/value.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub enum KdlValue {
88

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

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

124-
/// Returns `Some(i64)` if the `KdlValue` is a [`KdlValue::Integer`],
124+
/// Returns `Some(i128)` if the `KdlValue` is a [`KdlValue::Integer`],
125125
/// otherwise returns `None`.
126-
pub fn as_integer(&self) -> Option<i64> {
126+
pub fn as_integer(&self) -> Option<i128> {
127127
use KdlValue::*;
128128
match self {
129129
Integer(i) => Some(*i),
@@ -219,8 +219,8 @@ impl KdlValue {
219219
}
220220
}
221221

222-
impl From<i64> for KdlValue {
223-
fn from(value: i64) -> Self {
222+
impl From<i128> for KdlValue {
223+
fn from(value: i128) -> Self {
224224
KdlValue::Integer(value)
225225
}
226226
}

0 commit comments

Comments
 (0)