Skip to content

Adds a trait UniDecode for idiomatic transliteration. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unidecode"
version = "0.3.0"
version = "0.3.1"
authors = ["Amit Chowdhury <[email protected]>"]
description = "Provides pure ASCII transliterations of Unicode strings."
documentation = "https://chowdhurya.github.io/rust-unidecode/unidecode/"
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()`:
Expand Down
52 changes: 47 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ");
//! ```
Expand Down Expand Up @@ -63,11 +61,55 @@ pub fn unidecode(s: &str) -> String {
///
/// Examples
/// --------
/// ```ignore
/// ```
/// # use unidecode::unidecode_char;
/// assert_eq!(unidecode_char('Æ'), "AE");
/// assert_eq!(unidecode_char('北'), "Bei ");
/// ```
#[inline]
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)
}
}