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};

/// An implementation of `Error` which may or may not include a scope and/or usage message.
pub struct ArgsError {
    desc: String
}

impl ArgsError {
    /// Creates a new `ArgsError` with the provided `scope` and `msg`.
    /// If `scope` is an empty string (i.e. `""`) it will be ignored.
    pub fn new(scope: &str, msg: &str) -> ArgsError {
        Self::new_with_usage(scope, msg, "")
    }

    /// Creates a new `ArgsError` with the provided `scope`, `msg` and `usage` message.
    /// If either `scope` or `usage` are an empty string (i.e. `""`) they will be ignored.
    pub fn new_with_usage(scope: &str, msg: &str, usage: &str) -> ArgsError {
        // If there is a scope, append it to the front
        let mut desc = if scope.to_string().is_empty() {
            String::new()
        } else {
            format!("{}: ", scope)
        };

        // Append the error message
        desc.push_str(msg);

        // Append the usage message, if it exists
        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
    }
}