use super::builder::FormatDriverBuilder;
use crate::{FormatAccess, Storage, StorageOpenOptions};
use std::io;
pub trait ImplicitOpenGate<S: Storage + 'static> {
#[allow(async_fn_in_trait)] async fn open_format<F: FormatDriverBuilder<S>>(
&mut self,
builder: F,
) -> io::Result<FormatAccess<S>>;
#[allow(async_fn_in_trait)] async fn open_storage(&mut self, builder: StorageOpenOptions) -> io::Result<S>;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct PermissiveImplicitOpenGate();
impl<S: Storage + 'static> ImplicitOpenGate<S> for PermissiveImplicitOpenGate {
async fn open_format<F: FormatDriverBuilder<S>>(
&mut self,
builder: F,
) -> io::Result<FormatAccess<S>> {
Ok(FormatAccess::new(
Box::pin(builder.open(Self::default())).await?,
))
}
async fn open_storage(&mut self, builder: StorageOpenOptions) -> io::Result<S> {
S::open(builder).await
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct DenyImplicitOpenGate();
impl<S: Storage + 'static> ImplicitOpenGate<S> for DenyImplicitOpenGate {
async fn open_format<F: FormatDriverBuilder<S>>(
&mut self,
builder: F,
) -> io::Result<FormatAccess<S>> {
let msg = if let Some(filename) = builder.get_image_path() {
format!("Opening implicitly referenced format layer {filename:?} denied")
} else {
"Opening implicitly referenced format layer denied".into()
};
Err(io::Error::new(io::ErrorKind::PermissionDenied, msg))
}
async fn open_storage(&mut self, builder: StorageOpenOptions) -> io::Result<S> {
let msg = if let Some(filename) = builder.get_filename() {
format!("Opening implicitly referenced storage object {filename:?} denied")
} else {
"Opening implicitly referenced storage object denied".into()
};
Err(io::Error::new(io::ErrorKind::PermissionDenied, msg))
}
}