loonfs_core/path/
helpers.rs1use crate::error::{CoreError, Result};
4use loonfs_api::{AbsolutePath, DisplayName, PathError};
5
6pub(crate) fn parse_absolute_path_for_core(absolute_path: &str) -> Result<AbsolutePath> {
7 AbsolutePath::parse(absolute_path).map_err(map_path_error_to_core)
8}
9
10pub fn parse_mutation_path(absolute_path: &str) -> Result<AbsolutePath> {
18 let path = parse_absolute_path_for_core(absolute_path)?;
19 ensure_mutation_path(&path)?;
20 Ok(path)
21}
22
23pub(crate) fn ensure_mutation_path(path: &AbsolutePath) -> Result<()> {
32 if path.is_root() {
33 return Err(CoreError::RootMutationForbidden);
34 }
35 Ok(())
36}
37
38pub(crate) fn map_path_error_to_core(error: PathError) -> CoreError {
39 CoreError::InvalidPath(error.invalid_path_input().to_owned())
40}
41
42pub(crate) fn final_component(absolute_path: &AbsolutePath) -> Result<DisplayName> {
43 absolute_path
44 .final_component()
45 .map(|component| component.to_display_name())
46 .ok_or_else(|| CoreError::InvalidPath(absolute_path.as_str().to_owned()))
47}