#![forbid(unsafe_code)]
#![deny(missing_docs)]
use std::{error::Error, fmt};
pub type PortResult<T> = Result<T, HostDirError>;
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum HostDirErrorKind {
NotFound,
AlreadyExists,
Escape,
SpecialFile,
Malformed,
QuotaExceeded,
Cancelled,
NotEmpty,
Native,
Unsupported,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostDirError {
pub kind: HostDirErrorKind,
pub message: String,
}
impl HostDirError {
pub fn new(kind: HostDirErrorKind, message: impl Into<String>) -> Self {
Self {
kind,
message: message.into(),
}
}
}
impl fmt::Display for HostDirError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl Error for HostDirError {}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum HostEntryKind {
File,
Directory,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostEntry {
pub name: String,
pub kind: HostEntryKind,
pub len: u64,
}
pub trait Cancellation: Send + Sync {
fn is_cancelled(&self) -> bool;
}
#[derive(Debug, Default)]
pub struct NeverCancel;
impl Cancellation for NeverCancel {
fn is_cancelled(&self) -> bool {
false
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct HostCompareExchange {
pub exchanged: bool,
pub observed: Option<Vec<u8>>,
}
pub trait HostDirPort: Send + Sync {
fn label(&self) -> &str;
fn list(&self, dir: &[String]) -> PortResult<Vec<HostEntry>>;
fn metadata(&self, path: &[String]) -> PortResult<Option<HostEntry>>;
fn read(&self, path: &[String]) -> PortResult<Vec<u8>>;
fn replace(&self, path: &[String], bytes: &[u8], cancel: &dyn Cancellation) -> PortResult<()>;
fn compare_exchange(
&self,
_path: &[String],
_expected: Option<&[u8]>,
_replacement: Option<&[u8]>,
_cancel: &dyn Cancellation,
) -> PortResult<HostCompareExchange> {
Err(HostDirError::new(
HostDirErrorKind::Unsupported,
"host directory compare-exchange unsupported",
))
}
fn remove_file(&self, path: &[String]) -> PortResult<()>;
fn create_dir(&self, path: &[String]) -> PortResult<()>;
fn remove_dir_all(&self, path: &[String]) -> PortResult<()>;
fn child(&self, name: &str) -> PortResult<std::sync::Arc<dyn HostDirPort>>;
}