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.

56 lines
1.4 KiB

8 years ago
5 years ago
8 years ago
8 years ago
8 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 years ago
5 years ago
8 years ago
5 years ago
5 years ago
8 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(
  26. SystemTime::now()
  27. .duration_since(UNIX_EPOCH)
  28. .unwrap()
  29. .as_secs(),
  30. ))
  31. }
  32. fn read_filename(&self) -> Result<String, Box<dyn Error>> {
  33. Ok(self.get_path(Some(self.recent_date()?)))
  34. }
  35. fn recent_date(&self) -> Result<u64, Box<dyn Error>> {
  36. let dir = self.get_path(None);
  37. read_dir(dir)?
  38. .map(|entry| {
  39. entry
  40. .ok()
  41. .and_then(|e| e.file_name().into_string().ok())
  42. .and_then(|filename| u64::from_str(&filename).ok())
  43. })
  44. .fold(None, |maybe_newest, maybe_time| {
  45. maybe_newest.into_iter().chain(maybe_time).max()
  46. })
  47. .ok_or_else(|| "Not found".to_string().into())
  48. }
  49. }