Skip to content

Commit 544abaa

Browse files
committed
fixup
1 parent 2762312 commit 544abaa

File tree

1 file changed

+39
-30
lines changed

1 file changed

+39
-30
lines changed

postgres-protocol/src/types/numeric.rs

+39-30
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,33 @@ impl Numeric {
7676
pub fn sign(&self) -> NumericSign {
7777
self.sign
7878
}
79+
80+
fn nan() -> Self {
81+
Self {
82+
sign: NumericSign::NaN,
83+
scale: 0,
84+
weight: 0,
85+
digits: vec![],
86+
}
87+
}
88+
89+
fn infinity() -> Self {
90+
Self {
91+
sign: NumericSign::PositiveInfinity,
92+
scale: 0,
93+
weight: 0,
94+
digits: vec![],
95+
}
96+
}
97+
98+
fn negative_infinity() -> Self {
99+
Self {
100+
sign: NumericSign::NegativeInfinity,
101+
scale: 0,
102+
weight: 0,
103+
digits: vec![],
104+
}
105+
}
79106
}
80107

81108
impl std::fmt::Display for Numeric {
@@ -161,33 +188,15 @@ impl FromStr for Numeric {
161188
let mut scale = 0;
162189
let mut sign = NumericSign::Positive;
163190

164-
match value {
165-
"NaN" | "nan" => {
166-
return Ok(Numeric {
167-
sign: NumericSign::NaN,
168-
scale: 0,
169-
weight: 0,
170-
digits: vec![],
171-
})
172-
}
173-
"Infinity" | "infinity" | "inf" => {
174-
return Ok(Numeric {
175-
sign: NumericSign::PositiveInfinity,
176-
scale: 0,
177-
weight: 0,
178-
digits: vec![],
179-
})
180-
}
181-
"-Infinity" | "-infinity" | "-inf" => {
182-
return Ok(Numeric {
183-
sign: NumericSign::NegativeInfinity,
184-
scale: 0,
185-
weight: 0,
186-
digits: vec![],
187-
})
188-
}
189-
_ => {}
190-
};
191+
if value.eq_ignore_ascii_case("NaN") {
192+
return Ok(Numeric::nan());
193+
}
194+
if value.eq_ignore_ascii_case("Infinity") || value.eq_ignore_ascii_case("Inf") {
195+
return Ok(Numeric::infinity());
196+
}
197+
if value.eq_ignore_ascii_case("-Infinity") || value.eq_ignore_ascii_case("-Inf") {
198+
return Ok(Numeric::negative_infinity());
199+
}
191200

192201
let mut s = value.as_bytes();
193202
if let Some(&b'-') = s.first() {
@@ -206,7 +215,7 @@ impl FromStr for Numeric {
206215

207216
if let Some(mut e) = e {
208217
if e.is_empty() {
209-
return Err("empty sientific notation string".into());
218+
return Err("empty scientific notation string".into());
210219
}
211220

212221
let mut positive = true;
@@ -221,7 +230,7 @@ impl FromStr for Numeric {
221230

222231
for &b in e {
223232
if !b.is_ascii_digit() {
224-
return Err("sientific notation string contain non-digit character".into());
233+
return Err("scientific notation string contain non-digit character".into());
225234
}
226235
exp = exp * 10 + (b - b'0') as u16;
227236
}
@@ -591,7 +600,7 @@ mod test {
591600
}
592601

593602
#[test]
594-
fn test_from_sientific_notation() {
603+
fn test_from_scientific_notation() {
595604
let cases = &[
596605
(
597606
"2e4",

0 commit comments

Comments
 (0)