summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-21 18:07:31 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-28 11:54:59 +0000
commit1f189f91addf905cab01934b14ba2f7edbab6bdf (patch)
tree4d7c0bc14ee4279927413b2d7d02e9bb2718712f
parent57f08af75f931eb0733d2f18784f2dc9872ab354 (diff)
downloadmullvadvpn-1f189f91addf905cab01934b14ba2f7edbab6bdf.tar.xz
mullvadvpn-1f189f91addf905cab01934b14ba2f7edbab6bdf.zip
Return result from `gettext::Messages::from_file`
Instead of panicking on error.
-rw-r--r--Cargo.lock19
-rw-r--r--android/translations-converter/Cargo.toml1
-rw-r--r--android/translations-converter/src/gettext/messages.rs23
-rw-r--r--android/translations-converter/src/main.rs8
4 files changed, 41 insertions, 10 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 57d8b725ae..781f984822 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -257,6 +257,12 @@ dependencies = [
]
[[package]]
+name = "convert_case"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
+
+[[package]]
name = "core-foundation"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -386,6 +392,18 @@ dependencies = [
]
[[package]]
+name = "derive_more"
+version = "0.99.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f82b1b72f1263f214c0f823371768776c4f5841b942c9883aa8e5ec584fd0ba6"
+dependencies = [
+ "convert_case",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
name = "digest"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3038,6 +3056,7 @@ dependencies = [
name = "translations-converter"
version = "0.1.0"
dependencies = [
+ "derive_more",
"htmlize",
"lazy_static",
"regex",
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;