A library for writing host-specific, single-binary configuration management and deployment tools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.6 KiB

use std::borrow::Cow;
use std::error::Error;
use std::fmt;
use std::fs::File as FsFile;
use std::io;
use std::io::{Read, Write};
use std::ops::Deref;
use std::path::Path;
use symbols::Symbol;
pub struct File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
path: D,
content: C
}
impl<C, D> File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
pub fn new(path: D, content: C) -> Self {
File {
path: path,
content: content
}
}
}
impl<C, D> Symbol for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
fn target_reached(&self) -> Result<bool, Box<Error>> {
let file = FsFile::open(self.path.as_ref());
// Check if file exists
if let Err(e) = file {
return if e.kind() == io::ErrorKind::NotFound {
Ok(false)
} else {
Err(Box::new(e))
};
}
// Check if content is the same
let mut file_content = file.unwrap().bytes();
let mut target_content = self.content.bytes();
loop {
match (file_content.next(), target_content.next()) {
(None, None) => return Ok(true),
(Some(Ok(a)), Some(b)) if a == b => {},
(Some(Err(e)), _) => return Err(Box::new(e)),
(_, _) => return Ok(false)
}
}
}
fn execute(&self) -> Result<(), Box<Error>> {
let mut file = try!(FsFile::create(self.path.as_ref()));
try!(file.write_all(self.content.as_bytes()));
Ok(())
}
}
impl<C, D> fmt::Display for File<C, D> where C: Deref<Target=str>, D: AsRef<str> + fmt::Display {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error>{
write!(f, "File {}", self.path)
}
}