hyper_sync/header/shared/
charset.rs1use std::fmt::{self, Display};
2use std::str::FromStr;
3#[allow(unused, deprecated)]
4use std::ascii::AsciiExt;
5
6use self::Charset::*;
7
8#[derive(Clone,Debug,PartialEq)]
16#[allow(non_camel_case_types)]
17pub enum Charset{
18 Us_Ascii,
20 Iso_8859_1,
22 Iso_8859_2,
24 Iso_8859_3,
26 Iso_8859_4,
28 Iso_8859_5,
30 Iso_8859_6,
32 Iso_8859_7,
34 Iso_8859_8,
36 Iso_8859_9,
38 Iso_8859_10,
40 Shift_Jis,
42 Euc_Jp,
44 Iso_2022_Kr,
46 Euc_Kr,
48 Iso_2022_Jp,
50 Iso_2022_Jp_2,
52 Iso_8859_6_E,
54 Iso_8859_6_I,
56 Iso_8859_8_E,
58 Iso_8859_8_I,
60 Gb2312,
62 Big5,
64 Koi8_R,
66 Ext(String)
68}
69
70impl Charset {
71 fn name(&self) -> &str {
72 match *self {
73 Us_Ascii => "US-ASCII",
74 Iso_8859_1 => "ISO-8859-1",
75 Iso_8859_2 => "ISO-8859-2",
76 Iso_8859_3 => "ISO-8859-3",
77 Iso_8859_4 => "ISO-8859-4",
78 Iso_8859_5 => "ISO-8859-5",
79 Iso_8859_6 => "ISO-8859-6",
80 Iso_8859_7 => "ISO-8859-7",
81 Iso_8859_8 => "ISO-8859-8",
82 Iso_8859_9 => "ISO-8859-9",
83 Iso_8859_10 => "ISO-8859-10",
84 Shift_Jis => "Shift-JIS",
85 Euc_Jp => "EUC-JP",
86 Iso_2022_Kr => "ISO-2022-KR",
87 Euc_Kr => "EUC-KR",
88 Iso_2022_Jp => "ISO-2022-JP",
89 Iso_2022_Jp_2 => "ISO-2022-JP-2",
90 Iso_8859_6_E => "ISO-8859-6-E",
91 Iso_8859_6_I => "ISO-8859-6-I",
92 Iso_8859_8_E => "ISO-8859-8-E",
93 Iso_8859_8_I => "ISO-8859-8-I",
94 Gb2312 => "GB2312",
95 Big5 => "5",
96 Koi8_R => "KOI8-R",
97 Ext(ref s) => s
98 }
99 }
100}
101
102impl Display for Charset {
103 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
104 f.write_str(self.name())
105 }
106}
107
108impl FromStr for Charset {
109 type Err = ::Error;
110 fn from_str(s: &str) -> ::Result<Charset> {
111 Ok(match s.to_ascii_uppercase().as_ref() {
112 "US-ASCII" => Us_Ascii,
113 "ISO-8859-1" => Iso_8859_1,
114 "ISO-8859-2" => Iso_8859_2,
115 "ISO-8859-3" => Iso_8859_3,
116 "ISO-8859-4" => Iso_8859_4,
117 "ISO-8859-5" => Iso_8859_5,
118 "ISO-8859-6" => Iso_8859_6,
119 "ISO-8859-7" => Iso_8859_7,
120 "ISO-8859-8" => Iso_8859_8,
121 "ISO-8859-9" => Iso_8859_9,
122 "ISO-8859-10" => Iso_8859_10,
123 "SHIFT-JIS" => Shift_Jis,
124 "EUC-JP" => Euc_Jp,
125 "ISO-2022-KR" => Iso_2022_Kr,
126 "EUC-KR" => Euc_Kr,
127 "ISO-2022-JP" => Iso_2022_Jp,
128 "ISO-2022-JP-2" => Iso_2022_Jp_2,
129 "ISO-8859-6-E" => Iso_8859_6_E,
130 "ISO-8859-6-I" => Iso_8859_6_I,
131 "ISO-8859-8-E" => Iso_8859_8_E,
132 "ISO-8859-8-I" => Iso_8859_8_I,
133 "GB2312" => Gb2312,
134 "5" => Big5,
135 "KOI8-R" => Koi8_R,
136 s => Ext(s.to_owned())
137 })
138 }
139}
140
141#[test]
142fn 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]
151fn test_display() {
152 assert_eq!("US-ASCII", format!("{}", Us_Ascii));
153 assert_eq!("ABCD", format!("{}", Ext("ABCD".to_owned())));
154}