add utf-8 character encoding

This commit is contained in:
Pyfisch 2015-10-17 13:41:15 +02:00
parent 2aa215fd5c
commit 21f1730f9d
2 changed files with 49 additions and 45 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "charsets"
version = "0.1.0"
version = "0.1.1"
authors = ["Pyfisch <pyfisch@gmail.com>"]
description = "An enum representing all charset names commonly used."
readme = "README.md"

View File

@ -22,12 +22,12 @@ pub use self::Charset::*;
#[derive(Debug, Eq, PartialEq)]
pub enum Error {
/// Parsing as as charset failed.
Invalid
Invalid,
}
impl ErrorTrait for Error {
fn description(&self) -> &str {
return "The given charset is invalid"
return "The given charset is invalid";
}
}
@ -95,12 +95,13 @@ pub enum Charset{
Big5,
/// KOI8-R
Koi8R,
/// UTF-8
Utf8,
/// An arbitrary charset specified as a string
Unregistered(String)
Unregistered(String),
}
const MAPPING: [(Charset, &'static str); 24] = [
(UsAscii, "US-ASCII"),
const MAPPING: [(Charset, &'static str); 25] = [(UsAscii, "US-ASCII"),
(Iso88591, "ISO-8859-1"),
(Iso88592, "ISO-8859-2"),
(Iso88593, "ISO-8859-3"),
@ -123,17 +124,18 @@ const MAPPING: [(Charset, &'static str); 24] = [
(Iso88598I, "ISO-8859-8-I"),
(Gb2312, "GB2312"),
(Big5, "5"),
(Koi8R, "KOI8-R")
];
(Koi8R, "KOI8-R"),
(Utf8, "utf-8")];
impl Charset {
fn name(&self) -> &str {
if let &Unregistered(ref s) = self {
return &s[..]
return &s[..];
}
MAPPING.iter()
.find(|&&(ref variant, _)| self == variant)
.map(|&(_, name)| name).unwrap()
.map(|&(_, name)| name)
.unwrap()
}
}
@ -179,9 +181,10 @@ impl PartialEq for Charset {
(&Iso88598I, &Iso88598I) |
(&Gb2312, &Gb2312) |
(&Big5, &Big5) |
(&Koi8R, &Koi8R) => true,
(&Koi8R, &Koi8R) |
(&Utf8, &Utf8) => true,
(&Unregistered(ref s), &Unregistered(ref t)) => s.eq_ignore_ascii_case(t),
_ => false
_ => false,
}
}
}
@ -209,6 +212,7 @@ mod tests {
fn test_cmp() {
assert!(Iso88593 == Iso88593);
assert!(UsAscii != Iso88593);
assert_eq!(Unregistered("foobar".to_owned()), Unregistered("FOOBAR".to_owned()));
assert_eq!(Unregistered("foobar".to_owned()),
Unregistered("FOOBAR".to_owned()));
}
}