Skip to content

Commit 94f0680

Browse files
committed
refactor and simplify
1 parent 98513c1 commit 94f0680

File tree

3 files changed

+95
-114
lines changed

3 files changed

+95
-114
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
language: rust
22
rust:
3-
- 1.3.0
43
- stable
54
- beta
65
- nightly

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Here is the comprehensive example:
2828
}
2929
Other(descr: &'static str) {
3030
description(descr)
31-
display("Errorr {}", descr)
31+
display("Error {}", descr)
3232
}
3333
IoAt(place: &'static str, err: io::Error) {
3434
cause(err)

src/lib.rs

Lines changed: 94 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -203,37 +203,37 @@
203203
//! Empty braces can be omitted as of quick_error 0.1.3.
204204
//!
205205
206+
206207
/// Main macro that does all the work
207208
#[macro_export]
208209
macro_rules! quick_error {
209210
( $(#[$meta:meta])*
210211
pub enum $name:ident { $($chunks:tt)* }
211212
) => {
212213
quick_error!(SORT [pub enum $name $(#[$meta])* ]
213-
enum [] items [] buf []
214+
items [] buf []
214215
queue [ $($chunks)* ]);
215216
};
216217
( $(#[$meta:meta])*
217218
enum $name:ident { $($chunks:tt)* }
218219
) => {
219220
quick_error!(SORT [enum $name $(#[$meta])* ]
220-
enum [] items [] buf []
221+
items [] buf []
221222
queue [ $($chunks)* ]);
222223
};
223224
// Queue is empty, can do the work
224225
(SORT [enum $name:ident $(#[$meta:meta])* ]
225-
enum [ $( $(#[$emeta:meta])*
226-
=> $eitem:ident $( ( $($etyp:ty),* ) )* )* ]
227-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
226+
items [ $( $(#[$imeta:imeta])*
227+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
228228
{ $($ifuncs:tt)* } )* ]
229229
buf [ ]
230230
queue [ ]
231231
) => {
232232
$(#[$meta])*
233233
enum $name {
234234
$(
235-
$(#[$emeta])*
236-
$eitem $(( $($etyp),* ))*,
235+
$(#[$imeta])*
236+
$iitem $(( $($ityp),* ))*,
237237
)*
238238
}
239239
quick_error!(IMPLEMENTATIONS $name { $(
@@ -244,18 +244,17 @@ macro_rules! quick_error {
244244
)*
245245
};
246246
(SORT [pub enum $name:ident $(#[$meta:meta])* ]
247-
enum [ $( $(#[$emeta:meta])*
248-
=> $eitem:ident $( ( $($etyp:ty),* ) )* )* ]
249-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
247+
items [ $( $(#[$imeta:meta])*
248+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
250249
{ $($ifuncs:tt)* } )* ]
251250
buf [ ]
252251
queue [ ]
253252
) => {
254253
$(#[$meta])*
255254
pub enum $name {
256255
$(
257-
$(#[$emeta])*
258-
$eitem $(( $($etyp),* ))*,
256+
$(#[$imeta])*
257+
$iitem $(( $($ityp),* ))*,
259258
)*
260259
}
261260
quick_error!(IMPLEMENTATIONS $name { $(
@@ -267,39 +266,35 @@ macro_rules! quick_error {
267266
};
268267
// Add meta to buffer
269268
(SORT [$($def:tt)*]
270-
enum [ $( $(#[$emeta:meta])*
271-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
272-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
269+
items [ $( $(#[$imeta:meta])*
270+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
273271
{ $($ifuncs:tt)* } )* ]
274272
buf [ $( #[$bmeta:meta] )* ]
275273
queue [ #[$qmeta:meta] $($tail:tt)* ]
276274
) => {
277275
quick_error!(SORT [$($def)* ]
278-
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*]
279-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
276+
items [$( $(#[$imeta])* => $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
280277
buf [ $( #[$bmeta] )* #[$qmeta] ]
281278
queue [ $($tail)* ]);
282279
};
283280
// Add ident to buffer
284281
(SORT [$($def:tt)*]
285-
enum [ $( $(#[$emeta:meta])*
286-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
287-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
282+
items [ $( $(#[$imeta:meta])*
283+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
288284
{ $($ifuncs:tt)* } )* ]
289285
buf [ $( #[$bmeta:meta] )* ]
290286
queue [ $qitem:ident $($tail:tt)* ]
291287
) => {
292288
quick_error!(SORT [$($def)* ]
293-
enum [ $( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )* ]
294-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
289+
items [ $( $(#[$imeta])*
290+
=> $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
295291
buf [ $(#[$bmeta])* => $qitem ]
296292
queue [ $($tail)* ]);
297293
};
298294
// Flush buffer on meta after ident
299295
(SORT [$($def:tt)*]
300-
enum [ $( $(#[$emeta:meta])*
301-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
302-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
296+
items [ $( $(#[$imeta:meta])*
297+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
303298
{ $($ifuncs:tt)* } )* ]
304299
buf [ $( #[$bmeta:meta] )*
305300
=> $bitem:ident $(( $($bvar:ident : $btyp:ty),* ))* ]
@@ -308,77 +303,67 @@ macro_rules! quick_error {
308303
quick_error!(SORT [$($def)* ]
309304
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*
310305
$(#[$bmeta])* => $bitem $(( $($btyp),* ))*]
311-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
306+
items [ $( $(#[$imeta:imeta])*
307+
=> $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
312308
$bitem $(( $($bvar:$btyp),* ))* {} ]
313309
buf [ #[$qmeta] ]
314310
queue [ $($tail)* ]);
315311
};
316312
// Add parenthesis
317313
(SORT [$($def:tt)*]
318-
enum [ $( $(#[$emeta:meta])*
319-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
320-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
314+
items [ $( $(#[$imeta:meta])*
315+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
321316
{ $($ifuncs:tt)* } )* ]
322317
buf [ $( #[$bmeta:meta] )* => $bitem:ident ]
323318
queue [ ( $( $qvar:ident : $qtyp:ty ),* ) $($tail:tt)* ]
324319
) => {
325320
quick_error!(SORT [$($def)* ]
326-
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*]
327-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
321+
items [$( $(#[$imeta])* => $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )* ]
328322
buf [ $( #[$bmeta] )* => $bitem ( $( $qvar:$qtyp ),* ) ]
329323
queue [ $($tail)* ]);
330324
};
331325
// Add braces and flush always on braces
332326
(SORT [$($def:tt)*]
333-
enum [ $( $(#[$emeta:meta])*
334-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
335-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
327+
items [ $( $(#[$imeta:meta])*
328+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
336329
{ $($ifuncs:tt)* } )* ]
337330
buf [ $( #[$bmeta:meta] )*
338331
=> $bitem:ident $(( $($bvar:ident : $btyp:ty),* ))* ]
339332
queue [ { $($qfuncs:tt)* } $($tail:tt)* ]
340333
) => {
341334
quick_error!(SORT [$($def)* ]
342-
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*
343-
$(#[$bmeta])* => $bitem $(( $($btyp),* ))* ]
344-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
345-
$bitem $(( $($bvar:$btyp),* ))* { $($qfuncs)* } ]
335+
items [$( $(#[$imeta])* => $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
336+
$(#[$bmeta])* => $bitem $(( $($bvar:$btyp),* ))* { $($qfuncs)* } ]
346337
buf [ ]
347338
queue [ $($tail)* ]);
348339
};
349340
// Flush buffer on double ident
350341
(SORT [$($def:tt)*]
351-
enum [ $( $(#[$emeta:meta])*
352-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
353-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
342+
items [ $( $(#[$imeta:meta])*
343+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
354344
{ $($ifuncs:tt)* } )* ]
355345
buf [ $( #[$bmeta:meta] )*
356346
=> $bitem:ident $(( $($bvar:ident : $btyp:ty),* ))* ]
357347
queue [ $qitem:ident $($tail:tt)* ]
358348
) => {
359349
quick_error!(SORT [$($def)* ]
360-
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*
361-
$(#[$bmeta])* => $bitem $(( $($btyp),* ))*]
362-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
363-
$bitem $(( $($bvar:$btyp),* ))* {} ]
350+
items [$( $(#[$imeta])* => $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
351+
$(#[$bmeta])* => $bitem $(( $($bvar:$btyp),* ))* {} ]
364352
buf [ => $qitem ]
365353
queue [ $($tail)* ]);
366354
};
367355
// Flush buffer on end
368356
(SORT [$($def:tt)*]
369-
enum [ $( $(#[$emeta:meta])*
370-
=> $eitem:ident $(( $($etyp:ty),* ))* )* ]
371-
items [ $( $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
357+
items [ $( $(#[$imeta:meta])*
358+
=> $iitem:ident $(( $($ivar:ident : $ityp:ty),* ))*
372359
{ $($ifuncs:tt)* } )* ]
373360
buf [ $( #[$bmeta:meta] )*
374361
=> $bitem:ident $(( $($bvar:ident : $btyp:ty),* ))* ]
375362
queue [ ]
376363
) => {
377364
quick_error!(SORT [$($def)* ]
378-
enum [$( $(#[$emeta])* => $eitem $(( $($etyp),* ))* )*
379-
$(#[$bmeta])* => $bitem $(( $($btyp),* ))* ]
380-
items [ $( $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
381-
$bitem $(( $($bvar:$btyp),* ))* {} ]
365+
items [$( $(#[$imeta])* => $iitem $(( $($ivar:$ityp),* ))* { $($ifuncs)* } )*
366+
$(#[$bmeta])* => $bitem $(( $($bvar:$btyp),* ))* {} ]
382367
buf [ ]
383368
queue [ ]);
384369
};
@@ -588,7 +573,9 @@ macro_rules! quick_error {
588573

589574
#[cfg(test)]
590575
mod test {
591-
use std::io;
576+
use std::num::ParseFloatError;
577+
use std::str::Utf8Error;
578+
use std::string::FromUtf8Error;
592579
use std::error::Error;
593580

594581
quick_error! {
@@ -616,26 +603,25 @@ mod test {
616603
}
617604

618605
quick_error! {
619-
#[derive(Debug)]
620-
pub enum IoWrapper {
621-
/// I/O Error
622-
Io(err: io::Error) {
606+
#[derive(Debug, PartialEq)]
607+
pub enum Wrapper {
608+
/// ParseFloat Error
609+
ParseFloatError(err: ParseFloatError) {
623610
from()
624611
description(err.description())
625-
display("I/O error: {err}", err=err)
612+
display("parse float error: {err}", err=err)
626613
cause(err)
627614
}
628615
Other(descr: &'static str) {
629616
description(descr)
630617
display("Error: {}", descr)
631618
}
632-
/// I/O error with some context
633-
IoAt(place: &'static str, err: io::Error) {
619+
/// FromUtf8 Error
620+
FromUtf8Error(err: Utf8Error, source: Vec<u8>) {
634621
cause(err)
635-
display(self_) -> ("{} {}: {}", self_.description(), place, err)
636-
description("io error at")
637-
from(s: String) -> ("idea",
638-
io::Error::new(io::ErrorKind::Other, s))
622+
display(me) -> ("{desc} at index {pos}: {err}", desc=me.description(), pos=err.valid_up_to(), err=err)
623+
description("utf8 error")
624+
from(err: FromUtf8Error) -> (err.utf8_error().clone(), err.into_bytes())
639625
}
640626
Discard {
641627
from(&'static str)
@@ -647,72 +633,68 @@ mod test {
647633
}
648634

649635
#[test]
650-
fn io_wrapper_err() {
651-
let io1 = IoWrapper::Io(
652-
io::Error::new(io::ErrorKind::Other, "some error"));
653-
assert_eq!(format!("{}", io1), "I/O error: some error".to_string());
654-
assert_eq!(format!("{:?}", io1),
655-
"Io(Error { repr: Custom(Custom { kind: Other, \
656-
error: StringError(\"some error\") }) })".to_string());
657-
assert_eq!(io1.description(), "some error");
658-
assert_eq!(io1.cause().unwrap().description(), "some error");
636+
fn wrapper_err() {
637+
let cause = "one and a half times pi".parse::<f32>().unwrap_err();
638+
let err = Wrapper::ParseFloatError(cause.clone());
639+
assert_eq!(format!("{}", err), format!("parse float error: {}", cause));
640+
assert_eq!(format!("{:?}", err), format!("ParseFloatError({:?})", cause));
641+
assert_eq!(err.description(), cause.description());
642+
assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
659643
}
660644

661645
#[test]
662-
fn io_wrapper_trait_str() {
663-
let err: &Error = &IoWrapper::Other("hello");
664-
assert_eq!(format!("{}", err), "Error: hello".to_string());
665-
assert_eq!(format!("{:?}", err), "Other(\"hello\")".to_string());
666-
assert_eq!(err.description(), "hello".to_string());
646+
fn wrapper_trait_str() {
647+
let desc = "hello";
648+
let err: &Error = &Wrapper::Other(desc);
649+
assert_eq!(format!("{}", err), format!("Error: {}", desc));
650+
assert_eq!(format!("{:?}", err), format!("Other({:?})", desc));
651+
assert_eq!(err.description(), desc);
667652
assert!(err.cause().is_none());
668653
}
669654

670655
#[test]
671-
fn io_wrapper_trait_two_fields() {
672-
let io1 = IoWrapper::Io(
673-
io::Error::new(io::ErrorKind::Other, "some error"));
674-
let err: &Error = &IoWrapper::IoAt("file",
675-
io::Error::new(io::ErrorKind::NotFound, io1));
676-
assert_eq!(format!("{}", err),
677-
"io error at file: I/O error: some error".to_string());
678-
assert_eq!(format!("{:?}", err), "IoAt(\"file\", Error { \
679-
repr: Custom(Custom { kind: NotFound, \
680-
error: Io(Error { repr: Custom(Custom { \
681-
kind: Other, error: StringError(\"some error\") \
682-
}) }) }) })".to_string());
683-
assert_eq!(err.description(), "io error at");
684-
assert_eq!(err.cause().unwrap().description(), "some error");
656+
fn wrapper_trait_two_fields() {
657+
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
658+
let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err().utf8_error();
659+
let err: &Error = &Wrapper::FromUtf8Error(cause.clone(), invalid_utf8.clone());
660+
assert_eq!(format!("{}", err), format!("{desc} at index {pos}: {cause}", desc=err.description(), pos=cause.valid_up_to(), cause=cause));
661+
assert_eq!(format!("{:?}", err), format!("FromUtf8Error({:?}, {:?})", cause, invalid_utf8));
662+
assert_eq!(err.description(), "utf8 error");
663+
assert_eq!(format!("{:?}", err.cause().unwrap()), format!("{:?}", cause));
685664
}
686665

687666
#[test]
688-
fn io_wrapper_from() {
689-
let io1: IoWrapper = From::from(io::Error::from_raw_os_error(2));
690-
assert_eq!(format!("{}", io1),
691-
"I/O error: No such file or directory (os error 2)".to_string());
692-
let descr = io1.cause().unwrap().description();
693-
assert!(descr == "os error" // rust <= 1.6
694-
|| descr == "entity not found" // rust 1.7 (probably, nightly)
695-
);
667+
fn wrapper_from() {
668+
let cause = "one and a half times pi".parse::<f32>().unwrap_err();
669+
let err = Wrapper::ParseFloatError(cause.clone());
670+
let err_from: Wrapper = From::from(cause);
671+
assert_eq!(err_from, err);
696672
}
697673

698674
#[test]
699-
fn io_wrapper_custom_from() {
700-
let io1: IoWrapper = From::from("Stringy".to_string());
701-
assert_eq!(format!("{}", io1), "io error at idea: Stringy".to_string());
702-
assert_eq!(io1.cause().unwrap().description(), "Stringy");
675+
fn wrapper_custom_from() {
676+
let invalid_utf8: Vec<u8> = vec![0, 159, 146, 150];
677+
let cause = String::from_utf8(invalid_utf8.clone()).unwrap_err();
678+
let err = Wrapper::FromUtf8Error(cause.utf8_error().clone(), invalid_utf8);
679+
let err_from: Wrapper = From::from(cause);
680+
assert_eq!(err_from, err);
703681
}
704682

705683
#[test]
706-
fn io_wrapper_discard() {
707-
let io1: IoWrapper = From::from("hello");
708-
assert_eq!(format!("{}", io1), "Discard".to_string());
709-
assert!(io1.cause().is_none());
684+
fn wrapper_discard() {
685+
let err: Wrapper = From::from("hello");
686+
assert_eq!(format!("{}", err), format!("Discard"));
687+
assert_eq!(format!("{:?}", err), format!("Discard"));
688+
assert_eq!(err.description(), "Discard");
689+
assert!(err.cause().is_none());
710690
}
711691

712692
#[test]
713-
fn io_wrapper_signleton() {
714-
let io1: IoWrapper = IoWrapper::Singleton;
715-
assert_eq!(format!("{}", io1), "Just a string".to_string());
693+
fn wrapper_singleton() {
694+
let err: Wrapper = Wrapper::Singleton;
695+
assert_eq!(format!("{}", err), format!("Just a string"));
696+
assert_eq!(format!("{:?}", err), format!("Singleton"));
697+
assert_eq!(err.description(), "Singleton");
698+
assert!(err.cause().is_none());
716699
}
717-
718700
}

0 commit comments

Comments
 (0)