use url::Url;
use super::search::Bound;
use super::Timestamp;
use crate::Version;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NearestTimestamp {
Earliest(Timestamp),
Latest(Timestamp),
Unknown,
}
impl NearestTimestamp {
pub(crate) fn from_boundary(bound: Bound, ts: Timestamp) -> Self {
match bound {
Bound::GreatestLower => Self::Earliest(ts),
Bound::LeastUpper => Self::Latest(ts),
}
}
}
#[derive(Debug, thiserror::Error)]
pub enum LogHistoryError {
#[error("No commits found in log directory {log_root}")]
NoCommitsFound {
log_root: Url,
},
#[error("Invalid timestamp range: ({start_timestamp}, {end_timestamp})")]
InvalidTimestampRange {
start_timestamp: Timestamp,
end_timestamp: Timestamp,
},
#[error(
"There are no commits in the timestamp range ({start_timestamp}, {end_timestamp}). \
The entire timestamp range falls between versions {between_version} and {}.",
.between_version + 1
)]
EmptyTimestampRange {
start_timestamp: Timestamp,
end_timestamp: Timestamp,
between_version: Version,
},
#[error("Timestamp {timestamp} is out of range: {reason}")]
TimestampOutOfRange {
timestamp: Timestamp,
reason: &'static str,
nearest_timestamp: NearestTimestamp,
},
#[error(
"No recreatable commits found at {log_root}: commits exist but version 0 is missing \
and no complete checkpoint is present"
)]
NoRecreatableCommit {
log_root: Url,
},
#[error("{context}{}", source.as_ref().map(|e| format!(": {e}")).unwrap_or_default())]
Internal {
context: &'static str,
#[source]
source: Option<Box<crate::Error>>,
},
}
impl LogHistoryError {
pub(crate) fn internal(context: &'static str, source: crate::Error) -> Self {
Self::Internal {
context,
source: Some(Box::new(source)),
}
}
pub(crate) fn internal_message(context: &'static str) -> Self {
Self::Internal {
context,
source: None,
}
}
pub(crate) fn out_of_range(
timestamp: Timestamp,
bound: Bound,
nearest_timestamp: NearestTimestamp,
) -> Self {
Self::TimestampOutOfRange {
timestamp,
reason: bound.out_of_range_reason(),
nearest_timestamp,
}
}
}
impl From<LogHistoryError> for crate::Error {
fn from(e: LogHistoryError) -> Self {
crate::Error::LogHistory(Box::new(e))
}
}