Ghee/src/paths.rs
2023-09-05 21:27:30 -07:00

36 lines
869 B
Rust

use anyhow::Result;
use std::{ffi::OsString, path::PathBuf};
use thiserror::Error;
use crate::parser::{key::Key, xattr::Namespace};
#[derive(Error, Debug)]
pub enum SubIdxPathErr {
#[error("The provided key was empty")]
EmptyKey,
}
/// The path of a sub-index: an index nested inside another (primary) index
pub fn sub_idx_path(view_of: &PathBuf, key: &Key) -> Result<PathBuf> {
if key.is_empty() {
return Err(SubIdxPathErr::EmptyKey.into());
}
let mut sub_idx_dirname = OsString::new();
for k in key.iter() {
sub_idx_dirname.push(":");
if k.namespace == Namespace::User {
sub_idx_dirname.push(k.attr.clone());
} else {
sub_idx_dirname.push(k.to_string());
}
}
let mut sub_idx_path = view_of.clone();
sub_idx_path.push(sub_idx_dirname);
Ok(sub_idx_path)
}