use std::path::PathBuf;
#[allow(unused_imports)] use super::{Env, WriteFile};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WriteMode {
Truncate,
Append,
Update,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DirEntry {
pub path: PathBuf,
pub is_dir: bool,
}
impl DirEntry {
pub fn file_name(&self) -> String {
self.path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.unwrap_or_default()
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FileMeta {
pub len: u64,
pub is_dir: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Capabilities {
pub hard_link: bool,
pub sync_dir: bool,
pub atomic_rename: bool,
pub file_lock: bool,
pub threads: bool,
pub durable_sync: bool,
}
impl Capabilities {
pub const fn none() -> Self {
Self {
hard_link: false,
sync_dir: false,
atomic_rename: false,
file_lock: false,
threads: false,
durable_sync: false,
}
}
pub const fn posix() -> Self {
Self {
hard_link: true,
sync_dir: true,
atomic_rename: true,
file_lock: true,
threads: true,
durable_sync: true,
}
}
pub const fn with_hard_link(mut self, yes: bool) -> Self {
self.hard_link = yes;
self
}
pub const fn with_sync_dir(mut self, yes: bool) -> Self {
self.sync_dir = yes;
self
}
pub const fn with_atomic_rename(mut self, yes: bool) -> Self {
self.atomic_rename = yes;
self
}
pub const fn with_file_lock(mut self, yes: bool) -> Self {
self.file_lock = yes;
self
}
pub const fn with_threads(mut self, yes: bool) -> Self {
self.threads = yes;
self
}
pub const fn with_durable_sync(mut self, yes: bool) -> Self {
self.durable_sync = yes;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dir_entry_file_name_is_the_last_component() {
let entry = DirEntry {
path: PathBuf::from("/db/sst/000007.sst"),
is_dir: false,
};
assert_eq!(entry.file_name(), "000007.sst");
}
#[test]
fn capabilities_builders_flip_one_flag_each() {
let none = Capabilities::none();
assert!(!none.hard_link && !none.threads);
assert!(Capabilities::posix().hard_link);
assert!(!Capabilities::posix().with_hard_link(false).hard_link);
assert!(Capabilities::none().with_threads(true).threads);
assert!(Capabilities::none().with_sync_dir(true).sync_dir);
assert!(Capabilities::none().with_atomic_rename(true).atomic_rename);
assert!(Capabilities::none().with_file_lock(true).file_lock);
assert!(Capabilities::none().with_durable_sync(true).durable_sync);
}
}