mirror of
https://github.com/encounter/decomp-toolkit.git
synced 2025-08-04 03:05:39 +00:00
This allows handling path conversions in a more structured way, as well as avoiding needless UTF-8 checks. All argument inputs use `Utf8NativePathBuf`, while all config entries use `Utf8UnixPathBuf`, ensuring that we deserialize/serialize using forward slashes. We can omit `.display()` and lossy UTF-8 conversions since all paths are known valid UTF-8.
27 lines
856 B
Rust
27 lines
856 B
Rust
use std::{
|
|
path::{Path, PathBuf},
|
|
str::Utf8Error,
|
|
string::FromUtf8Error,
|
|
};
|
|
|
|
use typed_path::{NativePath, NativePathBuf, Utf8NativePath, Utf8NativePathBuf};
|
|
|
|
// For argp::FromArgs
|
|
pub fn native_path(value: &str) -> Result<Utf8NativePathBuf, String> {
|
|
Ok(Utf8NativePathBuf::from(value))
|
|
}
|
|
|
|
/// Checks if the path is valid UTF-8 and returns it as a [`Utf8NativePath`].
|
|
#[inline]
|
|
pub fn check_path(path: &Path) -> Result<&Utf8NativePath, Utf8Error> {
|
|
Utf8NativePath::from_bytes_path(NativePath::new(path.as_os_str().as_encoded_bytes()))
|
|
}
|
|
|
|
/// Checks if the path is valid UTF-8 and returns it as a [`Utf8NativePathBuf`].
|
|
#[inline]
|
|
pub fn check_path_buf(path: PathBuf) -> Result<Utf8NativePathBuf, FromUtf8Error> {
|
|
Utf8NativePathBuf::from_bytes_path_buf(NativePathBuf::from(
|
|
path.into_os_string().into_encoded_bytes(),
|
|
))
|
|
}
|