use std::fmt::Debug;
use iceoryx2_bb_container::semantic_string::SemanticString;
use iceoryx2_bb_container::semantic_string::SemanticStringAccessor;
use iceoryx2_bb_log::fatal_panic;
pub use iceoryx2_bb_system_types::file_name::FileName;
pub use iceoryx2_bb_system_types::file_path::FilePath;
pub use iceoryx2_bb_system_types::path::Path;
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum NamedConceptDoesExistError {
InsufficientPermissions,
UnderlyingResourcesBeingSetUp,
UnderlyingResourcesCorrupted,
InternalError,
}
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum NamedConceptRemoveError {
InsufficientPermissions,
InternalError,
}
#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
pub enum NamedConceptListError {
InsufficientPermissions,
InternalError,
}
pub trait NamedConceptConfiguration: Default + Clone + Debug {
fn prefix(self, value: FileName) -> Self;
fn get_prefix(&self) -> &FileName;
fn suffix(self, value: FileName) -> Self;
fn path_hint(self, value: Path) -> Self;
fn get_suffix(&self) -> &FileName;
fn get_path_hint(&self) -> &Path;
fn path_for(&self, value: &FileName) -> FilePath {
let mut path = *self.get_path_hint();
fatal_panic!(from self, when path.add_path_entry(self.get_prefix().as_string()),
"The path hint \"{}\" in combination with the prefix \"{}\" exceed the maximum supported path length of {} of the operating system.",
path, value, Path::max_len());
fatal_panic!(from self, when path.push_bytes(value.as_string()),
"The path hint \"{}\" in combination with the file name \"{}\" exceed the maximum supported path length of {} of the operating system.",
path, value, Path::max_len());
fatal_panic!(from self, when path.push_bytes(self.get_suffix()),
"The path hint \"{}\" in combination with the file name \"{}\" and the suffix \"{}\" exceed the maximum supported path length of {} of the operating system.",
path, value, self.get_suffix(), Path::max_len());
unsafe { FilePath::new_unchecked(path.as_bytes()) }
}
fn extract_name_from_path(&self, value: &FilePath) -> Option<FileName> {
if *self.get_path_hint() != value.path() {
return None;
}
self.extract_name_from_file(unsafe { &FileName::new_unchecked(value.file_name()) })
}
fn extract_name_from_file(&self, value: &FileName) -> Option<FileName> {
let mut file = *value;
if !fatal_panic!(from self, when file.strip_prefix(self.get_prefix().as_bytes()),
"Stripping the prefix \"{}\" from the file name \"{}\" leads to invalid content.",
self.get_prefix(), file)
{
return None;
}
if !fatal_panic!(from self, when file.strip_suffix(self.get_suffix().as_bytes()),
"Stripping the suffix \"{}\" from the file name \"{}\" leads to invalid content.",
self.get_suffix(), file)
{
return None;
}
Some(file)
}
}
pub trait NamedConceptBuilder<T: NamedConceptMgmt> {
fn new(name: &FileName) -> Self;
fn config(self, config: &T::Configuration) -> Self;
}
pub trait NamedConcept {
fn name(&self) -> &FileName;
}
pub trait NamedConceptMgmt {
type Configuration: NamedConceptConfiguration;
unsafe fn remove(name: &FileName) -> Result<bool, NamedConceptRemoveError> {
Self::remove_cfg(name, &Self::Configuration::default())
}
fn does_exist(name: &FileName) -> Result<bool, NamedConceptDoesExistError> {
Self::does_exist_cfg(name, &Self::Configuration::default())
}
fn list() -> Result<Vec<FileName>, NamedConceptListError> {
Self::list_cfg(&Self::Configuration::default())
}
unsafe fn remove_cfg(
name: &FileName,
cfg: &Self::Configuration,
) -> Result<bool, NamedConceptRemoveError>;
fn does_exist_cfg(
name: &FileName,
cfg: &Self::Configuration,
) -> Result<bool, NamedConceptDoesExistError>;
fn list_cfg(cfg: &Self::Configuration) -> Result<Vec<FileName>, NamedConceptListError>;
}