use std::path::{Path, PathBuf};
use anyhow::{anyhow, Context, Result};
use crate::{NodeConstraint, NodeType};
mod atomic_file;
pub mod mmap;
pub mod shuffle;
pub mod sort;
pub use atomic_file::AtomicFile;
pub fn dir_size(path: &Path) -> Result<usize> {
Ok(std::fs::read_dir(path)
.with_context(|| format!("Could not list {}", path.display()))?
.map(|entry| {
entry
.unwrap_or_else(|e| panic!("Could not read {} entry: {:?}", path.display(), e))
.metadata()
.as_ref()
.unwrap_or_else(|e| panic!("Could not read {} entry: {:?}", path.display(), e))
.len() as usize
})
.sum::<usize>())
}
#[inline(always)]
pub fn suffix_path<P: AsRef<Path>, S: AsRef<std::ffi::OsStr>>(path: P, suffix: S) -> PathBuf {
let mut path = path.as_ref().as_os_str().to_owned();
path.push(suffix);
path.into()
}
pub fn parse_allowed_node_types(s: &str) -> Result<Vec<NodeType>> {
s.parse::<NodeConstraint>()
.map_err(|s| anyhow!("Could not parse --allowed-node-types {s}"))
.map(|constr| constr.to_vec())
}