Cargo format

This commit is contained in:
Adrian Heine 2019-09-12 22:59:32 +02:00
parent 9bab810b91
commit 8c0224e983
44 changed files with 1784 additions and 611 deletions

View file

@ -10,7 +10,7 @@ use symbols::{Action, OwnedSymbolAction, Symbol, SymbolAction, SymbolRunner};
pub enum UserAdderError {
AlreadyExists,
UnknownError,
ImplError(Box<dyn Error>)
ImplError(Box<dyn Error>),
}
impl Error for UserAdderError {
@ -18,13 +18,13 @@ impl Error for UserAdderError {
match self {
UserAdderError::AlreadyExists => "User already exists",
UserAdderError::UnknownError => "Unknown error",
UserAdderError::ImplError(_) => "User adding error"
UserAdderError::ImplError(_) => "User adding error",
}
}
fn cause(&self) -> Option<&dyn Error> {
match self {
UserAdderError::ImplError(ref e) => Some(e.as_ref()),
_ => None
_ => None,
}
}
}
@ -33,7 +33,7 @@ impl fmt::Display for UserAdderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.cause() {
Some(e) => write!(f, "{} (cause: {})", self.description(), e),
None => write!(f, "{}", self.description())
None => write!(f, "{}", self.description()),
}
}
}
@ -44,18 +44,18 @@ pub trait UserAdder {
#[derive(Debug, PartialEq)]
pub enum UserError {
GenericError
GenericError,
}
impl Error for UserError {
fn description(&self) -> &str {
match self {
UserError::GenericError => "Could not find out if user exists"
UserError::GenericError => "Could not find out if user exists",
}
}
fn cause(&self) -> Option<&dyn Error> {
match self {
_ => None
_ => None,
}
}
}
@ -64,7 +64,7 @@ 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),
None => write!(f, "{}", self.description())
None => write!(f, "{}", self.description()),
}
}
}
@ -72,12 +72,16 @@ impl fmt::Display for UserError {
pub struct User<'a, C: 'a + CommandRunner, A: 'a + UserAdder> {
user_name: Cow<'a, str>,
command_runner: &'a C,
user_adder: &'a A
user_adder: &'a A,
}
impl<'a, C: CommandRunner, A: 'a + UserAdder> User<'a, C, A> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a C, user_adder: &'a A) -> Self {
User { user_name, command_runner, user_adder }
User {
user_name,
command_runner,
user_adder,
}
}
}
@ -89,16 +93,21 @@ impl<'a, C: CommandRunner, A: 'a + UserAdder> fmt::Display for User<'a, C, A> {
impl<'a, C: CommandRunner, A: 'a + UserAdder> Symbol for User<'a, C, A> {
fn target_reached(&self) -> Result<bool, Box<dyn Error>> {
let output = try!(self.command_runner.run_with_args("getent", &["passwd", &*self.user_name]));
let output = try!(self
.command_runner
.run_with_args("getent", &["passwd", &*self.user_name]));
match output.status.code() {
Some(2) => Ok(false),
Some(0) => Ok(true),
_ => Err(Box::new(UserError::GenericError))
_ => Err(Box::new(UserError::GenericError)),
}
}
fn execute(&self) -> Result<(), Box<dyn Error>> {
self.user_adder.add_user(&*self.user_name).map_err(|e| Box::new(e) as Box<dyn Error>)
self
.user_adder
.add_user(&*self.user_name)
.map_err(|e| Box::new(e) as Box<dyn Error>)
}
fn provides(&self) -> Option<Vec<Resource>> {
@ -109,13 +118,16 @@ impl<'a, C: CommandRunner, A: 'a + UserAdder> Symbol for User<'a, C, A> {
Box::new(SymbolAction::new(runner, self))
}
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b> where Self: 'b {
fn into_action<'b>(self: Box<Self>, runner: &'b dyn SymbolRunner) -> Box<dyn Action + 'b>
where
Self: 'b,
{
Box::new(OwnedSymbolAction::new(runner, *self))
}
}
pub struct SystemUserAdder<'a, C: 'a + CommandRunner> {
command_runner: &'a C
command_runner: &'a C,
}
impl<'a, C: CommandRunner> SystemUserAdder<'a, C> {
@ -130,27 +142,26 @@ impl<'a, C: CommandRunner> UserAdder for SystemUserAdder<'a, C> {
"adduser",
&[
// "-m", // Necessary for Fedora, not accepted in Debian
"--system",
user_name
]);
"--system", user_name,
],
);
match output {
Ok(output) => match output.status.code() {
Some(0) => Ok(()),
Some(1) =>
{
println!("{:?}", output);
Err(UserAdderError::AlreadyExists)},
Some(_) =>
{
println!("{:?}", output);
Err(UserAdderError::UnknownError)
},
Some(1) => {
println!("{:?}", output);
Err(UserAdderError::AlreadyExists)
}
Some(_) => {
println!("{:?}", output);
Err(UserAdderError::UnknownError)
}
None => {
println!("{:?}", output);
Err(UserAdderError::UnknownError)
},
println!("{:?}", output);
Err(UserAdderError::UnknownError)
}
},
Err(e) => Err(UserAdderError::ImplError(Box::new(e)))
Err(e) => Err(UserAdderError::ImplError(Box::new(e))),
}
}
}
@ -161,10 +172,10 @@ mod test {
use std::fmt;
use command_runner::StdCommandRunner;
use symbols::Symbol;
use symbols::user::User;
use symbols::user::UserAdder;
use symbols::user::UserAdderError;
use symbols::Symbol;
#[derive(Debug, PartialEq)]
struct DummyError;
@ -190,13 +201,21 @@ mod test {
#[test]
fn test_target_reached_nonexisting() {
let symbol = User { user_name: "nonexisting".into(), command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
let symbol = User {
user_name: "nonexisting".into(),
command_runner: &StdCommandRunner,
user_adder: &DummyUserAdder,
};
assert_eq!(symbol.target_reached().unwrap(), false);
}
#[test]
fn test_target_reached_root() {
let symbol = User { user_name: "root".into(), command_runner: &StdCommandRunner, user_adder: &DummyUserAdder };
let symbol = User {
user_name: "root".into(),
command_runner: &StdCommandRunner,
user_adder: &DummyUserAdder,
};
assert_eq!(symbol.target_reached().unwrap(), true);
}
}