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