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.

44 lines
1.3 KiB

7 years ago
5 years ago
7 years ago
7 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
5 years ago
7 years ago
  1. use std::error::Error;
  2. use std::fs::read_dir;
  3. use std::str::FromStr;
  4. use std::time::{SystemTime, UNIX_EPOCH};
  5. pub trait Storage {
  6. fn write_filename(&self) -> String;
  7. fn read_filename(&self) -> Result<String, Box<dyn Error>>;
  8. fn recent_date(&self) -> Result<u64, Box<dyn Error>>;
  9. }
  10. #[derive(Clone)]
  11. pub struct SimpleStorage(String, String);
  12. impl SimpleStorage {
  13. pub fn new(base: String, filename: String) -> Self {
  14. SimpleStorage(base, filename)
  15. }
  16. fn get_path(&self, date: Option<u64>) -> String {
  17. match date {
  18. Some(d) => format!("{}/_{}/{}", self.0, self.1, d),
  19. None => format!("{}/_{}", self.0, self.1)
  20. }
  21. }
  22. }
  23. impl Storage for SimpleStorage {
  24. fn write_filename(&self) -> String {
  25. self.get_path(Some(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_secs()))
  26. }
  27. fn read_filename(&self) -> Result<String, Box<dyn Error>> {
  28. Ok(self.get_path(Some(try!(self.recent_date()))))
  29. }
  30. fn recent_date(&self) -> Result<u64, Box<dyn Error>> {
  31. let dir = self.get_path(None);
  32. try!(read_dir(dir))
  33. .map(|entry| entry.ok().and_then(|e| e.file_name().into_string().ok()).and_then(|filename| u64::from_str(&filename).ok()))
  34. .fold(None, |maybe_newest, maybe_time| maybe_newest.into_iter().chain(maybe_time).max())
  35. .ok_or_else(|| "Not found".to_string().into())
  36. }
  37. }