oneyaml_core/encoding.rs
1//! Character encodings of YAML input (YAML 1.2.2 §5.2), as vocabulary
2//! shared by the decoder (`oneyaml-syntax`) and `Document::encoding`
3//! (`oneyaml`), so the stable API names no syntax type.
4//! Only the names live here; detection and decoding live in
5//! `oneyaml-syntax`.
6
7/// A character encoding of YAML input (§5.2).
8#[non_exhaustive]
9#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
10pub enum Encoding {
11 /// UTF-8 (the default when nothing else is detected).
12 Utf8,
13 /// UTF-16, little-endian.
14 Utf16Le,
15 /// UTF-16, big-endian.
16 Utf16Be,
17 /// UTF-32, little-endian.
18 Utf32Le,
19 /// UTF-32, big-endian.
20 Utf32Be,
21}
22
23impl Encoding {
24 /// The encoding's usual name (`UTF-8`, `UTF-16LE`, …).
25 #[must_use]
26 pub const fn name(self) -> &'static str {
27 match self {
28 Self::Utf8 => "UTF-8",
29 Self::Utf16Le => "UTF-16LE",
30 Self::Utf16Be => "UTF-16BE",
31 Self::Utf32Le => "UTF-32LE",
32 Self::Utf32Be => "UTF-32BE",
33 }
34 }
35}
36
37#[cfg(test)]
38mod tests {
39 use super::Encoding;
40
41 #[test]
42 fn names() {
43 assert_eq!(Encoding::Utf8.name(), "UTF-8");
44 assert_eq!(Encoding::Utf16Le.name(), "UTF-16LE");
45 assert_eq!(Encoding::Utf16Be.name(), "UTF-16BE");
46 assert_eq!(Encoding::Utf32Le.name(), "UTF-32LE");
47 assert_eq!(Encoding::Utf32Be.name(), "UTF-32BE");
48 }
49}