diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-02-09 19:28:19 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-02-10 18:24:03 +0000 |
| commit | 1d28e962cd435c78fbbce1fc2d061f762f94d430 (patch) | |
| tree | 647f331dcf5618e007d752539302e7042d3e5a95 | |
| parent | 47640bba05dfcfef7e05e97a1c38bdad56188f40 (diff) | |
| download | mullvadvpn-1d28e962cd435c78fbbce1fc2d061f762f94d430.tar.xz mullvadvpn-1d28e962cd435c78fbbce1fc2d061f762f94d430.zip | |
Refactor to create `android::StringValue` type
| -rw-r--r-- | android/translations-converter/src/android.rs | 105 | ||||
| -rw-r--r-- | android/translations-converter/src/main.rs | 2 |
2 files changed, 74 insertions, 33 deletions
diff --git a/android/translations-converter/src/android.rs b/android/translations-converter/src/android.rs index d8c5f20cb2..430da6efcf 100644 --- a/android/translations-converter/src/android.rs +++ b/android/translations-converter/src/android.rs @@ -36,7 +36,7 @@ pub struct StringResource { /// The string value. #[serde(rename = "$value")] - pub value: String, + pub value: StringValue, } impl StringResources { @@ -91,26 +91,10 @@ impl StringResource { /// /// The name is the resource ID, and the value will be properly escaped. pub fn new(name: String, value: &str) -> Self { - let value_with_parameters = value - .replace(r"\", r"\\") - .replace("\"", "\\\"") - .replace(r"'", r"\'") - .replace("&", "&") - .replace("<", "<") - .replace(">", ">"); - - 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); - } - StringResource { name, translatable: true, - value, + value: StringValue::from(value), } } @@ -118,20 +102,7 @@ impl StringResource { /// /// 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.value, " "); - // Unescape apostrophes - let value = APOSTROPHES.replace_all(&value, "'"); - // Mark where parameters are positioned, removing the parameter index - let value = PARAMETERS.replace_all(&value, "%"); - // Unescape ampersands - let value = AMPERSANDS.replace_all(&value, "&"); - // Unescape less thans - let value = LESS_THANS.replace_all(&value, "<"); - // Unescape greater thans - let value = GREATER_THANS.replace_all(&value, ">"); - - self.value = value.into_owned(); + self.value.normalize(); } } @@ -309,3 +280,73 @@ 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 = string + .replace(r"\", r"\\") + .replace("\"", "\\\"") + .replace(r"'", r"\'") + .replace("&", "&") + .replace("<", "<") + .replace(">", ">"); + + 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, "'"); + // Mark where parameters are positioned, removing the parameter index + let value = PARAMETERS.replace_all(&value, "%"); + // Unescape ampersands + let value = AMPERSANDS.replace_all(&value, "&"); + // Unescape less thans + let value = LESS_THANS.replace_all(&value, "<"); + // Unescape greater thans + let value = GREATER_THANS.replace_all(&value, ">"); + + self.0 = value.into_owned(); + } + + /// 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/main.rs b/android/translations-converter/src/main.rs index 3f4499dd39..9aac19a2ee 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -62,7 +62,7 @@ fn main() { }; if destination - .insert(string.value.clone(), string.name) + .insert(string.value.to_string(), string.name) .is_some() { panic!( |
