breaking change: FromStr: return Charset in Err

This commit is contained in:
Emma Tebibyte 2023-03-07 16:53:12 -05:00
parent 9b164fdbfb
commit dee28d0028
2 changed files with 50 additions and 12 deletions

View File

@ -1,7 +1,10 @@
[package] [package]
name = "charsets" name = "charsets"
version = "0.2.0" version = "0.3.0"
authors = ["Pyfisch <pyfisch@gmail.com>"] authors = [
"Pyfisch <pyfisch@gmail.com>",
"Emma Tebibyte <emma@tebibyte.media>",
]
description = "An enum representing all charset names commonly used." description = "An enum representing all charset names commonly used."
readme = "README.md" readme = "README.md"
documentation = "http://pyfisch.github.io/rust-charsets/rust-charsets/index.html" documentation = "http://pyfisch.github.io/rust-charsets/rust-charsets/index.html"

View File

@ -1,3 +1,27 @@
/*
* Copyright © 2016 pyfisch <pyfisch@gmail.com>
* Copyright © 2023 Emma Tebibyte <emma@tebibyte.media>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the Software), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#![no_std]
#![cfg_attr(test, deny(missing_docs))] #![cfg_attr(test, deny(missing_docs))]
#![cfg_attr(test, deny(warnings))] #![cfg_attr(test, deny(warnings))]
@ -9,9 +33,17 @@
//! Charset names can be parsed from string, formatted to string and compared. //! Charset names can be parsed from string, formatted to string and compared.
//! Unregistered charsets are represented using an `Unregistered` variant. //! Unregistered charsets are represented using an `Unregistered` variant.
use std::fmt::{self, Display}; extern crate alloc;
use std::str::FromStr;
use std::ascii::AsciiExt; use alloc::{
borrow::ToOwned,
string::{ String, ToString },
};
use core::{
fmt::{self, Display},
str::FromStr,
};
pub use self::Charset::*; pub use self::Charset::*;
@ -121,12 +153,14 @@ impl Display for Charset {
} }
impl FromStr for Charset { impl FromStr for Charset {
type Err = (); type Err = Charset;
fn from_str(s: &str) -> Result<Charset, ()> { fn from_str(s: &str) -> Result<Charset, Charset> {
Ok(MAPPING.iter() match MAPPING.iter()
.find(|&&(_, ref name)| name.eq_ignore_ascii_case(s)) .find(|&&(_, ref name)| name.eq_ignore_ascii_case(s))
.map(|&(ref variant, _)| variant.to_owned()) .map(|&(ref variant, _)| variant.to_owned()) {
.unwrap_or(Unregistered(s.to_owned()))) Some(variant) => Ok(variant),
None => Err(Unregistered(s.to_owned())),
}
} }
} }
@ -174,7 +208,8 @@ mod tests {
assert_eq!(UsAscii, "US-Ascii".parse().unwrap()); assert_eq!(UsAscii, "US-Ascii".parse().unwrap());
assert_eq!(UsAscii, "US-ASCII".parse().unwrap()); assert_eq!(UsAscii, "US-ASCII".parse().unwrap());
assert_eq!(ShiftJis, "Shift-JIS".parse().unwrap()); assert_eq!(ShiftJis, "Shift-JIS".parse().unwrap());
assert_eq!(Unregistered("ABCD".to_owned()), "abcd".parse().unwrap()); assert_eq!(Unregistered("ABCD".to_owned()),
"abcd".parse::<Charset>().err().unwrap());
} }
#[test] #[test]