@@ -516,6 +516,11 @@ macro_rules! quick_error {
516
516
$name $item: $imode [ $( $var: $typ ) ,* ]
517
517
{ $( $funcs ) * } ) ;
518
518
) *
519
+ $(
520
+ quick_error!( FIND_CONTEXT_IMPL
521
+ $name $item: $imode [ $( $var: $typ ) ,* ]
522
+ { $( $funcs ) * } ) ;
523
+ ) *
519
524
} ;
520
525
( FIND_DISPLAY_IMPL $name: ident $item: ident: $imode: tt
521
526
{ display( $self_: tt) -> ( $( $exprs: tt ) * ) $( $tail: tt ) * }
@@ -586,6 +591,7 @@ macro_rules! quick_error {
586
591
) => {
587
592
None
588
593
} ;
594
+ // ----------------------------- FROM IMPL --------------------------
589
595
( FIND_FROM_IMPL $name: ident $item: ident: $imode: tt
590
596
[ $( $var: ident: $typ: ty ) ,* ]
591
597
{ from( ) $( $tail: tt ) * }
@@ -656,6 +662,53 @@ macro_rules! quick_error {
656
662
{ }
657
663
) => {
658
664
} ;
665
+ // ----------------------------- CONTEXT IMPL --------------------------
666
+ ( FIND_CONTEXT_IMPL $name: ident $item: ident: TUPLE
667
+ [ $( $var: ident: $typ: ty ) ,* ]
668
+ { context( $cvar: ident: $ctyp: ty, $fvar: ident: $ftyp: ty)
669
+ -> ( $( $texpr: expr ) ,* ) $( $tail: tt ) * }
670
+ ) => {
671
+ impl <' a> From <Context <$ctyp, $ftyp>> for $name {
672
+ fn from( Context ( $cvar, $fvar) : Context <$ctyp, $ftyp>) -> $name {
673
+ $name:: $item( $( $texpr ) ,* )
674
+ }
675
+ }
676
+ quick_error!( FIND_CONTEXT_IMPL
677
+ $name $item: TUPLE [ $( $var: $typ ) ,* ]
678
+ { $( $tail) * } ) ;
679
+ } ;
680
+ /*
681
+ (FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT
682
+ [$( $var:ident: $typ:ty ),*]
683
+ { from($fvar:ident: $ftyp:ty) -> {$( $tvar:ident: $texpr:expr ),*} $( $tail:tt )*}
684
+ ) => {
685
+ impl From<$ftyp> for $name {
686
+ fn from($fvar: $ftyp) -> $name {
687
+ $name::$item {
688
+ $( $tvar: $texpr ),*
689
+ }
690
+ }
691
+ }
692
+ quick_error!(FIND_CONTEXT_IMPL
693
+ $name $item: STRUCT [$( $var:$typ ),*]
694
+ { $($tail)* });
695
+ };
696
+ */
697
+ ( FIND_CONTEXT_IMPL $name: ident $item: ident: $imode: tt
698
+ [ $( $var: ident: $typ: ty ) ,* ]
699
+ { $t: tt $( $tail: tt ) * }
700
+ ) => {
701
+ quick_error!( FIND_CONTEXT_IMPL
702
+ $name $item: $imode [ $( $var: $typ ) ,* ]
703
+ { $( $tail ) * }
704
+ ) ;
705
+ } ;
706
+ ( FIND_CONTEXT_IMPL $name: ident $item: ident: $imode: tt
707
+ [ $( $var: ident: $typ: ty ) ,* ]
708
+ { }
709
+ ) => {
710
+ } ;
711
+ // ----------------------------- ITEM IMPL --------------------------
659
712
( ITEM_BODY $( #[ $imeta: meta] ) * $item: ident: UNIT
660
713
) => { } ;
661
714
( ITEM_BODY $( #[ $imeta: meta] ) * $item: ident: TUPLE
@@ -704,12 +757,34 @@ macro_rules! quick_error {
704
757
=> { quick_error!( ERROR_CHECK TUPLE $( $tail) * ) ; } ;
705
758
( ERROR_CHECK STRUCT from( $fvar: ident: $ftyp: ty) -> { $( $v: ident: $e: expr ) ,* } $( $tail: tt ) * )
706
759
=> { quick_error!( ERROR_CHECK STRUCT $( $tail) * ) ; } ;
760
+
761
+ ( ERROR_CHECK TUPLE context( $cvar: ident: $ctyp: ty, $fvar: ident: $ftyp: ty)
762
+ -> ( $( $e: expr ) ,* ) $( $tail: tt ) * )
763
+ => { quick_error!( ERROR_CHECK TUPLE $( $tail) * ) ; } ;
764
+ ( ERROR_CHECK STRUCT context( $cvar: ident: $ctyp: ty, $fvar: ident: $ftyp: ty)
765
+ -> { $( $v: ident: $e: expr ) ,* } $( $tail: tt ) * )
766
+ => { quick_error!( ERROR_CHECK STRUCT $( $tail) * ) ; } ;
767
+
707
768
( ERROR_CHECK $imode: tt ) => { } ;
708
769
// Utility functions
709
770
( IDENT $ident: ident) => { $ident }
710
771
}
711
772
712
773
774
+ pub struct Context < X , E > ( pub X , pub E ) ;
775
+
776
+ pub trait ResultExt < T , E > {
777
+ fn context < X > ( self , x : X ) -> Result < T , Context < X , E > > ;
778
+ }
779
+
780
+ impl < T , E > ResultExt < T , E > for Result < T , E > {
781
+ fn context < X > ( self , x : X ) -> Result < T , Context < X , E > > {
782
+ self . map_err ( |e| Context ( x, e) )
783
+ }
784
+ }
785
+
786
+
787
+
713
788
#[ cfg( test) ]
714
789
mod test {
715
790
use std:: num:: ParseFloatError ;
0 commit comments