use crate::{errors::NulError, ffi::string::LibcString};
#[allow(unused)]
#[cfg(test)]
use similar_asserts::assert_eq;
use std::path::Path;
use thiserror::Error;
#[derive(Copy, Clone, Debug, Error, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum PathError {
#[error("hwloc file paths can't contain NUL chars")]
ContainsNul,
#[error("hwloc file paths can't contain non-Unicode data")]
NotUnicode,
}
impl From<NulError> for PathError {
fn from(_: NulError) -> Self {
Self::ContainsNul
}
}
pub(crate) fn make_hwloc_path(path: impl AsRef<Path>) -> Result<LibcString, PathError> {
fn polymorphized(path: &Path) -> Result<LibcString, PathError> {
Ok(LibcString::new(
path.to_str().ok_or(PathError::NotUnicode)?,
)?)
}
polymorphized(path.as_ref())
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::{path::PathParams, prelude::*};
#[allow(unused)]
use similar_asserts::assert_eq;
use static_assertions::{assert_impl_all, assert_not_impl_any};
use std::{
error::Error,
fmt::{self, Binary, LowerExp, LowerHex, Octal, Pointer, UpperExp, UpperHex},
hash::Hash,
io::{self, Read},
ops::Deref,
panic::UnwindSafe,
path::PathBuf,
};
assert_impl_all!(PathError:
Copy, Error, From<NulError>, Hash, Ord, Sized, Sync, Unpin, UnwindSafe
);
assert_not_impl_any!(PathError:
Binary, Default, Deref, Drop, IntoIterator, LowerExp, LowerHex, Octal,
Pointer, Read, UpperExp, UpperHex, fmt::Write, io::Write
);
fn any_path() -> impl Strategy<Value = PathBuf> {
PathBuf::arbitrary_with(PathParams::default().with_component_regex(".*"))
}
proptest! {
#[allow(clippy::option_if_let_else)]
#[test]
fn make_hwloc_path(path in any_path()) {
let res = super::make_hwloc_path(&path);
if let Some(s) = path.to_str() {
if s.contains('\0') {
prop_assert_eq!(res, Err(PathError::ContainsNul));
} else {
prop_assert_eq!(res.as_ref().map(AsRef::as_ref), Ok(s));
}
} else {
prop_assert_eq!(res, Err(PathError::NotUnicode))
}
}
}
}