@@ -20,7 +20,10 @@ use alloc::{
20
20
vec,
21
21
vec:: Vec ,
22
22
} ;
23
- use core:: fmt;
23
+ use core:: {
24
+ fmt:: { self , Display } ,
25
+ str:: FromStr ,
26
+ } ;
24
27
25
28
use log:: debug;
26
29
@@ -3260,6 +3263,18 @@ impl<'a> Parser<'a> {
3260
3263
}
3261
3264
}
3262
3265
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
+
3263
3278
/// Parse a comma-separated list of 1+ SelectItem
3264
3279
pub fn parse_projection ( & mut self ) -> Result < Vec < SelectItem > , ParserError > {
3265
3280
// BigQuery and Snowflake allow trailing commas, but only in project lists
@@ -5281,12 +5296,7 @@ impl<'a> Parser<'a> {
5281
5296
let _ = self . consume_token ( & Token :: Eq ) ;
5282
5297
let next_token = self . next_token ( ) ;
5283
5298
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 ) ?) ,
5290
5300
_ => self . expected ( "literal int" , next_token) ?,
5291
5301
}
5292
5302
} else {
@@ -6730,10 +6740,7 @@ impl<'a> Parser<'a> {
6730
6740
// The call to n.parse() returns a bigdecimal when the
6731
6741
// bigdecimal feature is enabled, and is otherwise a no-op
6732
6742
// (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) ) ,
6737
6744
Token :: SingleQuotedString ( ref s) => Ok ( Value :: SingleQuotedString ( s. to_string ( ) ) ) ,
6738
6745
Token :: DoubleQuotedString ( ref s) => Ok ( Value :: DoubleQuotedString ( s. to_string ( ) ) ) ,
6739
6746
Token :: TripleSingleQuotedString ( ref s) => {
@@ -6825,9 +6832,7 @@ impl<'a> Parser<'a> {
6825
6832
pub fn parse_literal_uint ( & mut self ) -> Result < u64 , ParserError > {
6826
6833
let next_token = self . next_token ( ) ;
6827
6834
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 ) ,
6831
6836
_ => self . expected ( "literal int" , next_token) ,
6832
6837
}
6833
6838
}
@@ -10331,7 +10336,7 @@ impl<'a> Parser<'a> {
10331
10336
} else {
10332
10337
let next_token = self . next_token ( ) ;
10333
10338
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 ) ? ,
10335
10340
_ => self . expected ( "literal int" , next_token) ?,
10336
10341
} ;
10337
10342
Some ( TopQuantity :: Constant ( quantity) )
0 commit comments