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
use std::env;
use std::ffi::OsStr;
use std::path::PathBuf;
use std::str::FromStr;
use failure::{Error, Fail};
use log::Level;
pub fn absolute_from_os_str(path: &OsStr) -> PathBuf {
let mut current = env::current_dir().unwrap_or_else(|e| {
warn!(
"Some paths may not be turned to absolute. Couldn't read current dir: {}",
e,
);
PathBuf::new()
});
current.push(path);
if let Ok(canonicized) = current.canonicalize() {
canonicized
} else {
current
}
}
#[derive(Debug, Fail)]
#[fail(display = "Missing = in map option")]
pub struct MissingEquals;
pub fn key_val<K, V>(opt: &str) -> Result<(K, V), Error>
where
K: FromStr,
K::Err: Fail + 'static,
V: FromStr,
V::Err: Fail + 'static,
{
let pos = opt.find('=').ok_or(MissingEquals)?;
Ok((opt[..pos].parse()?, opt[pos + 1..].parse()?))
}
pub fn log_error(target: &str, e: &Error) {
for cause in e.iter_chain() {
error!(target: target, "{}", cause);
}
if log_enabled!(Level::Debug) {
let bt = format!("{}", e.backtrace());
if !bt.is_empty() {
debug!(target: target, "{}", bt);
}
}
}
pub fn log_errors_named<R, F>(target: &str, f: F) -> Result<R, Error>
where
F: FnOnce() -> Result<R, Error>,
{
let result = f();
if let Err(ref e) = result {
log_error(target, e);
}
result
}
pub fn log_errors<R, F: FnOnce() -> Result<R, Error>>(f: F) -> Result<R, Error> {
log_errors_named("spirit", f)
}
#[cfg(test)]
mod tests {
use std::ffi::OsString;
use std::net::{AddrParseError, IpAddr};
use std::num::ParseIntError;
use super::*;
#[test]
fn abs() {
let current = env::current_dir().unwrap();
let parent = absolute_from_os_str(&OsString::from(".."));
assert!(parent.is_absolute());
assert!(current.starts_with(parent));
let child = absolute_from_os_str(&OsString::from("this-likely-doesn't-exist"));
assert!(child.is_absolute());
assert!(child.starts_with(current));
}
#[test]
fn key_val_success() {
assert_eq!(
("hello".to_owned(), "world".to_owned()),
key_val("hello=world").unwrap()
);
let ip: IpAddr = "192.0.2.1".parse().unwrap();
assert_eq!(("ip".to_owned(), ip), key_val("ip=192.0.2.1").unwrap());
assert_eq!(("count".to_owned(), 4), key_val("count=4").unwrap());
}
#[test]
fn key_val_extra_equals() {
assert_eq!(
("greeting".to_owned(), "hello=world".to_owned()),
key_val("greeting=hello=world").unwrap(),
);
}
#[test]
fn key_val_parse_fail() {
key_val::<String, IpAddr>("hello=192.0.2.1.0")
.unwrap_err()
.downcast_ref::<AddrParseError>()
.expect("Different error returned");
key_val::<usize, String>("hello=world")
.unwrap_err()
.downcast_ref::<ParseIntError>()
.expect("Different error returned");
}
#[test]
fn key_val_missing_eq() {
key_val::<String, String>("no equal sign")
.unwrap_err()
.downcast_ref::<MissingEquals>()
.expect("Different error returned");
}
}