actix_http/header/shared/
charset.rs1use std::{fmt, str};
2
3use self::Charset::*;
4
5#[derive(Debug, Clone, PartialEq, Eq)]
11#[allow(non_camel_case_types)]
12pub enum Charset {
13 Us_Ascii,
15 Iso_8859_1,
17 Iso_8859_2,
19 Iso_8859_3,
21 Iso_8859_4,
23 Iso_8859_5,
25 Iso_8859_6,
27 Iso_8859_7,
29 Iso_8859_8,
31 Iso_8859_9,
33 Iso_8859_10,
35 Shift_Jis,
37 Euc_Jp,
39 Iso_2022_Kr,
41 Euc_Kr,
43 Iso_2022_Jp,
45 Iso_2022_Jp_2,
47 Iso_8859_6_E,
49 Iso_8859_6_I,
51 Iso_8859_8_E,
53 Iso_8859_8_I,
55 Gb2312,
57 Big5,
59 Koi8_R,
61 Ext(String),
63}
64
65impl Charset {
66 fn label(&self) -> &str {
67 match *self {
68 Us_Ascii => "US-ASCII",
69 Iso_8859_1 => "ISO-8859-1",
70 Iso_8859_2 => "ISO-8859-2",
71 Iso_8859_3 => "ISO-8859-3",
72 Iso_8859_4 => "ISO-8859-4",
73 Iso_8859_5 => "ISO-8859-5",
74 Iso_8859_6 => "ISO-8859-6",
75 Iso_8859_7 => "ISO-8859-7",
76 Iso_8859_8 => "ISO-8859-8",
77 Iso_8859_9 => "ISO-8859-9",
78 Iso_8859_10 => "ISO-8859-10",
79 Shift_Jis => "Shift-JIS",
80 Euc_Jp => "EUC-JP",
81 Iso_2022_Kr => "ISO-2022-KR",
82 Euc_Kr => "EUC-KR",
83 Iso_2022_Jp => "ISO-2022-JP",
84 Iso_2022_Jp_2 => "ISO-2022-JP-2",
85 Iso_8859_6_E => "ISO-8859-6-E",
86 Iso_8859_6_I => "ISO-8859-6-I",
87 Iso_8859_8_E => "ISO-8859-8-E",
88 Iso_8859_8_I => "ISO-8859-8-I",
89 Gb2312 => "GB2312",
90 Big5 => "Big5",
91 Koi8_R => "KOI8-R",
92 Ext(ref s) => s,
93 }
94 }
95}
96
97impl fmt::Display for Charset {
98 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99 f.write_str(self.label())
100 }
101}
102
103impl str::FromStr for Charset {
104 type Err = crate::Error;
105
106 fn from_str(s: &str) -> Result<Charset, crate::Error> {
107 Ok(match s.to_ascii_uppercase().as_ref() {
108 "US-ASCII" => Us_Ascii,
109 "ISO-8859-1" => Iso_8859_1,
110 "ISO-8859-2" => Iso_8859_2,
111 "ISO-8859-3" => Iso_8859_3,
112 "ISO-8859-4" => Iso_8859_4,
113 "ISO-8859-5" => Iso_8859_5,
114 "ISO-8859-6" => Iso_8859_6,
115 "ISO-8859-7" => Iso_8859_7,
116 "ISO-8859-8" => Iso_8859_8,
117 "ISO-8859-9" => Iso_8859_9,
118 "ISO-8859-10" => Iso_8859_10,
119 "SHIFT-JIS" => Shift_Jis,
120 "EUC-JP" => Euc_Jp,
121 "ISO-2022-KR" => Iso_2022_Kr,
122 "EUC-KR" => Euc_Kr,
123 "ISO-2022-JP" => Iso_2022_Jp,
124 "ISO-2022-JP-2" => Iso_2022_Jp_2,
125 "ISO-8859-6-E" => Iso_8859_6_E,
126 "ISO-8859-6-I" => Iso_8859_6_I,
127 "ISO-8859-8-E" => Iso_8859_8_E,
128 "ISO-8859-8-I" => Iso_8859_8_I,
129 "GB2312" => Gb2312,
130 "BIG5" => Big5,
131 "KOI8-R" => Koi8_R,
132 s => Ext(s.to_owned()),
133 })
134 }
135}
136
137#[cfg(test)]
138mod tests {
139 use super::*;
140
141 #[test]
142 fn test_parse() {
143 assert_eq!(Us_Ascii, "us-ascii".parse().unwrap());
144 assert_eq!(Us_Ascii, "US-Ascii".parse().unwrap());
145 assert_eq!(Us_Ascii, "US-ASCII".parse().unwrap());
146 assert_eq!(Shift_Jis, "Shift-JIS".parse().unwrap());
147 assert_eq!(Ext("ABCD".to_owned()), "abcd".parse().unwrap());
148 }
149
150 #[test]
151 fn test_display() {
152 assert_eq!("US-ASCII", format!("{}", Us_Ascii));
153 assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
154 }
155}