diff --git a/Cargo.toml b/Cargo.toml index 77dd3a3..93ea482 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "unidecode" -version = "0.3.0" +version = "0.3.1" authors = ["Amit Chowdhury "] description = "Provides pure ASCII transliterations of Unicode strings." documentation = "https://chowdhurya.github.io/rust-unidecode/unidecode/" diff --git a/README.md b/README.md index 5813192..97cc093 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,22 @@ assert_eq!(unidecode("ᔕᓇᓇ"), "shanana"); assert_eq!(unidecode("げんまい茶"), "genmaiCha "); ``` +Altenatively, using UniDecode trait. + +```rust +use unidecode::UniDecode; + +// for string slices +assert_eq!("вопросов".unidecode(), "voprosov"); +assert_eq!("アリガトゥ".unidecode(), "arigatou"); + +// for chars +assert_eq!('™'.unidecode(), "tm"); +assert_eq!('®'.unidecode(), "(r)"); +assert_eq!('Æ'.unidecode(), "AE"); +``` + + Guarantees and Warnings ----------------------- Here are some guarantees you have when calling `unidecode()`: diff --git a/src/lib.rs b/src/lib.rs index d766bc2..9b0066f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,13 +13,11 @@ //! //! Examples //! -------- -//! ```ignore -//! extern crate unidecode; +//! ``` //! use unidecode::unidecode; -//! //! assert_eq!(unidecode("Æneid"), "AEneid"); //! assert_eq!(unidecode("étude"), "etude"); -//! assert_eq!(unidecode("北亰"), "Bei Jing"); +//! assert_eq!(unidecode("北亰"), "Bei Jing "); //! assert_eq!(unidecode("ᔕᓇᓇ"), "shanana"); //! assert_eq!(unidecode("げんまい茶"), "genmaiCha "); //! ``` @@ -63,7 +61,8 @@ pub fn unidecode(s: &str) -> String { /// /// Examples /// -------- -/// ```ignore +/// ``` +/// # use unidecode::unidecode_char; /// assert_eq!(unidecode_char('Æ'), "AE"); /// assert_eq!(unidecode_char('北'), "Bei "); /// ``` @@ -71,3 +70,46 @@ pub fn unidecode(s: &str) -> String { pub fn unidecode_char(ch: char) -> &'static str { MAPPING.get(ch as usize).map(|&s| s).unwrap_or("") } + +/// UniDecode Trait for idiomatic transliteration. +/// +/// With this trait _unidecode_ transliteration can be done in an idiomatic fashion. +/// +/// Examples +/// -------- +/// ``` +/// use unidecode::UniDecode; +/// +/// // for string slices +/// assert_eq!("Æneid".unidecode(), "AEneid"); +/// assert_eq!("вопросов".unidecode(), "voprosov"); +/// assert_eq!("北亰".unidecode(), "Bei Jing "); +/// assert_eq!("ᔕᓇᓇ".unidecode(), "shanana"); +/// assert_eq!("アリガトゥ".unidecode(), "arigatou"); +/// +/// // for chars +/// assert_eq!('™'.unidecode(), "tm"); +/// assert_eq!('®'.unidecode(), "(r)"); +/// assert_eq!('Æ'.unidecode(), "AE"); +/// assert_eq!('é'.unidecode(), "e"); +/// ``` +pub trait UniDecode { + type Output; + /// performs _unidecode_ transliteration + fn unidecode(&self) -> Self::Output; +} + +impl UniDecode for str { + type Output = String; + fn unidecode(&self) -> Self::Output { + unidecode(&self) + } +} + +impl UniDecode for char { + type Output = &'static str; + + fn unidecode(&self) -> Self::Output { + unidecode_char(*self) + } +}