diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-21 18:07:31 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-28 11:54:59 +0000 |
| commit | 1f189f91addf905cab01934b14ba2f7edbab6bdf (patch) | |
| tree | 4d7c0bc14ee4279927413b2d7d02e9bb2718712f /android | |
| parent | 57f08af75f931eb0733d2f18784f2dc9872ab354 (diff) | |
| download | mullvadvpn-1f189f91addf905cab01934b14ba2f7edbab6bdf.tar.xz mullvadvpn-1f189f91addf905cab01934b14ba2f7edbab6bdf.zip | |
Return result from `gettext::Messages::from_file`
Instead of panicking on error.
Diffstat (limited to 'android')
| -rw-r--r-- | android/translations-converter/Cargo.toml | 1 | ||||
| -rw-r--r-- | android/translations-converter/src/gettext/messages.rs | 23 | ||||
| -rw-r--r-- | android/translations-converter/src/main.rs | 8 |
3 files changed, 22 insertions, 10 deletions
diff --git a/android/translations-converter/Cargo.toml b/android/translations-converter/Cargo.toml index a5f01aa386..50b410de29 100644 --- a/android/translations-converter/Cargo.toml +++ b/android/translations-converter/Cargo.toml @@ -8,6 +8,7 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +derive_more = "0.99" htmlize = "0.5" lazy_static = "1" regex = "1" diff --git a/android/translations-converter/src/gettext/messages.rs b/android/translations-converter/src/gettext/messages.rs index 5effebf1b5..2b2777f78c 100644 --- a/android/translations-converter/src/gettext/messages.rs +++ b/android/translations-converter/src/gettext/messages.rs @@ -1,4 +1,5 @@ use super::{msg_string::MsgString, parse_line, plural_form::PluralForm}; +use derive_more::{Display, Error, From}; use std::{ collections::BTreeMap, fs::File, @@ -57,7 +58,7 @@ impl Messages { /// msgstr[0] "Unu tradukita mesaĝo" /// msgstr[1] "%d tradukitaj mesaĝoj" /// ``` - pub fn from_file(file_path: impl AsRef<Path>) -> Self { + pub fn from_file(file_path: impl AsRef<Path>) -> Result<Self, Error> { let mut parsing_header = false; let mut entries = Vec::new(); let mut current_id = None; @@ -68,12 +69,11 @@ impl Messages { let file = BufReader::new(File::open(file_path).expect("Failed to open gettext file")); // Ensure there's an empty line at the end so that the "else" part of the string matching // code will run for the last message in the file. - let lines = file - .lines() - .map(|line_result| line_result.expect("Failed to read from gettext file")) - .chain(Some(String::new())); + let lines = file.lines().chain(Some(Ok(String::new()))); + + for line_result in lines { + let line = line_result?; - for line in lines { match_str! { (line.trim()) ["msgid \"", msg_id, "\""] => { current_id = Some(MsgString::from_escaped(msg_id)); @@ -142,10 +142,10 @@ impl Messages { } } - Self { + Ok(Messages { entries, plural_form, - } + }) } } @@ -163,3 +163,10 @@ impl From<MsgString> for MsgValue { MsgValue::Invariant(string) } } + +#[derive(Debug, Display, Error, From)] +pub enum Error { + /// IO error while reading input file. + #[display(fmt = "Failed to read from the input file")] + Io(std::io::Error), +} diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index c985061c32..8444470cd3 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -98,19 +98,23 @@ fn main() { fs::create_dir(&destination_dir).expect("Failed to create Android locale directory"); } + let translations = gettext::Messages::from_file(&locale_file) + .expect("Failed to load translations for a locale"); + generate_translations( locale, known_urls.clone(), known_strings.clone(), known_plurals.clone(), - gettext::Messages::from_file(&locale_file), + translations, destination_dir.join("strings.xml"), destination_dir.join("plurals.xml"), ); } let template_path = locale_dir.join("messages.pot"); - let template = gettext::Messages::from_file(&template_path); + let template = gettext::Messages::from_file(&template_path) + .expect("Failed to load messages template file"); let mut missing_translations = known_strings; let mut missing_plurals: HashMap<_, _> = known_plurals; |
