rsvim_core/buf/opt/
file_encoding.rs

1//! The "file-encoding" option for Vim buffer.
2
3use std::fmt::Display;
4use std::string::ToString;
5
6#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
7pub enum FileEncodingOption {
8  Utf8,
9  // Utf16,
10  // Utf32,
11}
12
13impl Display for FileEncodingOption {
14  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
15    match self {
16      FileEncodingOption::Utf8 => write!(f, "utf-8"),
17      // FileEncoding::Utf16 => "utf-16".to_string(),
18      // FileEncoding::Utf32 => "utf-32".to_string(),
19    }
20  }
21}
22
23impl TryFrom<&str> for FileEncodingOption {
24  type Error = String;
25
26  fn try_from(value: &str) -> Result<Self, Self::Error> {
27    let lower_value = value.to_lowercase();
28    match lower_value.as_str() {
29      "utf-8" | "utf8" => Ok(FileEncodingOption::Utf8),
30      // "utf-16" | "utf16" => Ok(FileEncoding::Utf16),
31      // "utf-32" | "utf32" => Ok(FileEncoding::Utf32),
32      _ => Err("Unknown FileEncoding value".to_string()),
33    }
34  }
35}
36
37#[cfg(test)]
38mod tests {
39  use super::*;
40
41  #[test]
42  fn display1() {
43    let actual1 = format!("{}", FileEncodingOption::Utf8);
44    assert_eq!(actual1, "utf-8");
45  }
46}