Browse Source

WIP Repository and resources

master
Adrian Heine 8 years ago
parent
commit
124b639e50
  1. 2
      src/lib.rs
  2. 30
      src/repository.rs
  3. 13
      src/resources/mod.rs
  4. 9
      src/symbols/user.rs

2
src/lib.rs

@ -24,3 +24,5 @@ pub mod command_runner;
pub mod loggers;
pub mod symbols;
pub mod schema;
pub mod repository;
pub mod resources;

30
src/repository.rs

@ -0,0 +1,30 @@
use std::collections::HashMap;
use symbols::Symbol;
use resources::Resource;
pub trait SymbolRepository<'a> {
fn get_symbol(&self, resource: &Resource) -> Option<Box<Symbol + 'a>>;
}
impl<'a, C> SymbolRepository<'a> for C where C: Fn(&Resource) -> Option<Box<Symbol + 'a>> {
fn get_symbol(&self, resource: &Resource) -> Option<Box<Symbol + 'a>> {
self(resource)
}
}
pub struct DispatchingSymbolRepository<'a> {
repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>
}
impl<'a> DispatchingSymbolRepository<'a> {
pub fn new(repositories: HashMap<&'a str, Box<SymbolRepository<'a> + 'a>>) -> DispatchingSymbolRepository<'a> {
DispatchingSymbolRepository { repositories: repositories }
}
}
impl<'a> SymbolRepository<'a> for DispatchingSymbolRepository<'a> {
fn get_symbol(&self, resource: &Resource) -> Option<Box<Symbol + 'a>> {
self.repositories.get(resource.get_type()).and_then(|repo| repo.get_symbol(resource))
}
}

13
src/resources/mod.rs

@ -0,0 +1,13 @@
pub trait Resource {
fn get_type(&self) -> &str;
fn get_value(&self) -> &str;
}
pub struct UserResource<'a> {
pub name: &'a str
}
impl<'a> Resource for UserResource<'a> {
fn get_type(&self) -> &str { "user" }
fn get_value(&self) -> &str { self.name }
}

9
src/symbols/user.rs

@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::io::Error as IoError;
@ -70,13 +71,13 @@ impl fmt::Display for UserError {
}
pub struct User<'a, E, A> where E: Error + Sized, A: 'a + UserAdder<SubE=E> {
user_name: &'a str,
user_name: Cow<'a, str>,
command_runner: &'a CommandRunner,
user_adder: &'a A
}
impl<'a, E: Error + Sized, A: 'a + UserAdder<SubE=E>> User<'a, E, A> {
pub fn new(user_name: &'a str, command_runner: &'a CommandRunner, user_adder: &'a A) -> User<'a, E, A> {
pub fn new(user_name: Cow<'a, str>, command_runner: &'a CommandRunner, user_adder: &'a A) -> User<'a, E, A> {
User {
user_name: user_name,
command_runner: command_runner,
@ -93,7 +94,7 @@ impl<'a, E: Error, A: UserAdder<SubE=E>> fmt::Display for User<'a, E, A> {
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]);
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),
@ -105,7 +106,7 @@ impl<'a, E: 'static + Error, A: UserAdder<SubE=E>> Symbol for User<'a, E, A> {
}
fn execute(&self) -> Result<(), Box<Error>> {
self.user_adder.add_user(self.user_name).map_err(|e| Box::new(e) as Box<Error>)
self.user_adder.add_user(&*self.user_name).map_err(|e| Box::new(e) as Box<Error>)
}
}

Loading…
Cancel
Save