Skip to content

Commit 09c2c28

Browse files
authored
chore: refactoring (#140)
1 parent 6656f7e commit 09c2c28

File tree

2 files changed

+14
-39
lines changed

2 files changed

+14
-39
lines changed

src/v2_parser.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -966,13 +966,7 @@ fn unambiguous_ident(input: &mut Input<'_>) -> PResult<()> {
966966
cut_err(
967967
repeat(1.., identifier_char)
968968
.verify_map(|s: String| {
969-
if s == "true"
970-
|| s == "false"
971-
|| s == "null"
972-
|| s == "inf"
973-
|| s == "-inf"
974-
|| s == "nan"
975-
{
969+
if matches!(s.as_str(), "true" | "false" | "null" | "inf" | "-inf" | "nan") {
976970
None
977971
} else {
978972
Some(s)

src/value.rs

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,23 @@ pub enum KdlValue {
2323

2424
impl Eq for KdlValue {}
2525

26+
fn normalize_float(f: &f64) -> f64 {
27+
match f {
28+
_ if f == &f64::INFINITY => f64::MAX,
29+
_ if f == &f64::NEG_INFINITY => -f64::MAX,
30+
// We collapse NaN to 0.0 because we're evil like that.
31+
_ if f.is_nan() => 0.0,
32+
_ => *f,
33+
}
34+
}
35+
2636
impl PartialEq for KdlValue {
2737
fn eq(&self, other: &Self) -> bool {
2838
match (self, other) {
2939
(Self::String(l0), Self::String(r0)) => l0 == r0,
3040
(Self::Integer(l0), Self::Integer(r0)) => l0 == r0,
3141
(Self::Float(l0), Self::Float(r0)) => {
32-
let l0 = if l0 == &f64::INFINITY {
33-
f64::MAX
34-
} else if l0 == &f64::NEG_INFINITY {
35-
-f64::MAX
36-
} else if l0.is_nan() {
37-
// We collapse NaN to 0.0 because we're evil like that.
38-
0.0
39-
} else {
40-
*l0
41-
};
42-
let r0 = if r0 == &f64::INFINITY {
43-
f64::MAX
44-
} else if r0 == &f64::NEG_INFINITY {
45-
-f64::MAX
46-
} else if r0.is_nan() {
47-
// We collapse NaN to 0.0 because we're evil like that.
48-
0.0
49-
} else {
50-
*r0
51-
};
52-
l0 == r0
42+
normalize_float(l0) == normalize_float(r0)
5343
}
5444
(Self::Bool(l0), Self::Bool(r0)) => l0 == r0,
5545
_ => core::mem::discriminant(self) == core::mem::discriminant(other),
@@ -65,16 +55,7 @@ impl std::hash::Hash for KdlValue {
6555
Self::String(val) => val.hash(state),
6656
Self::Integer(val) => val.hash(state),
6757
Self::Float(val) => {
68-
let val = if val == &f64::INFINITY {
69-
f64::MAX
70-
} else if val == &f64::NEG_INFINITY {
71-
-f64::MAX
72-
} else if val.is_nan() {
73-
// We collapse NaN to 0.0 because we're evil like that.
74-
0.0
75-
} else {
76-
*val
77-
};
58+
let val = normalize_float(val);
7859
// Good enough to be close-ish for our purposes.
7960
(val.trunc() as i128).hash(state);
8061
(val.fract() as i128).hash(state);
@@ -183,7 +164,7 @@ pub(crate) fn is_plain_ident(ident: &str) -> bool {
183164
&& !(ident
184165
.chars()
185166
.next()
186-
.map(|c| c == '.' || c == '-' || c == '+')
167+
.map(|c| matches!(c, '.' | '-' | '+'))
187168
== Some(true)
188169
&& ident_bytes.get(1).map(|c| c.is_ascii_digit()) == Some(true))
189170
&& ident != "inf"

0 commit comments

Comments
 (0)