Skip to content
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

WASM: change Language to be a proper TypeScript enum #5726

Merged
merged 1 commit into from
Feb 13, 2025
Merged
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
47 changes: 32 additions & 15 deletions ironfish-rust-wasm/src/keys/mnemonics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use wasm_bindgen::prelude::*;
#[wasm_bindgen]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum Language {
// These are the same language codes used by `bip39`
English = "en",
ChineseSimplified = "zh-hans",
ChineseTraditional = "zh-hant",
French = "fr",
Italian = "it",
Japanese = "ja",
Korean = "ko",
Spanish = "es",
English,
ChineseSimplified,
ChineseTraditional,
French,
Italian,
Japanese,
Korean,
Spanish,
}

impl From<bip39::Language> for Language {
Expand Down Expand Up @@ -46,20 +45,38 @@ impl From<Language> for bip39::Language {
Language::Japanese => Self::Japanese,
Language::Korean => Self::Korean,
Language::Spanish => Self::Spanish,
Language::__Invalid => unreachable!(),
}
}
}

#[wasm_bindgen]
impl Language {
#[wasm_bindgen(js_name = "fromLanguageCode")]
pub fn from_language_code(code: &str) -> Result<Self, IronfishError> {
Self::from_str(code).ok_or_else(|| IronfishErrorKind::InvalidLanguageEncoding.into())
// These are the same language codes used by `bip39`
match code {
"en" => Ok(Self::English),
"zh-hans" => Ok(Self::ChineseSimplified),
"zh-hant" => Ok(Self::ChineseTraditional),
"fr" => Ok(Self::French),
"it" => Ok(Self::Italian),
"ja" => Ok(Self::Japanese),
"ko" => Ok(Self::Korean),
"es" => Ok(Self::Spanish),
_ => Err(IronfishErrorKind::InvalidLanguageEncoding.into()),
}
}

#[wasm_bindgen(getter, js_name = "languageCode")]
pub fn language_code(self) -> String {
self.to_str().to_string()
// These are the same language codes used by `bip39`
match self {
Self::English => "en",
Self::ChineseSimplified => "zh-hans",
Self::ChineseTraditional => "zh-hant",
Self::French => "fr",
Self::Italian => "it",
Self::Japanese => "ja",
Self::Korean => "ko",
Self::Spanish => "es",
}
.to_string()
}
}