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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! Implementation of the `emails` rule: check missing/extra/different emails.
use std::collections::HashSet;
use crate::checker::Checker;
use crate::diagnostic::{Diagnostic, Severity};
use crate::fix::{Edit, Fix, FixTarget};
use crate::po::entry::Entry;
use crate::po::format::iter::FormatEmailPos;
use crate::po::message::Message;
use crate::rules::double_quotes::trim_quotes;
use crate::rules::rule::RuleChecker;
pub struct EmailsRule;
impl RuleChecker for EmailsRule {
fn name(&self) -> &'static str {
"emails"
}
fn description(&self) -> &'static str {
"Check for missing, extra or different emails in translation."
}
fn is_default(&self) -> bool {
true
}
fn is_check(&self) -> bool {
true
}
/// Check for missing, extra or different emails in the translation.
///
/// Wrong entry:
/// ```text
/// msgid "Test email: user@example.com"
/// msgstr "Email de test : utilisateur@exemple.com"
/// ```
///
/// Correct entry:
/// ```text
/// msgid "Test email: user@example.com"
/// msgstr "Email de test : user@example.com"
/// ```
///
/// Diagnostics reported:
/// - [`warning`](Severity::Warning): `missing emails (# / #)`
/// - [`warning`](Severity::Warning): `extra emails (# / #)`
/// - [`warning`](Severity::Warning): `different emails` (auto-fixable)
///
/// Only the `different emails` diagnostic carries an auto-fix: each
/// translation email is replaced in place with the email at the same
/// position in the source. The `missing` and `extra` cases are left
/// unfixed because inserting a missing email at the right position in
/// the prose or choosing which extra to drop both require translator
/// judgement.
fn check_msg(
&self,
checker: &Checker,
entry: &Entry,
msgid: &Message,
msgstr: &Message,
) -> Vec<Diagnostic> {
let id_emails: Vec<_> = FormatEmailPos::new(&msgid.value, entry.format_language).collect();
let str_emails: Vec<_> =
FormatEmailPos::new(&msgstr.value, entry.format_language).collect();
match id_emails.len().cmp(&str_emails.len()) {
std::cmp::Ordering::Greater => self
.new_diag(
checker,
Severity::Warning,
format!(
"missing emails ({} / {})",
id_emails.len(),
str_emails.len()
),
)
.map(|d| {
d.with_msgs_hl(
msgid,
id_emails.iter().map(|m| (m.start, m.end)),
msgstr,
str_emails.iter().map(|m| (m.start, m.end)),
)
})
.into_iter()
.collect(),
std::cmp::Ordering::Less => self
.new_diag(
checker,
Severity::Warning,
format!("extra emails ({} / {})", id_emails.len(), str_emails.len()),
)
.map(|d| {
d.with_msgs_hl(
msgid,
id_emails.iter().map(|m| (m.start, m.end)),
msgstr,
str_emails.iter().map(|m| (m.start, m.end)),
)
})
.into_iter()
.collect(),
std::cmp::Ordering::Equal => {
// Check that emails are the same, in any order.
// A single pair of quotes is skipped from both sides of the email.
let id_emails_hash: HashSet<_> =
id_emails.iter().map(|m| trim_quotes(m.s)).collect();
let str_emails_hash: HashSet<_> =
str_emails.iter().map(|m| trim_quotes(m.s)).collect();
if id_emails_hash == str_emails_hash {
vec![]
} else {
let edits: Vec<Edit> = id_emails
.iter()
.zip(str_emails.iter())
.filter(|(id, str)| trim_quotes(id.s) != trim_quotes(str.s))
.map(|(id, str)| Edit {
range: str.start..str.end,
replacement: id.s.to_string(),
})
.collect();
let fix = (!edits.is_empty()).then(|| Fix {
target: FixTarget::Msgstr {
file_byte_range: msgstr.byte_range.clone(),
},
edits,
});
self.new_diag(checker, Severity::Warning, "different emails")
.map(|d| {
d.with_msgs_hl(
msgid,
id_emails.iter().map(|m| (m.start, m.end)),
msgstr,
str_emails.iter().map(|m| (m.start, m.end)),
)
.with_optional_fix(fix)
})
.into_iter()
.collect()
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{diagnostic::Diagnostic, rules::rule::Rules};
fn check_emails(content: &str) -> Vec<Diagnostic> {
let mut checker = Checker::new(content.as_bytes());
let rules = Rules::new(vec![Box::new(EmailsRule {})]);
checker.do_all_checks(&rules);
checker.diagnostics
}
#[test]
fn test_no_emails() {
let diags = check_emails(
r#"
msgid "tested"
msgstr "testé"
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_emails_ok() {
let diags = check_emails(
// Order of emails is not checked.
r#"
msgid "user@domain.com -- „user2@example.com”"
msgstr "user2@example.com -- „user@domain.com”"
"#,
);
println!("{diags:#?}");
assert!(diags.is_empty());
}
#[test]
fn test_quotes_are_not_emails() {
// A quoted "@" is not an email: the surrounding quotes must not be
// treated as email characters (regression for "missing emails (1 / 0)").
let diags = check_emails(
r#"
msgid "Not an e-mail: \"@\"."
msgstr "Le \"@\" n'est pas un e-mail."
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_emails_error() {
let diags = check_emails(
r#"
msgid "missing email: user@domain.com -- user2@example.com"
msgstr "e-mail manquant : user@domain.com"
msgid "extra email: user@domain.com"
msgstr "e-mail extra : user@domain.com -- user2@example.com"
msgid "different emails: user@test.domain.com -- user2@example.com"
msgstr "e-mails différents : user@domain.com -- user2@example.com"
"#,
);
assert_eq!(diags.len(), 3);
let diag = &diags[0];
assert_eq!(diag.severity, Severity::Warning);
assert_eq!(diag.message, "missing emails (2 / 1)");
let diag = &diags[1];
assert_eq!(diag.severity, Severity::Warning);
assert_eq!(diag.message, "extra emails (1 / 2)");
let diag = &diags[2];
assert_eq!(diag.severity, Severity::Warning);
assert_eq!(diag.message, "different emails");
}
#[test]
fn test_different_emails_fix_replaces_each_in_place() {
// Two emails, both differ from the source. The fix should propose two
// edits, each replacing the translation email with the source email
// at the same position.
let diags = check_emails(
r#"
msgid "Contact a@x.com or b@x.com"
msgstr "Contacter a2@x.com ou b2@x.com"
"#,
);
assert_eq!(diags.len(), 1);
assert_eq!(diags[0].message, "different emails");
let fix = diags[0].fix.as_ref().expect("fix attached");
assert_eq!(fix.edits.len(), 2);
// Edits are produced in source order; replacements come from msgid.
assert_eq!(fix.edits[0].replacement, "a@x.com");
assert_eq!(fix.edits[1].replacement, "b@x.com");
}
#[test]
fn test_different_emails_fix_skips_positions_already_equal() {
// First email matches the source; only the second differs. One edit.
let diags = check_emails(
r#"
msgid "Contact a@x.com or b@x.com"
msgstr "Contacter a@x.com ou b2@x.com"
"#,
);
assert_eq!(diags.len(), 1);
let fix = diags[0].fix.as_ref().expect("fix attached");
assert_eq!(fix.edits.len(), 1);
assert_eq!(fix.edits[0].replacement, "b@x.com");
}
#[test]
fn test_missing_and_extra_emails_have_no_fix() {
// The two count-mismatch diagnostics carry no fix.
let diags = check_emails(
r#"
msgid "Contact a@x.com or b@x.com"
msgstr "Contacter a@x.com"
msgid "Contact a@x.com"
msgstr "Contacter a@x.com ou b@x.com"
"#,
);
assert_eq!(diags.len(), 2);
assert!(
diags[0].fix.is_none(),
"missing emails diagnostic must not carry a fix"
);
assert!(
diags[1].fix.is_none(),
"extra emails diagnostic must not carry a fix"
);
}
}