whoami/
conversions.rs

1use alloc::string::String;
2
3use crate::Result;
4
5#[cfg(feature = "std")]
6pub(super) type OsString = std::ffi::OsString;
7#[cfg(not(feature = "std"))]
8pub(super) type OsString = String;
9
10#[cfg(not(feature = "std"))]
11pub(crate) fn string_from_os(string: String) -> Result<String> {
12    Ok(string)
13}
14
15#[cfg(feature = "std")]
16pub(crate) fn string_from_os(string: OsString) -> Result<String> {
17    use crate::Error;
18
19    #[cfg(any(
20        all(not(target_os = "windows"), not(target_arch = "wasm32")),
21        all(target_arch = "wasm32", target_os = "wasi"),
22    ))]
23    {
24        #[cfg(not(target_os = "wasi"))]
25        use std::os::unix::ffi::OsStringExt;
26        #[cfg(target_os = "wasi")]
27        use std::os::wasi::ffi::OsStringExt;
28        use std::string::ToString;
29
30        String::from_utf8(string.into_vec())
31            .map_err(|e| Error::with_invalid_data(e.to_string()))
32    }
33
34    #[cfg(any(
35        target_os = "windows",
36        all(target_arch = "wasm32", not(target_os = "wasi")),
37    ))]
38    {
39        string
40            .into_string()
41            .map_err(|_| Error::with_invalid_data("Not valid unicode"))
42    }
43}