#[cfg(windows)]
mod windows;
#[cfg(windows)]
#[allow(unused_imports)]
pub(crate) use self::windows::*;
#[cfg(not(windows))]
mod unix;
#[cfg(not(windows))]
#[allow(unused_imports)]
pub(crate) use self::unix::*;
use std::io::Result;
pub trait Encoder {
fn to_string(&self, data: &[u8]) -> Result<String>;
fn to_bytes(&self, data: &str) -> Result<Vec<u8>>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Encoding {
ANSI,
OEM,
}
#[cfg(windows)]
pub fn to_ansi_bytes(s: &str) -> Result<Vec<u8>> {
Encoding::ANSI.to_bytes(s)
}
#[cfg(not(windows))]
pub fn to_ansi_bytes(s: &str) -> Result<Vec<u8>> {
Ok(s.as_bytes().to_vec())
}
pub fn path_from_lua(path: &str) -> Result<String> {
#[cfg(windows)]
{
let bytes = path.as_bytes();
Encoding::ANSI.to_string(bytes)
}
#[cfg(not(windows))]
{
Ok(path.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_to_ansi_bytes_ascii() {
let result = to_ansi_bytes("test/path/file.lua").unwrap();
assert_eq!(result, b"test/path/file.lua");
}
#[test]
fn test_to_ansi_bytes_empty() {
let result = to_ansi_bytes("").unwrap();
assert!(result.is_empty());
}
#[test]
fn test_encoding_enum() {
assert_ne!(Encoding::ANSI, Encoding::OEM);
}
#[cfg(windows)]
#[test]
fn test_to_ansi_bytes_japanese() {
let result = to_ansi_bytes("日本語パス").unwrap();
assert!(!result.is_empty());
let utf8_bytes = "日本語パス".as_bytes();
assert_ne!(result, utf8_bytes);
}
#[cfg(windows)]
#[test]
fn test_to_ansi_bytes_roundtrip() {
let original = "日本語テスト";
let ansi_bytes = to_ansi_bytes(original).unwrap();
let restored = Encoding::ANSI.to_string(&ansi_bytes).unwrap();
assert_eq!(restored, original);
}
}