use std::path::{Path, PathBuf};
use crate::ocfl::consts::*;
use crate::ocfl::inventory::Inventory;
use crate::ocfl::{DigestAlgorithm, SpecVersion, VersionNum};
pub fn inventory_path<P>(dir: P) -> PathBuf
where
P: AsRef<Path>,
{
dir.as_ref().join(INVENTORY_FILE)
}
pub fn sidecar_path<P>(dir: P, algorithm: DigestAlgorithm) -> PathBuf
where
P: AsRef<Path>,
{
dir.as_ref().join(sidecar_name(algorithm))
}
pub fn sidecar_name(algorithm: DigestAlgorithm) -> String {
format!("{}.{}", INVENTORY_FILE, algorithm)
}
pub fn object_namaste_path<P>(dir: P, version: SpecVersion) -> PathBuf
where
P: AsRef<Path>,
{
dir.as_ref().join(version.object_namaste().filename)
}
pub fn version_path<P>(object_root: P, version_num: VersionNum) -> PathBuf
where
P: AsRef<Path>,
{
object_root.as_ref().join(version_num.to_string())
}
pub fn content_path<P>(object_root: P, version_num: VersionNum, inventory: &Inventory) -> PathBuf
where
P: AsRef<Path>,
{
let mut version_dir = version_path(object_root, version_num);
version_dir.push(inventory.defaulted_content_dir());
version_dir
}
pub fn head_content_path<P>(object_root: P, inventory: &Inventory) -> PathBuf
where
P: AsRef<Path>,
{
content_path(object_root, inventory.head, inventory)
}
pub fn extensions_path<P>(dir: P) -> PathBuf
where
P: AsRef<Path>,
{
dir.as_ref().join(EXTENSIONS_DIR)
}
pub fn staging_extension_path<P>(storage_root: P) -> PathBuf
where
P: AsRef<Path>,
{
let mut extensions = extensions_path(storage_root);
extensions.push(ROCFL_STAGING_EXTENSION);
extensions
}
pub fn locks_extension_path<P>(storage_root: P) -> PathBuf
where
P: AsRef<Path>,
{
let mut extensions = extensions_path(storage_root);
extensions.push(ROCFL_LOCKS_EXTENSION);
extensions
}
pub fn ocfl_layout_path<P>(storage_root: P) -> PathBuf
where
P: AsRef<Path>,
{
storage_root.as_ref().join(OCFL_LAYOUT_FILE)
}
pub fn root_namaste_path<P>(storage_root: P, version: SpecVersion) -> PathBuf
where
P: AsRef<Path>,
{
storage_root.as_ref().join(version.root_namaste().filename)
}
pub fn ocfl_spec_path<P>(storage_root: P, version: SpecVersion) -> PathBuf
where
P: AsRef<Path>,
{
storage_root.as_ref().join(version.spec_filename())
}
pub fn join(part1: &str, part2: &str) -> String {
let mut joined = match part1.ends_with('/') {
true => part1[..part1.len() - 1].to_string(),
false => part1.to_string(),
};
if !part2.is_empty() {
if (!joined.is_empty() || part1 == "/") && !part2.starts_with('/') {
joined.push('/');
}
joined.push_str(part2);
}
joined
}
pub fn join_with_trailing_slash(part1: &str, part2: &str) -> String {
let mut joined = join(part1, part2);
if !joined.is_empty() && !joined.ends_with('/') {
joined.push('/');
}
joined
}