Skip to content

Commit 5540ff0

Browse files
committed
Fix one more case of int failure
1 parent 0019be9 commit 5540ff0

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

src/parser/mod.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ use alloc::{
2020
vec,
2121
vec::Vec,
2222
};
23-
use core::fmt;
23+
use core::{
24+
fmt::{self, Display},
25+
str::FromStr,
26+
};
2427

2528
use log::debug;
2629

@@ -3260,6 +3263,18 @@ impl<'a> Parser<'a> {
32603263
}
32613264
}
32623265

3266+
fn parse<T: FromStr>(s: String, loc: Location) -> Result<T, ParserError>
3267+
where
3268+
<T as FromStr>::Err: Display,
3269+
{
3270+
s.parse::<T>().map_err(|e| {
3271+
ParserError::ParserError(format!(
3272+
"Could not parse '{s}' as {}: {e}{loc}",
3273+
std::any::type_name::<T>()
3274+
))
3275+
})
3276+
}
3277+
32633278
/// Parse a comma-separated list of 1+ SelectItem
32643279
pub fn parse_projection(&mut self) -> Result<Vec<SelectItem>, ParserError> {
32653280
// BigQuery and Snowflake allow trailing commas, but only in project lists
@@ -5281,12 +5296,7 @@ impl<'a> Parser<'a> {
52815296
let _ = self.consume_token(&Token::Eq);
52825297
let next_token = self.next_token();
52835298
match next_token.token {
5284-
Token::Number(s, _) => Some(s.parse::<u32>().map_err(|e| {
5285-
ParserError::ParserError(format!(
5286-
"Could not parse '{s}' as u32: {e}{}",
5287-
next_token.location
5288-
))
5289-
})?),
5299+
Token::Number(s, _) => Some(Self::parse::<u32>(s, next_token.location)?),
52905300
_ => self.expected("literal int", next_token)?,
52915301
}
52925302
} else {
@@ -6730,10 +6740,7 @@ impl<'a> Parser<'a> {
67306740
// The call to n.parse() returns a bigdecimal when the
67316741
// bigdecimal feature is enabled, and is otherwise a no-op
67326742
// (i.e., it returns the input string).
6733-
Token::Number(ref n, l) => match n.parse() {
6734-
Ok(n) => Ok(Value::Number(n, l)),
6735-
Err(e) => parser_err!(format!("Could not parse '{n}' as number: {e}"), location),
6736-
},
6743+
Token::Number(n, l) => Ok(Value::Number(Self::parse(n, location)?, l)),
67376744
Token::SingleQuotedString(ref s) => Ok(Value::SingleQuotedString(s.to_string())),
67386745
Token::DoubleQuotedString(ref s) => Ok(Value::DoubleQuotedString(s.to_string())),
67396746
Token::TripleSingleQuotedString(ref s) => {
@@ -6825,9 +6832,7 @@ impl<'a> Parser<'a> {
68256832
pub fn parse_literal_uint(&mut self) -> Result<u64, ParserError> {
68266833
let next_token = self.next_token();
68276834
match next_token.token {
6828-
Token::Number(s, _) => s.parse::<u64>().map_err(|e| {
6829-
ParserError::ParserError(format!("Could not parse '{s}' as u64: {e}"))
6830-
}),
6835+
Token::Number(s, _) => Self::parse::<u64>(s, next_token.location),
68316836
_ => self.expected("literal int", next_token),
68326837
}
68336838
}
@@ -10331,7 +10336,7 @@ impl<'a> Parser<'a> {
1033110336
} else {
1033210337
let next_token = self.next_token();
1033310338
let quantity = match next_token.token {
10334-
Token::Number(s, _) => s.parse::<u64>().expect("literal int"),
10339+
Token::Number(s, _) => Self::parse::<u64>(s, next_token.location)?,
1033510340
_ => self.expected("literal int", next_token)?,
1033610341
};
1033710342
Some(TopQuantity::Constant(quantity))

0 commit comments

Comments
 (0)