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(
22 target_arch = "wasm32",
23 target_os = "wasi",
24 not(target_env = "p2")
25 ),
26 ))]
27 {
28 #[cfg(not(target_os = "wasi"))]
29 use std::os::unix::ffi::OsStringExt;
30 #[cfg(target_os = "wasi")]
31 use std::os::wasi::ffi::OsStringExt;
32 use std::string::ToString;
33
34 String::from_utf8(string.into_vec())
35 .map_err(|e| Error::with_invalid_data(e.to_string()))
36 }
37
38 #[cfg(any(
39 target_os = "windows",
40 all(target_arch = "wasm32", not(target_os = "wasi")),
41 all(target_arch = "wasm32", target_os = "wasi", target_env = "p2"),
42 ))]
43 {
44 string
45 .into_string()
46 .map_err(|_| Error::with_invalid_data("Not valid unicode"))
47 }
48}