Adrian Heine
8 years ago
6 changed files with 81 additions and 0 deletions
-
11src/resources/mod.rs
-
49src/symbols/dir.rs
-
6src/symbols/file.rs
-
5src/symbols/mod.rs
-
5src/symbols/nginx/server.rs
-
5src/symbols/systemd/node_js_user_service.rs
@ -0,0 +1,49 @@ |
|||
use std::borrow::Cow;
|
|||
use std::error::Error;
|
|||
use std::fmt;
|
|||
use std::fs;
|
|||
use std::io;
|
|||
use std::io::{Read, Write};
|
|||
use std::ops::Deref;
|
|||
use std::path::Path;
|
|||
|
|||
use symbols::Symbol;
|
|||
|
|||
pub struct Dir<D> where D: AsRef<str> + fmt::Display {
|
|||
path: D
|
|||
}
|
|||
|
|||
impl<D> Dir<D> where D: AsRef<str> + fmt::Display {
|
|||
pub fn new(path: D) -> Self {
|
|||
Dir { path: path }
|
|||
}
|
|||
}
|
|||
|
|||
impl<D> Symbol for Dir<D> where D: AsRef<str> + fmt::Display {
|
|||
fn target_reached(&self) -> Result<bool, Box<Error>> {
|
|||
let metadata = fs::metadata(self.path.as_ref());
|
|||
// Check if dir exists
|
|||
if let Err(e) = metadata {
|
|||
return if e.kind() == io::ErrorKind::NotFound {
|
|||
Ok(false)
|
|||
} else {
|
|||
Err(Box::new(e))
|
|||
};
|
|||
}
|
|||
if metadata.unwrap().is_dir() {
|
|||
Ok(true)
|
|||
} else {
|
|||
Err(Box::new(io::Error::new(io::ErrorKind::AlreadyExists, "Could not create a directory, non-directory file exists")))
|
|||
}
|
|||
}
|
|||
|
|||
fn execute(&self) -> Result<(), Box<Error>> {
|
|||
fs::create_dir_all(self.path.as_ref()).map_err(|e| Box::new(e) as Box<Error>)
|
|||
}
|
|||
}
|
|||
|
|||
impl<D> fmt::Display for Dir<D> where D: AsRef<str> + fmt::Display {
|
|||
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
|
|||
write!(f, "Dir {}", self.path)
|
|||
}
|
|||
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue