Remove symbol error type

This commit is contained in:
Adrian Heine 2017-02-07 13:54:06 +01:00
parent 55f2cfae95
commit 4be608a002
10 changed files with 85 additions and 108 deletions

View file

@ -43,27 +43,24 @@ pub trait UserAdder {
}
#[derive(Debug, PartialEq)]
pub enum UserError<E: Error> {
GenericError,
ExecError(E)
pub enum UserError {
GenericError
}
impl<E: Error> Error for UserError<E> {
impl Error for UserError {
fn description(&self) -> &str {
match self {
&UserError::GenericError => "Could not find out if user exists",
&UserError::ExecError(_) => "Error executing symbol"
&UserError::GenericError => "Could not find out if user exists"
}
}
fn cause(&self) -> Option<&Error> {
match self {
&UserError::ExecError(ref e) => Some(e),
_ => None
}
}
}
impl<E: Error> fmt::Display for UserError<E> {
impl fmt::Display for UserError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.cause() {
Some(e) => write!(f, "{} (cause: {})", self.description(), e),
@ -94,22 +91,21 @@ impl<'a, E: Error, A: UserAdder<SubE=E>> fmt::Display for User<'a, E, A> {
}
}
impl<'a, E: Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
type Error = UserError<UserAdderError<E>>;
fn target_reached(&self) -> Result<bool, Self::Error> {
impl<'a, E: 'static + Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let output = self.command_runner.run_with_args("getent", &["passwd", self.user_name]);
match output {
Ok(output) => match output.status.code() {
Some(2) => Ok(false),
Some(0) => Ok(true),
_ => Err(UserError::GenericError)
_ => Err(Box::new(UserError::GenericError))
},
Err(_) => Err(UserError::GenericError)
Err(e) => Err(Box::new(e))
}
}
fn execute(&self) -> Result<(), Self::Error> {
self.user_adder.add_user(self.user_name).map_err(|e| UserError::ExecError(e))
fn execute(&self) -> Result<(), Box<Error>> {
self.user_adder.add_user(self.user_name).map_err(|e| Box::new(e) as Box<Error>)
}
}
@ -192,12 +188,12 @@ mod test {
#[test]
fn test_target_reached_nonexisting() {
let symbol = User { user_name: "nonexisting", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
assert_eq!(symbol.target_reached(), Ok(false));
assert_eq!(symbol.target_reached().unwrap(), false);
}
#[test]
fn test_target_reached_root() {
let symbol = User { user_name: "root", command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
assert_eq!(symbol.target_reached(), Ok(true));
assert_eq!(symbol.target_reached().unwrap(), true);
}
}