Skip to content

Commit 5f232aa

Browse files
committed
More combination of public/private wrapper type implemented
1 parent 6996a1a commit 5f232aa

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/lib.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@
264264
/// Main macro that does all the work
265265
#[macro_export]
266266
macro_rules! quick_error {
267+
267268
( $(#[$meta:meta])*
268269
pub enum $name:ident { $($chunks:tt)* }
269270
) => {
@@ -278,6 +279,74 @@ macro_rules! quick_error {
278279
items [] buf []
279280
queue [ $($chunks)* ]);
280281
};
282+
283+
( $(#[$meta:meta])*
284+
pub enum $name:ident wraps $enum_name:ident { $($chunks:tt)* }
285+
) => {
286+
quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*);
287+
quick_error!(SORT [enum $enum_name $(#[$meta])* ]
288+
items [] buf []
289+
queue [ $($chunks)* ]);
290+
};
291+
292+
( $(#[$meta:meta])*
293+
pub enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* }
294+
) => {
295+
quick_error!(WRAPPER $enum_name [ pub struct ] $name $(#[$meta])*);
296+
quick_error!(SORT [pub enum $enum_name $(#[$meta])* ]
297+
items [] buf []
298+
queue [ $($chunks)* ]);
299+
};
300+
( $(#[$meta:meta])*
301+
enum $name:ident wraps $enum_name:ident { $($chunks:tt)* }
302+
) => {
303+
quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*);
304+
quick_error!(SORT [enum $enum_name $(#[$meta])* ]
305+
items [] buf []
306+
queue [ $($chunks)* ]);
307+
};
308+
309+
( $(#[$meta:meta])*
310+
enum $name:ident wraps pub $enum_name:ident { $($chunks:tt)* }
311+
) => {
312+
quick_error!(WRAPPER $enum_name [ struct ] $name $(#[$meta])*);
313+
quick_error!(SORT [pub enum $enum_name $(#[$meta])* ]
314+
items [] buf []
315+
queue [ $($chunks)* ]);
316+
};
317+
318+
319+
(
320+
WRAPPER $internal:ident [ $($strdef:tt)* ] $strname:ident
321+
$(#[$meta:meta])*
322+
) => {
323+
$(#[$meta])*
324+
$($strdef)* $strname ( $internal );
325+
326+
impl ::std::fmt::Display for $strname {
327+
fn fmt(&self, f: &mut ::std::fmt::Formatter)
328+
-> ::std::fmt::Result
329+
{
330+
::std::fmt::Display::fmt(&self.0, f)
331+
}
332+
}
333+
334+
impl From<$internal> for $strname {
335+
fn from(err: $internal) -> Self {
336+
$strname(err)
337+
}
338+
}
339+
340+
impl ::std::error::Error for $strname {
341+
fn description(&self) -> &str {
342+
self.0.description()
343+
}
344+
fn cause(&self) -> Option<&::std::error::Error> {
345+
self.0.cause()
346+
}
347+
}
348+
};
349+
281350
// Queue is empty, can do the work
282351
(SORT [enum $name:ident $( #[$meta:meta] )*]
283352
items [$($( #[$imeta:meta] )*
@@ -918,6 +987,30 @@ mod test {
918987
assert!(err.cause().is_none());
919988
}
920989

990+
quick_error! {
991+
#[derive(Debug)]
992+
pub enum Wrapper wraps Wrapped {
993+
One
994+
Two(s: String) {
995+
display("two: {}", s)
996+
from()
997+
}
998+
}
999+
}
1000+
1001+
#[test]
1002+
fn wrapper() {
1003+
assert_eq!(format!("{}", Wrapper::from(Wrapped::One)),
1004+
"One".to_string());
1005+
assert_eq!(format!("{}",
1006+
Wrapper::from(Wrapped::from(String::from("hello")))),
1007+
"two: hello".to_string());
1008+
assert_eq!(format!("{:?}", Wrapper::from(Wrapped::One)),
1009+
"Wrapper(One)".to_string());
1010+
assert_eq!(Wrapper::from(Wrapped::One).description(),
1011+
"One".to_string());
1012+
}
1013+
9211014
quick_error! {
9221015
#[derive(Debug, PartialEq)]
9231016
pub enum TupleWrapper {

0 commit comments

Comments
 (0)