add utf-8 character encoding
This commit is contained in:
parent
2aa215fd5c
commit
21f1730f9d
@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "charsets"
|
name = "charsets"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
authors = ["Pyfisch <pyfisch@gmail.com>"]
|
authors = ["Pyfisch <pyfisch@gmail.com>"]
|
||||||
description = "An enum representing all charset names commonly used."
|
description = "An enum representing all charset names commonly used."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
28
src/lib.rs
28
src/lib.rs
@ -22,12 +22,12 @@ pub use self::Charset::*;
|
|||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Parsing as as charset failed.
|
/// Parsing as as charset failed.
|
||||||
Invalid
|
Invalid,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ErrorTrait for Error {
|
impl ErrorTrait for Error {
|
||||||
fn description(&self) -> &str {
|
fn description(&self) -> &str {
|
||||||
return "The given charset is invalid"
|
return "The given charset is invalid";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -95,12 +95,13 @@ pub enum Charset{
|
|||||||
Big5,
|
Big5,
|
||||||
/// KOI8-R
|
/// KOI8-R
|
||||||
Koi8R,
|
Koi8R,
|
||||||
|
/// UTF-8
|
||||||
|
Utf8,
|
||||||
/// An arbitrary charset specified as a string
|
/// An arbitrary charset specified as a string
|
||||||
Unregistered(String)
|
Unregistered(String),
|
||||||
}
|
}
|
||||||
|
|
||||||
const MAPPING: [(Charset, &'static str); 24] = [
|
const MAPPING: [(Charset, &'static str); 25] = [(UsAscii, "US-ASCII"),
|
||||||
(UsAscii, "US-ASCII"),
|
|
||||||
(Iso88591, "ISO-8859-1"),
|
(Iso88591, "ISO-8859-1"),
|
||||||
(Iso88592, "ISO-8859-2"),
|
(Iso88592, "ISO-8859-2"),
|
||||||
(Iso88593, "ISO-8859-3"),
|
(Iso88593, "ISO-8859-3"),
|
||||||
@ -123,17 +124,18 @@ const MAPPING: [(Charset, &'static str); 24] = [
|
|||||||
(Iso88598I, "ISO-8859-8-I"),
|
(Iso88598I, "ISO-8859-8-I"),
|
||||||
(Gb2312, "GB2312"),
|
(Gb2312, "GB2312"),
|
||||||
(Big5, "5"),
|
(Big5, "5"),
|
||||||
(Koi8R, "KOI8-R")
|
(Koi8R, "KOI8-R"),
|
||||||
];
|
(Utf8, "utf-8")];
|
||||||
|
|
||||||
impl Charset {
|
impl Charset {
|
||||||
fn name(&self) -> &str {
|
fn name(&self) -> &str {
|
||||||
if let &Unregistered(ref s) = self {
|
if let &Unregistered(ref s) = self {
|
||||||
return &s[..]
|
return &s[..];
|
||||||
}
|
}
|
||||||
MAPPING.iter()
|
MAPPING.iter()
|
||||||
.find(|&&(ref variant, _)| self == variant)
|
.find(|&&(ref variant, _)| self == variant)
|
||||||
.map(|&(_, name)| name).unwrap()
|
.map(|&(_, name)| name)
|
||||||
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -179,9 +181,10 @@ impl PartialEq for Charset {
|
|||||||
(&Iso88598I, &Iso88598I) |
|
(&Iso88598I, &Iso88598I) |
|
||||||
(&Gb2312, &Gb2312) |
|
(&Gb2312, &Gb2312) |
|
||||||
(&Big5, &Big5) |
|
(&Big5, &Big5) |
|
||||||
(&Koi8R, &Koi8R) => true,
|
(&Koi8R, &Koi8R) |
|
||||||
|
(&Utf8, &Utf8) => true,
|
||||||
(&Unregistered(ref s), &Unregistered(ref t)) => s.eq_ignore_ascii_case(t),
|
(&Unregistered(ref s), &Unregistered(ref t)) => s.eq_ignore_ascii_case(t),
|
||||||
_ => false
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,6 +212,7 @@ mod tests {
|
|||||||
fn test_cmp() {
|
fn test_cmp() {
|
||||||
assert!(Iso88593 == Iso88593);
|
assert!(Iso88593 == Iso88593);
|
||||||
assert!(UsAscii != Iso88593);
|
assert!(UsAscii != Iso88593);
|
||||||
assert_eq!(Unregistered("foobar".to_owned()), Unregistered("FOOBAR".to_owned()));
|
assert_eq!(Unregistered("foobar".to_owned()),
|
||||||
|
Unregistered("FOOBAR".to_owned()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user