@@ -9,14 +9,49 @@ use postgres_types::private::BytesMut;
9
9
use serde:: { Deserialize , Serialize } ;
10
10
use serde:: ser:: { SerializeStruct } ;
11
11
use std:: convert:: TryInto ;
12
+ use std:: fmt:: { Display , Formatter } ;
13
+
14
+ #[ derive( Debug , PartialEq , Eq ) ]
15
+ pub struct ParseError {
16
+ pg : pg_interval:: ParseError ,
17
+ }
18
+
19
+ impl From < pg_interval:: ParseError > for ParseError {
20
+ fn from ( pg : pg_interval:: ParseError ) -> ParseError {
21
+ ParseError {
22
+ pg
23
+ }
24
+ }
25
+ }
26
+
27
+ impl Into < pg_interval:: ParseError > for ParseError {
28
+ fn into ( self ) -> pg_interval:: ParseError {
29
+ self . pg
30
+ }
31
+ }
32
+
33
+ impl Display for ParseError {
34
+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> std:: fmt:: Result {
35
+ match & self . pg {
36
+ pg_interval:: ParseError :: InvalidInterval ( s) => write ! ( f, "{}" , s) ,
37
+ pg_interval:: ParseError :: InvalidTime ( s) => write ! ( f, "{}" , s) ,
38
+ pg_interval:: ParseError :: InvalidYearMonth ( s) => write ! ( f, "{}" , s) ,
39
+ pg_interval:: ParseError :: ParseIntErr ( s) => write ! ( f, "{}" , s) ,
40
+ pg_interval:: ParseError :: ParseFloatErr ( s) => write ! ( f, "{}" , s) ,
41
+ }
42
+ }
43
+ }
44
+
45
+ impl Error for ParseError {
46
+ }
12
47
13
48
#[ derive( Debug ) ]
14
49
pub struct Interval {
15
50
pg : pg_interval:: Interval ,
16
51
}
17
52
18
53
impl Interval {
19
- pub fn new ( interval : & str ) -> Result < Interval , pg_interval :: ParseError > {
54
+ pub fn new ( interval : & str ) -> Result < Interval , ParseError > {
20
55
Ok ( Interval {
21
56
pg : pg_interval:: Interval :: from_postgres ( & interval) ?,
22
57
} )
@@ -36,7 +71,7 @@ impl Interval {
36
71
}
37
72
38
73
impl FromStr for Interval {
39
- type Err = pg_interval :: ParseError ;
74
+ type Err = ParseError ;
40
75
41
76
fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
42
77
Interval :: new ( s)
@@ -202,4 +237,12 @@ mod tests {
202
237
assert_eq ! ( deserialized. pg. days, 2 ) ;
203
238
assert_eq ! ( deserialized. pg. microseconds, 3 ) ;
204
239
}
240
+
241
+ #[ test]
242
+ fn test_anyhow_error_propagation ( ) {
243
+ let interval = ( || -> anyhow:: Result < Interval > {
244
+ Ok ( Interval :: new ( "1 monthss" ) ?)
245
+ } ) ( ) ;
246
+ assert_eq ! ( interval. err( ) . unwrap( ) . to_string( ) , "Unknown or duplicate deliminator \" monthss\" " ) ;
247
+ }
205
248
}
0 commit comments