1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
use std::{
fmt::{self, Display, Formatter},
ops::{Add, AddAssign, Deref},
};
/// A message string in a gettext translation file.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct 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 common C escape sequences properly created for special
/// characters. It will not attempt to escape non-ASCII characters and will just keep them as
/// UTF-8 characters.
pub fn from_unescaped(string: &str) -> Self {
let string = string.replace('\\', r"\\");
let string = string.replace('\n', r"\n");
let string = string.replace('\r', r"\r");
let string = string.replace('\t', r"\t");
let string = string.replace('"', r#"\""#);
MsgString(string)
}
/// 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.fmt(formatter)
}
}
impl Deref for MsgString {
type Target = str;
fn deref(&self) -> &Self::Target {
self.0.as_str()
}
}
impl AsRef<MsgString> for MsgString {
fn as_ref(&self) -> &Self {
self
}
}
impl<M> AddAssign<M> for MsgString
where
M: AsRef<MsgString>,
{
fn add_assign(&mut self, other: M) {
self.0 += &other.as_ref().0;
}
}
impl<M> Add<M> for MsgString
where
M: AsRef<MsgString>,
{
type Output = MsgString;
fn add(mut self, other: M) -> Self::Output {
self += other;
self
}
}
impl<'r> Add<&'r MsgString> for &MsgString {
type Output = MsgString;
fn add(self, other: &'r MsgString) -> Self::Output {
MsgString(self.0.clone() + &other.0)
}
}
#[cfg(test)]
mod tests {
use super::MsgString;
#[test]
fn empty_constructor() {
let input = MsgString::empty();
assert_eq!(input.to_string(), "");
}
#[test]
fn escaping() {
let input = MsgString::from_unescaped(concat!(
r#""Inside double quotes""#,
r"'Inside single quotes'",
r"Back-slash character: \",
"Whitespace characters: \n\r\t",
));
let expected = concat!(
r#"\"Inside double quotes\""#,
"'Inside single quotes'",
r"Back-slash character: \\",
r"Whitespace characters: \n\r\t",
);
assert_eq!(input.to_string(), expected);
}
#[test]
fn not_escaping() {
let original = r#"\"Inside double quotes\""#;
let input = MsgString::from_escaped(original);
assert_eq!(input.to_string(), original);
}
#[test]
fn appending() {
let mut target = MsgString::from_unescaped(r#""Initial""#);
let extra = MsgString::from_escaped(r#"\"Extra\""#);
target += extra;
let expected = concat!(r#"\"Initial\"#, r#""\"Extra\""#);
assert_eq!(target.to_string(), expected);
}
#[test]
fn concatenating_by_moving() {
let start = MsgString::from_unescaped(r#""Start""#);
let end = MsgString::from_escaped(r#"\"End\""#);
let result = start + end;
let expected = concat!(r#"\"Start\"#, r#""\"End\""#);
assert_eq!(result.to_string(), expected);
}
#[test]
fn concatenating_by_borrowing() {
let start = MsgString::from_escaped(r#"\"Start\""#);
let end = MsgString::from_unescaped(r#""End""#);
let result = &start + &end;
let expected = concat!(r#"\"Start\"#, r#""\"End\""#);
assert_eq!(result.to_string(), expected);
}
}
|