use std::ffi::{OsStr, OsString};
use std::io;
use std::path::Path;
#[cfg(unix)]
pub(crate) fn os_str_bytes(value: &OsStr) -> &[u8] {
use std::os::unix::ffi::OsStrExt;
value.as_bytes()
}
#[cfg(windows)]
pub(crate) fn os_str_bytes(value: &OsStr) -> &[u8] {
value.as_encoded_bytes()
}
pub(crate) fn path_bytes(path: &Path) -> &[u8] {
os_str_bytes(path.as_os_str())
}
#[cfg(unix)]
pub(crate) fn os_string_from_bytes(bytes: &[u8]) -> io::Result<OsString> {
use std::os::unix::ffi::OsStringExt;
Ok(OsString::from_vec(bytes.to_vec()))
}
#[cfg(windows)]
pub(crate) fn os_string_from_bytes(bytes: &[u8]) -> io::Result<OsString> {
let value = std::str::from_utf8(bytes).map_err(|_| invalid_path_encoding(bytes))?;
Ok(OsString::from(value))
}
pub(crate) fn os_string_from_vec(bytes: Vec<u8>) -> io::Result<OsString> {
os_string_from_bytes(&bytes)
}
#[cfg(windows)]
fn invalid_path_encoding(bytes: &[u8]) -> io::Error {
io::Error::new(
io::ErrorKind::InvalidData,
format!(
"image path is not valid UTF-8 on Windows: {:?}",
String::from_utf8_lossy(bytes)
),
)
}