-
Notifications
You must be signed in to change notification settings - Fork 61
/
lib.rs
110 lines (85 loc) · 2.28 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![cfg(test)]
// There are not yet any feature differences that require testing for Rust 1.34 compatibility.
use std::io;
fn io_failure() -> io::Result<()> {
Err(io::Error::new(io::ErrorKind::Other, "arbitrary failure"))
}
fn implements_error<T: std::error::Error>() {}
mod enum_style {
use super::*;
use snafu::prelude::*;
#[derive(Debug, Snafu)]
enum Error {
#[snafu(display("Without a source: {}", id))]
WithoutSource { id: i32 },
#[snafu(display("With a source: {}", source))]
WithSource { id: i32, source: io::Error },
}
type Result<T, E = Error> = std::result::Result<T, E>;
fn create_without_source() -> Result<()> {
WithoutSourceSnafu { id: 42 }.fail()
}
fn create_with_source() -> Result<()> {
io_failure().context(WithSourceSnafu { id: 42 })
}
#[test]
fn it_works() {
implements_error::<Error>();
let _ = create_without_source();
let _ = create_with_source();
}
}
mod struct_style {
use super::*;
use snafu::prelude::*;
#[derive(Debug, Snafu)]
#[snafu(display("Without a source: {}", id))]
struct WithoutSource {
id: i32,
}
#[derive(Debug, Snafu)]
#[snafu(display("With a source: {}", source))]
struct WithSource {
id: i32,
source: io::Error,
}
fn create_without_source() -> Result<(), WithoutSource> {
WithoutSourceSnafu { id: 42 }.fail()
}
fn create_with_source() -> Result<(), WithSource> {
io_failure().context(WithSourceSnafu { id: 42 })
}
#[test]
fn it_works() {
implements_error::<WithoutSource>();
implements_error::<WithSource>();
let _ = create_without_source();
let _ = create_with_source();
}
}
mod opaque_style {
use super::*;
use snafu::prelude::*;
#[derive(Debug, Snafu)]
struct Dummy;
#[derive(Debug, Snafu)]
struct Opaque(Dummy);
fn create() -> Result<(), Opaque> {
Ok(DummySnafu.fail()?)
}
#[test]
fn it_works() {
implements_error::<Opaque>();
let _ = create();
}
}
mod report {
use snafu::prelude::*;
#[derive(Debug, Snafu)]
struct Error;
#[test]
#[snafu::report]
fn it_works() -> Result<(), Error> {
Ok(())
}
}