scolapasta_path/platform_string/
unix_wasi.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#![allow(clippy::unnecessary_wraps)]

use std::ffi::{OsStr, OsString};
#[cfg(unix)]
use std::os::unix::ffi::{OsStrExt, OsStringExt};
#[cfg(target_os = "wasi")]
use std::os::wasi::ffi::{OsStrExt, OsStringExt};
#[cfg(not(any(unix, target_os = "wasi")))]
error!("Internal: This module can only be compiled on unix and wasi");

use super::ConvertBytesError;

pub fn bytes_to_os_str(bytes: &[u8]) -> Result<&OsStr, ConvertBytesError> {
    Ok(OsStr::from_bytes(bytes))
}

pub fn bytes_to_os_string(bytes: Vec<u8>) -> Result<OsString, ConvertBytesError> {
    Ok(OsString::from_vec(bytes))
}

pub fn os_str_to_bytes(os_str: &OsStr) -> Result<&[u8], ConvertBytesError> {
    Ok(os_str.as_bytes())
}

pub fn os_string_to_bytes(os_string: OsString) -> Result<Vec<u8>, ConvertBytesError> {
    Ok(os_string.into_vec())
}

#[cfg(test)]
mod tests {
    use std::ffi::OsString;
    #[cfg(unix)]
    use std::os::unix::ffi::OsStringExt;
    #[cfg(target_os = "wasi")]
    use std::os::wasi::ffi::OsStringExt;
    use std::str;

    use super::{bytes_to_os_str, bytes_to_os_string, os_str_to_bytes, os_string_to_bytes};

    #[test]
    fn utf8_bytes_convert_to_os_str() {
        let test_cases: &[&[u8]] = &[b"", b"abc", b"abc\0", b"\0abc", b"abc/xyz"];
        for &bytes in test_cases {
            bytes_to_os_str(bytes).unwrap();
            bytes_to_os_string(bytes.to_vec()).unwrap();
        }
    }

    #[test]
    fn invalid_utf8_bytes_convert_to_os_str() {
        let test_cases: &[&[u8]] = &[b"\xFF", b"\xFE", b"abc/\xFF/\xFE", b"\0\xFF", b"\xFF\0"];
        for &bytes in test_cases {
            bytes_to_os_str(bytes).unwrap();
            bytes_to_os_string(bytes.to_vec()).unwrap();
        }
    }

    #[test]
    fn bytes_os_str_round_trip() {
        let test_cases: &[&[u8]] = &[b"", b"abc", b"abc\0", b"\0abc", b"abc/xyz"];
        for &bytes in test_cases {
            let os_str = bytes_to_os_str(bytes).unwrap();
            assert_eq!(os_str_to_bytes(os_str).unwrap(), bytes);

            let os_string = bytes_to_os_string(bytes.to_vec()).unwrap();
            assert_eq!(os_string_to_bytes(os_string).unwrap(), bytes);
        }
    }

    #[test]
    fn invalid_utf8_bytes_os_str_round_trip() {
        let test_cases: &[&[u8]] = &[b"\xFF", b"\xFE", b"abc/\xFF/\xFE", b"\0\xFF", b"\xFF\0"];
        for &bytes in test_cases {
            let os_str = bytes_to_os_str(bytes).unwrap();
            assert_eq!(os_str_to_bytes(os_str).unwrap(), bytes);

            let os_string = bytes_to_os_string(bytes.to_vec()).unwrap();
            assert_eq!(os_string_to_bytes(os_string).unwrap(), bytes);
        }
    }

    #[test]
    fn owned_utf8_bytes_convert_to_os_string() {
        let test_cases: &[&[u8]] = &[b"", b"abc", b"abc\0", b"\0abc", b"abc/xyz"];
        for &bytes in test_cases {
            let s = str::from_utf8(bytes).unwrap();
            let os_string = OsString::from(s.to_owned());
            os_string_to_bytes(os_string).unwrap();
        }
    }

    #[test]
    fn owned_invalid_utf8_bytes_convert_to_os_string() {
        let test_cases: &[&[u8]] = &[b"\xFF", b"\xFE", b"abc/\xFF/\xFE", b"\0\xFF", b"\xFF\0"];
        for &bytes in test_cases {
            let os_string = OsString::from_vec(bytes.to_owned());
            os_string_to_bytes(os_string).unwrap();
        }
    }

    #[test]
    fn bytes_os_string_round_trip() {
        let test_cases: &[&[u8]] = &[b"", b"abc", b"abc\0", b"\0abc", b"abc/xyz"];
        for &bytes in test_cases {
            let os_string = bytes_to_os_string(bytes.to_owned()).unwrap();
            assert_eq!(os_string_to_bytes(os_string).unwrap(), bytes);
        }
    }

    #[test]
    fn invalid_utf8_bytes_os_string_round_trip() {
        let test_cases: &[&[u8]] = &[b"\xFF", b"\xFE", b"abc/\xFF/\xFE", b"\0\xFF", b"\xFF\0"];
        for &bytes in test_cases {
            let os_string = bytes_to_os_string(bytes.to_owned()).unwrap();
            assert_eq!(os_string_to_bytes(os_string).unwrap(), bytes);
        }
    }
}