summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-27 13:39:51 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-27 15:27:17 +0000
commit5902b0473682641aa472ec674f19aa8e40ae8899 (patch)
treedcc2301d6a6c605c8c412fe8df67cce2424f6bd5
parent49ba38b9ea62d5c6ad932e08f9e3d73f63bb7ce6 (diff)
downloadmullvadvpn-5902b0473682641aa472ec674f19aa8e40ae8899.tar.xz
mullvadvpn-5902b0473682641aa472ec674f19aa8e40ae8899.zip
Create `gettext:MsgString` type
A wrapper type with a custom `Display` implementation so that escaping the string can be properly taken care of.
-rw-r--r--android/translations-converter/src/gettext.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/android/translations-converter/src/gettext.rs b/android/translations-converter/src/gettext.rs
index 24efcdfdf1..62f274d394 100644
--- a/android/translations-converter/src/gettext.rs
+++ b/android/translations-converter/src/gettext.rs
@@ -2,9 +2,11 @@ use lazy_static::lazy_static;
use regex::Regex;
use std::{
collections::BTreeMap,
+ fmt::{self, Display, Formatter},
fs::{File, OpenOptions},
io::{self, BufRead, BufReader, BufWriter, Write},
mem,
+ ops::Deref,
path::Path,
};
@@ -48,6 +50,10 @@ pub enum MsgValue {
},
}
+/// A message string in a gettext translation file.
+#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
+pub struct MsgString(String);
+
/// A helper macro to match a string to various prefix and suffix combinations.
macro_rules! match_str {
(
@@ -217,6 +223,33 @@ impl PluralForm {
}
}
+impl From<String> for MsgString {
+ fn from(string: String) -> Self {
+ MsgString(string)
+ }
+}
+
+impl From<&str> for MsgString {
+ fn from(string: &str) -> Self {
+ string.to_owned().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)
+ }
+}
+
+impl Deref for MsgString {
+ type Target = str;
+
+ fn deref(&self) -> &Self::Target {
+ self.0.as_str()
+ }
+}
+
impl From<String> for MsgValue {
fn from(string: String) -> Self {
MsgValue::Invariant(string)