diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-03-29 22:16:03 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-19 12:13:36 +0000 |
| commit | b6497da49f7f31c2259dee709a0af1383d008017 (patch) | |
| tree | 459bbe967b61566d439b3fb5537a3897a96af739 | |
| parent | 10decde5021924f7c6b359bbeb7e45197f69b79a (diff) | |
| download | mullvadvpn-b6497da49f7f31c2259dee709a0af1383d008017.tar.xz mullvadvpn-b6497da49f7f31c2259dee709a0af1383d008017.zip | |
Move Android `StringValue` into a new module
| -rw-r--r-- | android/translations-converter/src/android/mod.rs | 76 | ||||
| -rw-r--r-- | android/translations-converter/src/android/string_value.rs | 78 |
2 files changed, 81 insertions, 73 deletions
diff --git a/android/translations-converter/src/android/mod.rs b/android/translations-converter/src/android/mod.rs index f49f50025c..71a9622f1a 100644 --- a/android/translations-converter/src/android/mod.rs +++ b/android/translations-converter/src/android/mod.rs @@ -1,18 +1,12 @@ -use lazy_static::lazy_static; -use regex::Regex; +mod string_value; + +use self::string_value::StringValue; use serde::{Deserialize, Serialize}; use std::{ fmt::{self, Display, Formatter}, ops::{Deref, DerefMut}, }; -lazy_static! { - static ref LINE_BREAKS: Regex = Regex::new(r"\s*\n\s*").unwrap(); - static ref APOSTROPHES: Regex = Regex::new(r"\\'").unwrap(); - static ref DOUBLE_QUOTES: Regex = Regex::new(r#"\\""#).unwrap(); - static ref PARAMETERS: Regex = Regex::new(r"%[0-9]*\$").unwrap(); -} - /// Contents of an Android string resources file. /// /// This type can be created directly deserializing the `strings.xml` file. @@ -281,67 +275,3 @@ impl Display for PluralQuantity { write!(formatter, "{}", quantity) } } - -/// An Android string value -/// -/// Handles escaping the string when it is created but also allows normalizing it for comparing it -/// with gettext messages through a `normalize` method. -#[derive(Clone, Debug, Eq, Deserialize, Hash, PartialEq, Serialize)] -pub struct StringValue(String); - -impl From<&str> for StringValue { - fn from(string: &str) -> Self { - let value_with_parameters = htmlize::escape_text(string) - .replace(r"\", r"\\") - .replace("\"", "\\\"") - .replace(r"'", r"\'"); - - let mut parts = value_with_parameters.split("%"); - let mut value = parts.next().unwrap().to_owned(); - - for (index, part) in parts.enumerate() { - value.push_str(&format!("%{}$", index + 1)); - value.push_str(part); - } - - StringValue(value) - } -} - -impl StringValue { - /// Normalize the string value into a common format. - /// - /// Makes it possible to compare the Android strings with the gettext messages. - pub fn normalize(&mut self) { - // Collapse line breaks present in the XML file - let value = LINE_BREAKS.replace_all(&self.0, " "); - // Unescape apostrophes - let value = APOSTROPHES.replace_all(&value, "'"); - // Unescape double quotes - let value = DOUBLE_QUOTES.replace_all(&value, r#"""#); - // Mark where parameters are positioned, removing the parameter index - let value = PARAMETERS.replace_all(&value, "%"); - - // Unescape XML characters - self.0 = htmlize::unescape(value.as_bytes()); - } - - /// Clones the internal string value. - pub fn to_string(&self) -> String { - self.0.clone() - } -} - -impl Deref for StringValue { - type Target = str; - - fn deref(&self) -> &Self::Target { - self.0.as_str() - } -} - -impl Display for StringValue { - fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - write!(formatter, "{}", self.0) - } -} diff --git a/android/translations-converter/src/android/string_value.rs b/android/translations-converter/src/android/string_value.rs new file mode 100644 index 0000000000..c07967179c --- /dev/null +++ b/android/translations-converter/src/android/string_value.rs @@ -0,0 +1,78 @@ +use lazy_static::lazy_static; +use regex::Regex; +use serde::{Deserialize, Serialize}; +use std::{ + fmt::{self, Display, Formatter}, + ops::Deref, +}; + +lazy_static! { + static ref LINE_BREAKS: Regex = Regex::new(r"\s*\n\s*").unwrap(); + static ref APOSTROPHES: Regex = Regex::new(r"\\'").unwrap(); + static ref DOUBLE_QUOTES: Regex = Regex::new(r#"\\""#).unwrap(); + static ref PARAMETERS: Regex = Regex::new(r"%[0-9]*\$").unwrap(); +} + +/// An Android string value +/// +/// Handles escaping the string when it is created but also allows normalizing it for comparing it +/// with gettext messages through a `normalize` method. +#[derive(Clone, Debug, Eq, Deserialize, Hash, PartialEq, Serialize)] +pub struct StringValue(String); + +impl From<&str> for StringValue { + fn from(string: &str) -> Self { + let value_with_parameters = htmlize::escape_text(string) + .replace(r"\", r"\\") + .replace("\"", "\\\"") + .replace(r"'", r"\'"); + + let mut parts = value_with_parameters.split("%"); + let mut value = parts.next().unwrap().to_owned(); + + for (index, part) in parts.enumerate() { + value.push_str(&format!("%{}$", index + 1)); + value.push_str(part); + } + + StringValue(value) + } +} + +impl StringValue { + /// Normalize the string value into a common format. + /// + /// Makes it possible to compare the Android strings with the gettext messages. + pub fn normalize(&mut self) { + // Collapse line breaks present in the XML file + let value = LINE_BREAKS.replace_all(&self.0, " "); + // Unescape apostrophes + let value = APOSTROPHES.replace_all(&value, "'"); + // Unescape double quotes + let value = DOUBLE_QUOTES.replace_all(&value, r#"""#); + // Mark where parameters are positioned, removing the parameter index + let value = PARAMETERS.replace_all(&value, "%"); + + // Unescape XML characters + self.0 = htmlize::unescape(value.as_bytes()); + } + + /// Clones the internal string value. + pub fn to_string(&self) -> String { + self.0.clone() + } +} + +impl Deref for StringValue { + type Target = str; + + fn deref(&self) -> &Self::Target { + self.0.as_str() + } +} + +impl Display for StringValue { + fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { + write!(formatter, "{}", self.0) + } +} |
