diff options
| author | Nicholas Rodrigues Lordello <[email protected]> | 2020-11-28 10:48:42 +0100 |
|---|---|---|
| committer | Nicholas Rodrigues Lordello <[email protected]> | 2020-11-28 10:48:42 +0100 |
| commit | bd7a6880f5dce74fd154f593a5a5f7034affeee4 (patch) | |
| tree | 09fc799b239632a5ebe854d62cb2c7c3574d55ac /src/mnemonic/language.rs | |
| parent | cd7beb199e654825cb1b3ab6ac85b805ffdb7871 (diff) | |
started implementing mnemonic word lists
Diffstat (limited to 'src/mnemonic/language.rs')
| -rw-r--r-- | src/mnemonic/language.rs | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/mnemonic/language.rs b/src/mnemonic/language.rs new file mode 100644 index 0000000..87950d8 --- /dev/null +++ b/src/mnemonic/language.rs @@ -0,0 +1,60 @@ +//! Mnemonic language for selecting word lists. + +use anyhow::{bail, Result}; +use std::{ + fmt::{self, Display, Formatter}, + str::FromStr, +}; + +/// The mnemonic langage used to select the word list. +#[derive(Clone, Copy, Debug)] +pub enum Language { + English, + // TODO(nlordell): Support more languages. Note that this is not necessarily + // trivial as some have specific considerations (like 'ñ' being equivalent + // to 'n' in Spanish, and Japanese using '\u{3000}` for spaces). +} + +impl Language { + /// Splits a mnemonic phrase into its words, returning the detected language + /// and a vector of **normalized** words. + pub fn split(phrase: impl AsRef<str>) -> Result<(Self, Vec<String>)> { + // TODO(nlordell): A lot to do here... + Ok(( + Language::English, + phrase + .as_ref() + .trim() + .split_whitespace() + .filter(|word| !word.is_empty()) + .map(|word| word.to_lowercase()) + .collect(), + )) + } +} + +impl Default for Language { + fn default() -> Self { + // TODO(nlordell): Read the default language from the system locale. + Language::English + } +} + +impl Display for Language { + fn fmt(&self, f: &mut Formatter) -> fmt::Result { + f.write_str(match self { + Language::English => "english", + }) + } +} + +impl FromStr for Language { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result<Self> { + Ok(match s.to_lowercase().as_str() { + "english" => Language::English, + _ => bail!("unsupported language '{}'", s), + }) + } +} |
