88} ;
99
1010#[ enum_dispatch( Value ) ]
11- pub trait Cast < Output : Sized > {
11+ pub trait Cast < Output : Sized >
12+ where
13+ Self : Sized ,
14+ {
1215 fn cast ( self ) -> Result < Output > {
1316 Err ( Error :: Value ( ValueError :: UnimplementedCast ) )
1417 }
@@ -76,17 +79,19 @@ impl Cast<u64> for bool {
7679}
7780impl Cast < u64 > for i64 {
7881 fn cast ( self ) -> Result < u64 > {
79- self . try_into ( ) . map_err ( |_| ValueError :: ImpossibleCast )
82+ self . try_into ( )
83+ . map_err ( |_| Error :: Value ( ValueError :: ImpossibleCast ) )
8084 }
8185}
8286impl Cast < u64 > for f64 {
8387 fn cast ( self ) -> Result < u64 > {
84- self . cast :: < i64 > ( ) ?. cast ( )
88+ let int: i64 = self . cast ( ) ?;
89+ int. cast ( )
8590 }
8691}
8792impl Cast < u64 > for String {
8893 fn cast ( self ) -> Result < u64 > {
89- lexical:: parse ( self ) . map_err ( |_| ValueError :: ImpossibleCast )
94+ lexical:: parse ( self ) . map_err ( |_| Error :: Value ( ValueError :: ImpossibleCast ) )
9095 }
9196}
9297
@@ -98,16 +103,17 @@ impl Cast<i64> for bool {
98103impl Cast < i64 > for u64 {
99104 fn cast ( self ) -> Result < i64 > {
100105 self . try_into ( )
106+ . map_err ( |_| Error :: Value ( ValueError :: ImpossibleCast ) )
101107 }
102108}
103109impl Cast < i64 > for f64 {
104110 fn cast ( self ) -> Result < i64 > {
105- self . try_into ( )
111+ Ok ( self . trunc ( ) as i64 ) // TODO: Better
106112 }
107113}
108114impl Cast < i64 > for String {
109115 fn cast ( self ) -> Result < i64 > {
110- lexical:: parse ( self ) . map_err ( |_| ValueError :: ImpossibleCast )
116+ lexical:: parse ( self ) . map_err ( |_| Error :: Value ( ValueError :: ImpossibleCast ) )
111117 }
112118}
113119
@@ -128,7 +134,7 @@ impl Cast<f64> for i64 {
128134}
129135impl Cast < f64 > for String {
130136 fn cast ( self ) -> Result < f64 > {
131- fast_float:: parse ( self ) . map_err ( |_| ValueError :: ImpossibleCast )
137+ fast_float:: parse ( self ) . map_err ( |_| Error :: Value ( ValueError :: ImpossibleCast ) )
132138 }
133139}
134140
@@ -162,6 +168,7 @@ impl<T: Cast<u64>> Cast<usize> for T {
162168 }
163169}
164170
171+ /*
165172// Non-Core
166173impl CastWithRules<bool> for Value {
167174 fn cast_with_rule(self, rule: Self) -> Result<bool> {
@@ -187,6 +194,7 @@ impl CastWithRules<f64> for Value {
187194 }
188195 }
189196}
197+ */
190198/*impl CastWithRules<String> for Value {
191199 fn cast_with_rule(self, rule: Self) -> Result<String> {
192200 match rule {
0 commit comments