-
Notifications
You must be signed in to change notification settings - Fork 61
/
implicit.rs
190 lines (154 loc) · 4.41 KB
/
implicit.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
use snafu::prelude::*;
mod basics {
use super::*;
use std::sync::atomic::{AtomicUsize, Ordering};
static COUNTER: AtomicUsize = AtomicUsize::new(0);
#[derive(Debug)]
struct OccurrenceCount(usize);
impl snafu::GenerateImplicitData for OccurrenceCount {
fn generate() -> Self {
OccurrenceCount(COUNTER.fetch_add(1, Ordering::SeqCst))
}
}
#[derive(Debug, Snafu)]
enum ErrorOne {
Alpha {
#[snafu(implicit)]
occurrence: OccurrenceCount,
},
}
#[test]
fn implicit_fields_are_constructed() {
let ErrorOne::Alpha {
occurrence: OccurrenceCount(o1),
} = AlphaSnafu.build();
let ErrorOne::Alpha {
occurrence: OccurrenceCount(o2),
} = AlphaSnafu.build();
assert_eq!(o1, 0);
assert_eq!(o2, 1);
}
}
mod multiple_fields {
use super::*;
#[derive(Debug, PartialEq)]
struct ImplicitData;
impl snafu::GenerateImplicitData for ImplicitData {
fn generate() -> Self {
ImplicitData
}
}
#[derive(Debug, Snafu)]
struct Error {
#[snafu(implicit)]
one: ImplicitData,
#[snafu(implicit)]
two: ImplicitData,
}
#[test]
fn multiple_implicit_fields_are_constructed() {
let Error { one, two } = Snafu.build();
assert_eq!(one, two);
}
}
mod with_and_without_source {
use snafu::{prelude::*, FromString, IntoError};
#[derive(Debug, PartialEq)]
enum ItWas {
Generate,
GenerateWithSource,
}
#[derive(Debug)]
struct ImplicitData(ItWas);
impl snafu::GenerateImplicitData for ImplicitData {
fn generate() -> Self {
Self(ItWas::Generate)
}
fn generate_with_source(_: &dyn snafu::Error) -> Self {
Self(ItWas::GenerateWithSource)
}
}
#[derive(Debug, Snafu)]
struct InnerError;
#[derive(Debug, Snafu)]
struct HasSource {
source: InnerError,
#[snafu(implicit)]
data: ImplicitData,
}
#[derive(Debug, Snafu)]
struct NoSource {
#[snafu(implicit)]
data: ImplicitData,
}
#[derive(Debug, Snafu)]
#[snafu(context(false))]
struct HasSourceNoContext {
source: InnerError,
#[snafu(implicit)]
data: ImplicitData,
}
#[derive(Debug, Snafu)]
#[snafu(whatever, display("{message}"))]
struct MyOwnWhatever {
message: String,
#[snafu(source(from(Box<dyn std::error::Error>, Some)))]
source: Option<Box<dyn std::error::Error>>,
#[snafu(implicit)]
data: ImplicitData,
}
#[test]
fn calls_generate_for_no_source() {
let e = NoSourceSnafu.build();
assert_eq!(e.data.0, ItWas::Generate);
}
#[test]
fn calls_generate_with_source_for_source() {
let e = HasSourceSnafu.into_error(InnerError);
assert_eq!(e.data.0, ItWas::GenerateWithSource);
}
#[test]
fn calls_generate_for_none() {
let e = NoSourceSnafu.into_error(snafu::NoneError);
assert_eq!(e.data.0, ItWas::Generate);
}
#[test]
fn calls_generate_with_source_for_no_context() {
let e = HasSourceNoContext::from(InnerError);
assert_eq!(e.data.0, ItWas::GenerateWithSource);
}
#[test]
fn calls_generate_for_whatever_with_no_source() {
let e = MyOwnWhatever::without_source("bang".into());
assert_eq!(e.data.0, ItWas::Generate);
}
#[test]
fn calls_generate_with_source_for_whatever_with_source() {
let e = MyOwnWhatever::with_source(Box::new(InnerError), "bang".into());
assert_eq!(e.data.0, ItWas::GenerateWithSource);
}
}
mod converted_sources {
use snafu::{prelude::*, IntoError};
#[derive(Debug)]
struct ImplicitData;
impl snafu::GenerateImplicitData for ImplicitData {
fn generate() -> Self {
Self
}
}
#[derive(Debug, Snafu)]
struct HasSource {
backtrace: snafu::Backtrace,
#[snafu(implicit)]
data: ImplicitData,
#[snafu(source(from(String, Into::into)))]
source: Box<dyn std::error::Error>,
}
#[test]
fn receives_the_error_after_conversion() {
let e = HasSourceSnafu.into_error(String::from("bad"));
// Mostly testing that this compiles; assertion is bonus
assert_eq!(e.source.to_string(), "bad");
}
}