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
use std::error::Error;
use std::fmt::{self,Debug,Display,Formatter};
pub struct ArgsError {
desc: String
}
impl ArgsError {
pub fn new(scope: &str, msg: &str) -> ArgsError {
Self::new_with_usage(scope, msg, "")
}
pub fn new_with_usage(scope: &str, msg: &str, usage: &str) -> ArgsError {
let mut desc = if scope.to_string().is_empty() {
String::new()
} else {
format!("{}: ", scope)
};
desc.push_str(msg);
if !usage.to_string().is_empty() { desc.push_str(&format!("\n\n{}", usage)); }
ArgsError { desc: desc }
}
}
impl Debug for ArgsError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.desc)
}
}
impl Display for ArgsError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.desc)
}
}
impl Error for ArgsError {
fn description(&self) -> &str {
&self.desc
}
}