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
58 lines
1.4 KiB
use std::error::Error;
|
|
use std::fs::read_dir;
|
|
use std::path::PathBuf;
|
|
use std::str::FromStr;
|
|
use std::time::{SystemTime, UNIX_EPOCH};
|
|
|
|
pub trait Storage {
|
|
fn write_filename(&self) -> PathBuf;
|
|
fn read_filename(&self) -> Result<PathBuf, Box<dyn Error>>;
|
|
fn recent_date(&self) -> Result<u64, Box<dyn Error>>;
|
|
}
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub struct SimpleStorage(PathBuf);
|
|
|
|
impl SimpleStorage {
|
|
#[must_use]
|
|
pub const fn new(base: PathBuf) -> Self {
|
|
Self(base)
|
|
}
|
|
|
|
fn get_path(&self, date: Option<u64>) -> PathBuf {
|
|
match date {
|
|
Some(d) => self.0.join(d.to_string()),
|
|
None => self.0.clone(),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Storage for SimpleStorage {
|
|
fn write_filename(&self) -> PathBuf {
|
|
self.get_path(Some(
|
|
SystemTime::now()
|
|
.duration_since(UNIX_EPOCH)
|
|
.unwrap()
|
|
.as_secs(),
|
|
))
|
|
}
|
|
|
|
fn read_filename(&self) -> Result<PathBuf, Box<dyn Error>> {
|
|
Ok(self.get_path(Some(self.recent_date()?)))
|
|
}
|
|
|
|
fn recent_date(&self) -> Result<u64, Box<dyn Error>> {
|
|
let dir = self.get_path(None);
|
|
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_else(|| "Not found".to_string().into())
|
|
}
|
|
}
|