Skip to content

Commit 6e4fc47

Browse files
committed
Add context support for struct enum members
1 parent 0193fac commit 6e4fc47

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

src/lib.rs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ macro_rules! quick_error {
666666
(FIND_CONTEXT_IMPL $name:ident $item:ident: TUPLE
667667
[$( $var:ident: $typ:ty ),*]
668668
{ context($cvar:ident: $ctyp:ty, $fvar:ident: $ftyp:ty)
669-
-> ($( $texpr:expr ),*) $( $tail:tt )*}
669+
-> ($( $texpr:expr ),*) $( $tail:tt )* }
670670
) => {
671671
impl<'a> From<$crate::Context<$ctyp, $ftyp>> for $name {
672672
fn from(
@@ -680,13 +680,16 @@ macro_rules! quick_error {
680680
$name $item: TUPLE [$( $var:$typ ),*]
681681
{ $($tail)* });
682682
};
683-
/*
684683
(FIND_CONTEXT_IMPL $name:ident $item:ident: STRUCT
685684
[$( $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 )* }
687687
) => {
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+
{
690693
$name::$item {
691694
$( $tvar: $texpr ),*
692695
}
@@ -696,7 +699,6 @@ macro_rules! quick_error {
696699
$name $item: STRUCT [$( $var:$typ ),*]
697700
{ $($tail)* });
698701
};
699-
*/
700702
(FIND_CONTEXT_IMPL $name:ident $item:ident: $imode:tt
701703
[$( $var:ident: $typ:ty ),*]
702704
{ $t:tt $( $tail:tt )*}
@@ -790,7 +792,7 @@ impl<T, E> ResultExt<T, E> for Result<T, E> {
790792

791793
#[cfg(test)]
792794
mod test {
793-
use std::num::ParseFloatError;
795+
use std::num::{ParseFloatError, ParseIntError};
794796
use std::str::Utf8Error;
795797
use std::string::FromUtf8Error;
796798
use std::error::Error;
@@ -968,9 +970,14 @@ mod test {
968970
quick_error! {
969971
#[derive(Debug)]
970972
pub enum ContextErr {
971-
Parse(src: String, err: ParseFloatError) {
973+
Float(src: String, err: ParseFloatError) {
972974
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)
974981
}
975982
}
976983
}
@@ -981,6 +988,15 @@ mod test {
981988
Ok(try!(s.parse().context(s)))
982989
}
983990
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"#);
9851001
}
9861002
}

0 commit comments

Comments
 (0)