-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
system/locale: Split iso_639 into 2 files, cleanup and reorganise
Signed-off-by: Ikey Doherty <[email protected]>
- Loading branch information
Showing
5 changed files
with
178 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// SPDX-FileCopyrightText: Copyright © 2024 Serpent OS Developers | ||
// | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
//! Parsing for ISO-639 files from iso-codes | ||
//! Essentially, loading of languages | ||
use serde::Deserialize; | ||
|
||
use super::Language; | ||
|
||
/// JSON document for ISO-639-2 | ||
#[derive(Deserialize)] | ||
pub struct Document<'a> { | ||
#[serde(rename = "639-2", borrow)] | ||
pub entries: Vec<Entry<'a>>, | ||
} | ||
|
||
/// A single entry in the JSON document | ||
#[derive(Deserialize)] | ||
pub struct Entry<'a> { | ||
#[serde(rename = "alpha_2", borrow)] | ||
pub code2: Option<&'a str>, | ||
|
||
#[serde(rename = "alpha_3", borrow)] | ||
pub code3: &'a str, | ||
|
||
/// Official display name | ||
#[serde(borrow)] | ||
pub name: &'a str, | ||
|
||
/// Common name (optional) | ||
#[serde(borrow)] | ||
pub common_name: Option<&'a str>, | ||
|
||
/// Three letter bibliographic | ||
pub bibliographic: Option<&'a str>, | ||
} | ||
|
||
impl From<&Entry<'_>> for Language { | ||
/// Convert iso entry into Language | ||
fn from(value: &Entry<'_>) -> Self { | ||
Self { | ||
code: value.code3.into(), | ||
code2: value.code2.map(|v| v.into()), | ||
display_name: value.name.into(), | ||
inverted_name: None, | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::Document; | ||
|
||
#[test] | ||
fn load_iso_639_2() { | ||
const TEST_DATA: &str = r#" | ||
{ | ||
"639-2": [ | ||
{ | ||
"alpha_2": "gd", | ||
"alpha_3": "gla", | ||
"name": "Gaelic; Scottish Gaelic" | ||
}, | ||
{ | ||
"alpha_2": "ga", | ||
"alpha_3": "gle", | ||
"name": "Irish" | ||
}, | ||
{ | ||
"alpha_2": "gl", | ||
"alpha_3": "glg", | ||
"name": "Galician" | ||
} | ||
] | ||
} | ||
"#; | ||
|
||
let loaded = serde_json::from_str::<Document>(TEST_DATA).expect("Failed to decode ISO-639-2 data"); | ||
let ga = loaded | ||
.entries | ||
.iter() | ||
.find(|i| i.code3 == "gle") | ||
.expect("Failed to find GLE"); | ||
assert_eq!(ga.name, "Irish"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.