58 lines
1.6 KiB
Rust
58 lines
1.6 KiB
Rust
use futures::{TryStream, TryStreamExt as _};
|
|
use snafu::{OptionExt as _, ResultExt as _, Snafu};
|
|
use std::num::ParseIntError;
|
|
|
|
use super::{CreateListerSnafu, ListError, Month, RecordingManager, Year};
|
|
|
|
pub type Day = u8;
|
|
|
|
#[derive(Debug, Snafu)]
|
|
pub enum TakeError {
|
|
/// days are supposed to be directories, but this wasn't (because it didn't end with `/`)
|
|
NotADirectory,
|
|
|
|
/// could not parse the day as an integer
|
|
ParseIntegerError { source: ParseIntError },
|
|
}
|
|
|
|
pub fn take(s: &str) -> Result<(Day, &str), TakeError> {
|
|
let (day, rest) = s.split_once('/').context(NotADirectorySnafu)?;
|
|
|
|
let day = day.parse().context(ParseIntegerSnafu)?;
|
|
|
|
Ok((day, rest))
|
|
}
|
|
|
|
#[derive(Debug, Snafu)]
|
|
pub enum DayEntryError {
|
|
/// failed to get an entry from the storage operator's lister
|
|
ReceiveEntryError { source: opendal::Error },
|
|
|
|
/// failed to parse the entry as a day
|
|
ParseError { source: TakeError },
|
|
}
|
|
|
|
impl RecordingManager {
|
|
pub async fn days(
|
|
&self,
|
|
year: Year,
|
|
month: Month,
|
|
) -> Result<impl TryStream<Ok = Day, Error = DayEntryError> + Unpin, ListError> {
|
|
let lister = self
|
|
.operator
|
|
.lister(&format!("{year}/{month}/"))
|
|
.await
|
|
.context(CreateListerSnafu)?;
|
|
|
|
Ok(lister
|
|
.map_err(|error| DayEntryError::ReceiveEntryError { source: error })
|
|
.and_then(|entry| {
|
|
std::future::ready(
|
|
take(entry.name())
|
|
.map(|(day, _rest)| day)
|
|
.context(ParseSnafu),
|
|
)
|
|
}))
|
|
}
|
|
}
|