use crate::GlobalTableId;
use crate::descriptor_table::DescriptorTable;
use crate::fs::FsFile;
use std::sync::Arc;
#[derive(Clone)]
pub enum FileAccessor {
File(Arc<dyn FsFile>),
DescriptorTable(Arc<DescriptorTable>),
}
impl FileAccessor {
#[must_use]
pub fn as_descriptor_table(&self) -> Option<&DescriptorTable> {
match self {
Self::DescriptorTable(d) => Some(d),
Self::File(_) => None,
}
}
#[must_use]
pub fn access_for_table(&self, table_id: &GlobalTableId) -> Option<Arc<dyn FsFile>> {
match self {
Self::File(fd) => Some(fd.clone()),
Self::DescriptorTable(descriptor_table) => descriptor_table.access_for_table(table_id),
}
}
pub fn insert_for_table(&self, table_id: GlobalTableId, fd: Arc<dyn FsFile>) {
if let Self::DescriptorTable(descriptor_table) = self {
descriptor_table.insert_for_table(table_id, fd);
}
}
#[must_use]
pub fn access_for_blob_file(&self, table_id: &GlobalTableId) -> Option<Arc<dyn FsFile>> {
match self {
Self::File(fd) => Some(fd.clone()),
Self::DescriptorTable(descriptor_table) => {
descriptor_table.access_for_blob_file(table_id)
}
}
}
pub fn insert_for_blob_file(&self, table_id: GlobalTableId, fd: Arc<dyn FsFile>) {
if let Self::DescriptorTable(descriptor_table) = self {
descriptor_table.insert_for_blob_file(table_id, fd);
}
}
}
impl std::fmt::Debug for FileAccessor {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::File(_) => write!(f, "FileAccessor::Pinned"),
Self::DescriptorTable(_) => {
write!(f, "FileAccessor::Cached")
}
}
}
}