Skip to content

Commit

Permalink
added string to iso code using strum (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhr0909 authored Jun 2, 2021
1 parent 71edfec commit 3d8cf9e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 10 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ rayon = "1.5.1"
regex = "1.5.4"
serde = { version = "1.0.126", features = ["derive"] }
serde_json = "1.0.64"
strum = "0.20.0"
strum_macros = "0.20.1"
strum = "0.21.0"
strum_macros = "0.21.0"
zip = "0.5.12"
lingua-afrikaans-language-model = { path = "language-models/af", version = "1.0.0" }
lingua-albanian-language-model = { path = "language-models/sq", version = "1.0.0" }
Expand Down
18 changes: 16 additions & 2 deletions src/isocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
*/

use std::fmt::{Debug, Display, Formatter, Result};
use std::str::FromStr;
use strum_macros::EnumString;

/// This enum specifies the ISO 639-1 code representations for the supported languages.
///
/// ISO 639 is a standardized nomenclature used to classify languages.
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, EnumString)]
#[allow(clippy::upper_case_acronyms)]
#[strum(ascii_case_insensitive)]
pub enum IsoCode639_1 {
/// The ISO 639-1 code for [`Afrikaans`](./enum.Language.html#variant.Afrikaans)
AF,
Expand Down Expand Up @@ -251,8 +254,9 @@ pub enum IsoCode639_1 {
/// This enum specifies the ISO 639-3 code representations for the supported languages.
///
/// ISO 639 is a standardized nomenclature used to classify languages.
#[derive(Debug, Eq, PartialEq)]
#[derive(Debug, Eq, PartialEq, EnumString)]
#[allow(clippy::upper_case_acronyms)]
#[strum(ascii_case_insensitive)]
pub enum IsoCode639_3 {
/// The ISO 639-3 code for [`Afrikaans`](./enum.Language.html#variant.Afrikaans)
AFR,
Expand Down Expand Up @@ -507,4 +511,14 @@ mod tests {
fn assert_iso_code_639_3_string_representation_is_correct() {
assert_eq!(IsoCode639_3::ENG.to_string(), "eng");
}

#[test]
fn assert_string_to_iso_code_639_1_is_correct() {
assert_eq!(IsoCode639_1::from_str("en").unwrap(), IsoCode639_1::EN);
}

#[test]
fn assert_string_to_iso_code_639_3_is_correct() {
assert_eq!(IsoCode639_3::from_str("eng").unwrap(), IsoCode639_3::ENG);
}
}
12 changes: 10 additions & 2 deletions src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ use crate::alphabet::Alphabet;
use crate::isocode::{IsoCode639_1, IsoCode639_3};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use std::str::FromStr;
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use strum_macros::{EnumIter, EnumString};

/// This enum specifies the so far 75 supported languages which can be detected by *Lingua*.
#[derive(Clone, Debug, Serialize, Deserialize, EnumIter, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Clone, Debug, Serialize, Deserialize, EnumIter, Eq, PartialEq, Hash, Ord, PartialOrd, EnumString)]
#[serde(rename_all(serialize = "UPPERCASE", deserialize = "UPPERCASE"))]
#[strum(ascii_case_insensitive)]
pub enum Language {
Afrikaans,
Albanian,
Expand Down Expand Up @@ -434,6 +436,12 @@ mod tests {
assert_eq!(deserialized, Language::English);
}

#[test]
fn test_from_str() {
let language = Language::from_str("english").unwrap();
assert_eq!(language, Language::English);
}

#[test]
fn assert_all_languages_are_available() {
assert_eq!(
Expand Down

0 comments on commit 3d8cf9e

Please sign in to comment.