forked from tokio-rs/tokio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.rs
61 lines (55 loc) · 2.16 KB
/
error.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
use std::fmt::Write;
/// Returns an error string explaining that the Tokio context hasn't been instantiated.
pub(crate) fn context_missing_error(features: &[&str]) -> String {
// TODO: Include Tokio version
let sfx = if !features.is_empty() {
let mut sfx = String::from(" with ");
for (i, feat) in features.iter().enumerate() {
if i == 0 {
if features.len() > 1 {
write!(&mut sfx, "either ").expect("failed to write to string");
}
} else if i == features.len() - 1 {
write!(&mut sfx, " or ").expect("failed to write to string");
} else {
write!(&mut sfx, ", ").expect("failed to write to string");
}
write!(&mut sfx, "{}", feat).expect("failed to write to string");
}
write!(&mut sfx, " enabled").expect("failed to write to string");
sfx
} else {
String::new()
};
format!(
"there is no reactor running, must be called from the context of a Tokio 1.x runtime{}",
sfx
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_context_missing_error_no_features() {
assert_eq!(
&context_missing_error(&[]),
"there is no reactor running, must be called from the context of a Tokio 1.x runtime"
);
}
#[test]
fn test_context_missing_error_one_feature() {
assert_eq!(&context_missing_error(&["rt"]),
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with rt enabled");
}
#[test]
fn test_context_missing_error_two_features() {
assert_eq!(&context_missing_error(&["rt", "signal"]),
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with either rt or signal enabled");
}
#[test]
fn test_context_missing_error_three_features() {
assert_eq!(&context_missing_error(&["rt", "signal", "sync"]),
"there is no reactor running, must be called from the context of a Tokio 1.x runtime with either rt, signal or sync enabled"
);
}
}