#![deny(missing_docs)]
#![cfg_attr(test, deny(warnings))]
#![doc(test(attr(deny(warnings))))]
use thiserror::Error;
#[derive(Error, Debug)]
pub enum RandomAccessError {
#[error("{} out of bounds. {} < {}{}",
.end.as_ref().map_or_else(|| "Offset", |_| "Range"),
.length,
.offset,
.end.as_ref().map_or_else(String::new, |end| format!("..{}", end)))]
OutOfBounds {
offset: u64,
end: Option<u64>,
length: u64,
},
#[error("Unrecoverable input/output error occured.{}{}",
.return_code.as_ref().map_or_else(String::new, |rc| format!(" Return code: {}.", rc)),
.context.as_ref().map_or_else(String::new, |ctx| format!(" Context: {}.", ctx)))]
IO {
return_code: Option<i32>,
context: Option<String>,
#[source]
source: std::io::Error,
},
}
impl From<std::io::Error> for RandomAccessError {
fn from(err: std::io::Error) -> Self {
Self::IO {
return_code: None,
context: None,
source: err,
}
}
}
#[async_trait::async_trait]
pub trait RandomAccess {
async fn write(
&mut self,
offset: u64,
data: &[u8],
) -> Result<(), RandomAccessError>;
async fn read(
&mut self,
offset: u64,
length: u64,
) -> Result<Vec<u8>, RandomAccessError>;
async fn del(
&mut self,
offset: u64,
length: u64,
) -> Result<(), RandomAccessError>;
async fn truncate(&mut self, length: u64) -> Result<(), RandomAccessError>;
async fn len(&mut self) -> Result<u64, RandomAccessError>;
async fn is_empty(&mut self) -> Result<bool, RandomAccessError>;
async fn sync_all(&mut self) -> Result<(), RandomAccessError>;
}