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
// SPDX-FileCopyrightText: 2026 Sébastien Helleu <flashcode@flashtux.org>
//
// SPDX-License-Identifier: GPL-3.0-or-later
//! Implementation of the `paths` rule: check missing/extra/different paths.
use std::collections::HashSet;
use crate::checker::Checker;
use crate::diagnostic::{Diagnostic, Severity};
use crate::po::entry::Entry;
use crate::po::format::iter::FormatPathPos;
use crate::po::message::Message;
use crate::rules::double_quotes::DOUBLE_QUOTES;
use crate::rules::rule::RuleChecker;
pub struct PathsRule;
impl RuleChecker for PathsRule {
fn name(&self) -> &'static str {
"paths"
}
fn is_default(&self) -> bool {
false
}
fn is_check(&self) -> bool {
true
}
fn severity(&self) -> Severity {
Severity::Info
}
/// Check for missing, extra or different paths in the translation.
///
/// This rule is not enabled by default.
///
/// Wrong entry:
/// ```text
/// msgid "Path: /tmp/output.txt"
/// msgstr "Chemin : /tmp/sortie.txt"
/// ```
///
/// Correct entry:
/// ```text
/// msgid "Path: /tmp/output.txt"
/// msgstr "Chemin : /tmp/output.txt"
/// ```
///
/// Diagnostics reported with severity [`info`](Severity::Info):
/// - `missing paths (# / #)`
/// - `extra paths (# / #)`
/// - `different paths`
fn check_msg(
&self,
checker: &Checker,
entry: &Entry,
msgid: &Message,
msgstr: &Message,
) -> Vec<Diagnostic> {
let id_paths: Vec<_> = FormatPathPos::new(&msgid.value, &entry.format_language).collect();
let str_paths: Vec<_> = FormatPathPos::new(&msgstr.value, &entry.format_language).collect();
match id_paths.len().cmp(&str_paths.len()) {
std::cmp::Ordering::Greater => {
vec![
self.new_diag(
checker,
format!("missing paths ({} / {})", id_paths.len(), str_paths.len()),
)
.with_msgs_hl(
msgid,
&id_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
msgstr,
&str_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
),
]
}
std::cmp::Ordering::Less => {
vec![
self.new_diag(
checker,
format!("extra paths ({} / {})", id_paths.len(), str_paths.len()),
)
.with_msgs_hl(
msgid,
&id_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
msgstr,
&str_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
),
]
}
std::cmp::Ordering::Equal => {
// Check that paths are the same, in any order.
// A single pair of quotes is skipped from both sides of the path.
let id_paths_hash: HashSet<_> = id_paths.iter().map(|m| trim_quotes(m.s)).collect();
let str_paths_hash: HashSet<_> =
str_paths.iter().map(|m| trim_quotes(m.s)).collect();
if id_paths_hash == str_paths_hash {
vec![]
} else {
vec![
self.new_diag(checker, "different paths".to_string())
.with_msgs_hl(
msgid,
&id_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
msgstr,
&str_paths
.iter()
.map(|m| (m.start, m.end))
.collect::<Vec<_>>(),
),
]
}
}
}
}
}
/// Trim one pair of quotes from both sides of the path, if any.
///
/// The quote skipped at the beginning may be different from the quote at the end.
fn trim_quotes(s: &str) -> &str {
if s.starts_with(DOUBLE_QUOTES) && s.ends_with(DOUBLE_QUOTES) {
// Return the string without the first and last UTF-8 char.
let start = s.chars().next().unwrap().len_utf8();
let end = s.char_indices().next_back().unwrap().0;
return &s[start..end];
}
s
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{diagnostic::Diagnostic, rules::rule::Rules};
fn check_paths(content: &str) -> Vec<Diagnostic> {
let mut checker = Checker::new(content.as_bytes());
let rules = Rules::new(vec![Box::new(PathsRule {})]);
checker.do_all_checks(&rules);
checker.diagnostics
}
#[test]
fn test_no_paths() {
let diags = check_paths(
r#"
msgid "tested"
msgstr "testé"
"#,
);
assert!(diags.is_empty());
}
#[test]
fn test_paths_ok() {
let diags = check_paths(
// Order of paths is not checked.
r#"
msgid "/tmp/output.txt -- ./relative/path"
msgstr "./relative/path -- /tmp/output.txt"
"#,
);
println!("{diags:#?}");
assert!(diags.is_empty());
}
#[test]
fn test_paths_error() {
let diags = check_paths(
r#"
msgid "missing path: /tmp/output.txt -- ./relative/path"
msgstr "chemin manquant : /tmp/output.txt"
msgid "extra path: /tmp/output.txt"
msgstr "chemin extra : /tmp/output.txt -- ./relative/path"
msgid "different paths: /tmp/test/output.txt -- ./relative/path"
msgstr "chemins différents : /tmp/output.txt -- ./relative/path"
"#,
);
assert_eq!(diags.len(), 3);
let diag = &diags[0];
assert_eq!(diag.severity, Severity::Info);
assert_eq!(diag.message, "missing paths (2 / 1)");
let diag = &diags[1];
assert_eq!(diag.severity, Severity::Info);
assert_eq!(diag.message, "extra paths (1 / 2)");
let diag = &diags[2];
assert_eq!(diag.severity, Severity::Info);
assert_eq!(diag.message, "different paths");
}
}