Skip to main content

pasta_lua/encoding/
unix.rs

1//! Unix/POSIX implementation of encoding module.
2//!
3//! On non-Windows systems, UTF-8 is the standard encoding.
4//! This module provides passthrough implementations.
5
6use super::{Encoder, Encoding};
7use std::io::Result;
8
9impl Encoder for Encoding {
10    /// Convert from bytes to string (UTF-8 passthrough).
11    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    /// Convert from string to bytes (UTF-8 passthrough).
17    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}