@@ -666,7 +666,7 @@ macro_rules! quick_error {
666
666
( FIND_CONTEXT_IMPL $name: ident $item: ident: TUPLE
667
667
[ $( $var: ident: $typ: ty ) ,* ]
668
668
{ context( $cvar: ident: $ctyp: ty, $fvar: ident: $ftyp: ty)
669
- -> ( $( $texpr: expr ) ,* ) $( $tail: tt ) * }
669
+ -> ( $( $texpr: expr ) ,* ) $( $tail: tt ) * }
670
670
) => {
671
671
impl <' a> From <$crate:: Context <$ctyp, $ftyp>> for $name {
672
672
fn from(
@@ -680,13 +680,16 @@ macro_rules! quick_error {
680
680
$name $item: TUPLE [ $( $var: $typ ) ,* ]
681
681
{ $( $tail) * } ) ;
682
682
} ;
683
- /*
684
683
( FIND_CONTEXT_IMPL $name: ident $item: ident: STRUCT
685
684
[ $( $var: ident: $typ: ty ) ,* ]
686
- { from($fvar:ident: $ftyp:ty) -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )*}
685
+ { context( $cvar: ident: $ctyp: ty, $fvar: ident: $ftyp: ty)
686
+ -> { $( $tvar: ident: $texpr: expr ) ,* } $( $tail: tt ) * }
687
687
) => {
688
- impl From<$ftyp> for $name {
689
- fn from($fvar: $ftyp) -> $name {
688
+ impl <' a> From <$crate:: Context <$ctyp, $ftyp>> for $name {
689
+ fn from(
690
+ $crate:: Context ( $cvar, $fvar) : $crate:: Context <$ctyp, $ftyp>)
691
+ -> $name
692
+ {
690
693
$name:: $item {
691
694
$( $tvar: $texpr ) ,*
692
695
}
@@ -696,7 +699,6 @@ macro_rules! quick_error {
696
699
$name $item: STRUCT [ $( $var: $typ ) ,* ]
697
700
{ $( $tail) * } ) ;
698
701
} ;
699
- */
700
702
( FIND_CONTEXT_IMPL $name: ident $item: ident: $imode: tt
701
703
[ $( $var: ident: $typ: ty ) ,* ]
702
704
{ $t: tt $( $tail: tt ) * }
@@ -790,7 +792,7 @@ impl<T, E> ResultExt<T, E> for Result<T, E> {
790
792
791
793
#[ cfg( test) ]
792
794
mod test {
793
- use std:: num:: ParseFloatError ;
795
+ use std:: num:: { ParseFloatError , ParseIntError } ;
794
796
use std:: str:: Utf8Error ;
795
797
use std:: string:: FromUtf8Error ;
796
798
use std:: error:: Error ;
@@ -968,9 +970,14 @@ mod test {
968
970
quick_error ! {
969
971
#[ derive( Debug ) ]
970
972
pub enum ContextErr {
971
- Parse ( src: String , err: ParseFloatError ) {
973
+ Float ( src: String , err: ParseFloatError ) {
972
974
context( s: & ' a str , e: ParseFloatError ) -> ( s. to_string( ) , e)
973
- display( "Error parsing {:?}: {}" , src, err)
975
+ display( "Float error {:?}: {}" , src, err)
976
+ }
977
+ Int { src: String , err: ParseIntError } {
978
+ context( s: & ' a str , e: ParseIntError )
979
+ -> { src: s. to_string( ) , err: e}
980
+ display( "Int error {:?}: {}" , src, err)
974
981
}
975
982
}
976
983
}
@@ -981,6 +988,15 @@ mod test {
981
988
Ok ( try!( s. parse ( ) . context ( s) ) )
982
989
}
983
990
assert_eq ! ( format!( "{}" , parse_float( "12ab" ) . unwrap_err( ) ) ,
984
- r#"Error parsing "12ab": invalid float literal"# ) ;
991
+ r#"Float error "12ab": invalid float literal"# ) ;
992
+ }
993
+
994
+ #[ test]
995
+ fn parse_int_error ( ) {
996
+ fn parse_int ( s : & str ) -> Result < i32 , ContextErr > {
997
+ Ok ( try!( s. parse ( ) . context ( s) ) )
998
+ }
999
+ assert_eq ! ( format!( "{}" , parse_int( "12.5" ) . unwrap_err( ) ) ,
1000
+ r#"Int error "12.5": invalid digit found in string"# ) ;
985
1001
}
986
1002
}
0 commit comments