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.

43 lines
1.3 KiB

use std::error::Error;
use std::fs::read_dir;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
pub trait Storage {
fn write_filename(&self) -> String;
fn read_filename(&self) -> Result<String, Box<Error>>;
fn recent_date(&self) -> Result<u64, Box<Error>>;
}
pub struct SimpleStorage(String, String);
impl SimpleStorage {
pub fn new(base: String, filename: String) -> Self {
SimpleStorage(base, filename)
}
fn get_path(&self, date: Option<u64>) -> String {
match date {
Some(d) => format!("{}/_{}/{}", self.0, self.1, d),
None => format!("{}/_{}", self.0, self.1)
}
}
}
impl Storage for SimpleStorage {
fn write_filename(&self) -> String {
self.get_path(Some(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()))
}
fn read_filename(&self) -> Result<String, Box<Error>> {
Ok(self.get_path(Some(try!(self.recent_date()))))
}
fn recent_date(&self) -> Result<u64, Box<Error>> {
let dir = self.get_path(None);
try!(read_dir(dir))
.map(|entry| entry.ok().and_then(|e| e.file_name().into_string().ok()).and_then(|filename| u64::from_str(&filename).ok()))
.fold(None, |maybe_newest, maybe_time| maybe_newest.into_iter().chain(maybe_time).max())
.ok_or("Not found".to_string().into())
}
}