pasta_lua/encoding/
unix.rs1use super::{Encoder, Encoding};
7use std::io::Result;
8
9impl Encoder for Encoding {
10 fn to_string(&self, data: &[u8]) -> Result<String> {
12 String::from_utf8(data.to_vec())
13 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))
14 }
15
16 fn to_bytes(&self, data: &str) -> Result<Vec<u8>> {
18 Ok(data.as_bytes().to_vec())
19 }
20}
21
22#[cfg(test)]
23mod tests {
24 use super::*;
25
26 #[test]
27 fn test_ansi_to_string() {
28 let result = Encoding::ANSI.to_string(b"Hello").unwrap();
29 assert_eq!(result, "Hello");
30 }
31
32 #[test]
33 fn test_ansi_to_bytes() {
34 let result = Encoding::ANSI.to_bytes("Hello").unwrap();
35 assert_eq!(result, b"Hello");
36 }
37
38 #[test]
39 fn test_oem_to_string() {
40 let result = Encoding::OEM.to_string(b"Hello").unwrap();
41 assert_eq!(result, "Hello");
42 }
43
44 #[test]
45 fn test_oem_to_bytes() {
46 let result = Encoding::OEM.to_bytes("Hello").unwrap();
47 assert_eq!(result, b"Hello");
48 }
49
50 #[test]
51 fn test_utf8_roundtrip() {
52 let original = "日本語テスト";
53 let bytes = Encoding::ANSI.to_bytes(original).unwrap();
54 let restored = Encoding::ANSI.to_string(&bytes).unwrap();
55 assert_eq!(original, restored);
56 }
57}