use super::path::S3Location;
use super::{InodeError, InodeErrorInfo, InodeKind, InodeNo, InodeStat};
use std::time::Duration;
#[derive(Debug, Clone)]
pub struct Lookup {
information: InodeInformation,
location: Option<S3Location>,
}
impl Lookup {
pub fn new(ino: InodeNo, stat: InodeStat, kind: InodeKind, location: Option<S3Location>) -> Self {
Self::new_from_info_and_loc(InodeInformation::new(ino, stat, kind), location)
}
pub fn new_from_info_and_loc(information: InodeInformation, location: Option<S3Location>) -> Self {
debug_assert!(
location
.as_ref()
.is_none_or(|location| location.partial_key.kind() == information.kind()),
"wrong kind for ino {}",
information.ino(),
);
Self { information, location }
}
pub fn kind(&self) -> InodeKind {
self.information.kind()
}
pub fn stat(&self) -> &InodeStat {
self.information.stat()
}
pub fn ino(&self) -> InodeNo {
self.information.ino()
}
pub fn validity(&self) -> Duration {
self.information.validity()
}
pub fn s3_location(&self) -> Result<&S3Location, InodeError> {
self.location
.as_ref()
.ok_or(InodeError::OperationNotSupportedOnSyntheticInode { ino: self.ino() })
}
pub fn try_into_s3_location(mut self) -> Result<S3Location, InodeError> {
self.location
.take()
.ok_or(InodeError::OperationNotSupportedOnSyntheticInode { ino: self.ino() })
}
pub fn inode_err(&self) -> InodeErrorInfo {
let (key, bucket) = match &self.location {
Some(location) => (
location.full_key().to_string(),
Some(location.bucket_name().to_string()),
),
None => ("SYNTHETIC".to_string(), Some("SYNTHETIC".to_string())),
};
InodeErrorInfo {
ino: self.ino(),
key: key.into(),
bucket: bucket.map(|b| b.into()),
}
}
}
#[derive(Debug, Clone)]
pub struct InodeInformation {
ino: InodeNo,
stat: InodeStat,
kind: InodeKind,
}
impl InodeInformation {
pub fn new(ino: InodeNo, stat: InodeStat, kind: InodeKind) -> Self {
InodeInformation { ino, stat, kind }
}
pub fn kind(&self) -> InodeKind {
self.kind
}
pub fn stat(&self) -> &InodeStat {
&self.stat
}
pub fn ino(&self) -> InodeNo {
self.ino
}
pub fn validity(&self) -> Duration {
self.stat.expiry.remaining_ttl()
}
}
impl From<Lookup> for InodeInformation {
fn from(looked_up: Lookup) -> Self {
looked_up.information
}
}