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.

58 lines
1.4 KiB

8 years ago
8 years ago
5 years ago
8 years ago
8 years ago
4 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
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::path::PathBuf;
  4. use std::str::FromStr;
  5. use std::time::{SystemTime, UNIX_EPOCH};
  6. pub trait Storage {
  7. fn write_filename(&self) -> PathBuf;
  8. fn read_filename(&self) -> Result<PathBuf, Box<dyn Error>>;
  9. fn recent_date(&self) -> Result<u64, Box<dyn Error>>;
  10. }
  11. #[derive(Debug, Clone)]
  12. pub struct SimpleStorage(PathBuf);
  13. impl SimpleStorage {
  14. #[must_use]
  15. pub const fn new(base: PathBuf) -> Self {
  16. Self(base)
  17. }
  18. fn get_path(&self, date: Option<u64>) -> PathBuf {
  19. match date {
  20. Some(d) => self.0.join(d.to_string()),
  21. None => self.0.clone(),
  22. }
  23. }
  24. }
  25. impl Storage for SimpleStorage {
  26. fn write_filename(&self) -> PathBuf {
  27. self.get_path(Some(
  28. SystemTime::now()
  29. .duration_since(UNIX_EPOCH)
  30. .unwrap()
  31. .as_secs(),
  32. ))
  33. }
  34. fn read_filename(&self) -> Result<PathBuf, Box<dyn Error>> {
  35. Ok(self.get_path(Some(self.recent_date()?)))
  36. }
  37. fn recent_date(&self) -> Result<u64, Box<dyn Error>> {
  38. let dir = self.get_path(None);
  39. read_dir(dir)?
  40. .map(|entry| {
  41. entry
  42. .ok()
  43. .and_then(|e| e.file_name().into_string().ok())
  44. .and_then(|filename| u64::from_str(&filename).ok())
  45. })
  46. .fold(None, |maybe_newest, maybe_time| {
  47. maybe_newest.into_iter().chain(maybe_time).max()
  48. })
  49. .ok_or_else(|| "Not found".to_string().into())
  50. }
  51. }