diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-11 22:12:46 +0000 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2021-05-19 12:13:36 +0000 |
| commit | 5acea54d49b3e44ab92a4e28d4e4e82a6859b142 (patch) | |
| tree | 24743e50c1673db2fa24527bf2d6e549c43fb073 | |
| parent | 9b8d75866eb0742ef05bcd739d5439b20bee3062 (diff) | |
| download | mullvadvpn-5acea54d49b3e44ab92a4e28d4e4e82a6859b142.tar.xz mullvadvpn-5acea54d49b3e44ab92a4e28d4e4e82a6859b142.zip | |
Improve construction semantics for `MsgString`
| -rw-r--r-- | android/translations-converter/src/gettext/mod.rs | 8 | ||||
| -rw-r--r-- | android/translations-converter/src/gettext/msg_string.rs | 27 | ||||
| -rw-r--r-- | android/translations-converter/src/main.rs | 27 |
3 files changed, 34 insertions, 28 deletions
diff --git a/android/translations-converter/src/gettext/mod.rs b/android/translations-converter/src/gettext/mod.rs index 2b529c0eb5..d3be28c4ef 100644 --- a/android/translations-converter/src/gettext/mod.rs +++ b/android/translations-converter/src/gettext/mod.rs @@ -190,9 +190,9 @@ impl IntoIterator for Translation { } } -impl From<String> for MsgValue { - fn from(string: String) -> Self { - MsgValue::Invariant(string.into()) +impl From<MsgString> for MsgValue { + fn from(string: MsgString) -> Self { + MsgValue::Invariant(string) } } @@ -250,5 +250,5 @@ fn normalize(string: &str) -> MsgString { // Remove escaped double-quotes let string = ESCAPED_DOUBLE_QUOTES.replace_all(&string, r#"""#); - string.into_owned().into() + MsgString::from_escaped(string) } diff --git a/android/translations-converter/src/gettext/msg_string.rs b/android/translations-converter/src/gettext/msg_string.rs index 7419b6bd94..7da987f70e 100644 --- a/android/translations-converter/src/gettext/msg_string.rs +++ b/android/translations-converter/src/gettext/msg_string.rs @@ -4,25 +4,34 @@ use std::{ }; /// A message string in a gettext translation file. -#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Clone, Debug)] pub struct MsgString(String); -impl From<String> for MsgString { - fn from(string: String) -> Self { - MsgString(string) +impl MsgString { + /// Create a new empty `MsgString`. + /// + /// Equivalent to `MsgString::from_escaped("")`. + pub fn empty() -> Self { + MsgString(String::new()) + } + + /// Create a new `MsgString` from string without any escaped characters. + /// + /// This will ensure that the string has the double quotes characters properly escaped. + pub fn from_unescaped(string: &str) -> Self { + MsgString(string.replace(r#"""#, r#"\""#)) } -} -impl From<&str> for MsgString { - fn from(string: &str) -> Self { - string.to_owned().into() + /// Create a new `MsgString` from string that already has proper escaping. + pub fn from_escaped(string: impl Into<String>) -> Self { + MsgString(string.into()) } } impl Display for MsgString { /// Write the ID message string with proper escaping. fn fmt(&self, formatter: &mut Formatter) -> fmt::Result { - self.0.replace(r#"""#, r#"\""#).fmt(formatter) + self.0.fmt(formatter) } } diff --git a/android/translations-converter/src/main.rs b/android/translations-converter/src/main.rs index b5fb61838c..bf8d0d8cf3 100644 --- a/android/translations-converter/src/main.rs +++ b/android/translations-converter/src/main.rs @@ -131,8 +131,8 @@ fn main() { .into_iter() .inspect(|(missing_translation, id)| println!(" {}: {}", id, missing_translation)) .map(|(id, _)| gettext::MsgEntry { - id: id.into(), - value: String::new().into(), + id: gettext::MsgString::from_unescaped(&id), + value: gettext::MsgString::empty().into(), }), ) .expect("Failed to append missing translations to message template file"); @@ -163,30 +163,27 @@ fn main() { .iter() .position(|plural| plural.quantity == android::PluralQuantity::One) .expect("Missing singular variant to use as msgid"); - let id = plural - .items - .remove(singular_position) - .string - .to_string() - .into(); + let id = gettext::MsgString::from_escaped( + plural.items.remove(singular_position).string.to_string(), + ); let other_position = plural .items .iter() .position(|plural| plural.quantity == android::PluralQuantity::Other) .expect("Missing other variant to use as msgid_plural"); - let plural_id = plural - .items - .remove(other_position) - .string - .to_string() - .into(); + let plural_id = gettext::MsgString::from_escaped( + plural.items.remove(other_position).string.to_string(), + ); gettext::MsgEntry { id, value: gettext::MsgValue::Plural { plural_id, - values: vec!["".into(), "".into()], + values: vec![ + gettext::MsgString::empty().into(), + gettext::MsgString::empty().into(), + ], }, } }), |
