mf2-cli 2.0.0

The `mf2` command: init a starter application, and check, compile, fmt, stats, dump, pseudo, export/import, watch and convert a MessageFormat 2 corpus.
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
//! `mf2 export` / `mf2 import`: flat JSON, the shape every
//! translation-management system speaks, and XLIFF 2, the standard a
//! translation tool protects placeholders in ([`xliff`];
//! `plans/05-tooling.md` §6.3).
//!
//! Export writes `{id: source}` sorted by id. Import reads one back into the
//! container the locale already uses: a `.mf2` resource keeps its sections,
//! comments and properties, and only the values change — a translator's
//! round trip must not throw away the context the source carries. Before it
//! writes anything, import runs every check `mf2 check` makes on the files
//! as they would be, and writes nothing if that brings an error (Phase 10
//! E3).

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

pub(crate) mod xliff;

use clap::{Args as ClapArgs, ValueEnum};
use mf2_build::loader::{Loader, json, resource};
use mf2_build::{Build, Config, Diagnostic, Emit, Features, Layout, Level, Lint, Report};
use mf2_resource::{parse, serialize_with};

use crate::FeatureArgs;
use crate::error::{Error, Result, read, write};

/// What `mf2 export` writes.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, ValueEnum)]
pub(crate) enum ExportFormat {
    /// `{id: source}`, the locale's own messages.
    #[default]
    Json,
    /// An XLIFF 2 document: the source locale's messages, with the locale's
    /// translations as targets.
    Xliff,
}

/// `mf2 export`.
#[derive(Debug, ClapArgs)]
pub(crate) struct ExportArgs {
    /// The locale to write.
    #[arg(value_name = "LOCALE")]
    locale: String,
    /// Where to write it; standard output by default.
    #[arg(long, short, value_name = "FILE")]
    pub(crate) out: Option<PathBuf>,
    /// The format.
    #[arg(long, value_enum, default_value_t)]
    format: ExportFormat,
}

/// `mf2 import`.
#[derive(Debug, ClapArgs)]
pub(crate) struct ImportArgs {
    /// The locale to read into.
    #[arg(value_name = "LOCALE")]
    locale: String,
    /// The flat JSON or XLIFF 2 document to read (told apart by content).
    #[arg(value_name = "FILE")]
    pub(crate) file: PathBuf,
    /// Say what would change and write nothing.
    #[arg(long)]
    dry_run: bool,
    // What the result is checked with, as `mf2 check`: without `--features`,
    // the i18n crate's, as cargo resolves them.
    #[command(flatten)]
    features: FeatureArgs,
}

pub(crate) fn export(dir: &Path, args: &ExportArgs) -> Result<()> {
    let layout = Layout::new(dir);
    let path = locale_path(&layout, &args.locale)?;
    if args.format == ExportFormat::Xliff {
        let source = Config::load(dir)?.source_locale;
        if source == args.locale {
            return Err(Error::Usage(format!(
                "{source} is the source locale: an XLIFF document is a translation into another"
            )));
        }
        let source_path = locale_path(&layout, &source)?;
        let (text, count) = xliff::export(&source_path, &source, &path, &args.locale)?;
        match &args.out {
            Some(file) => {
                write(file, &text)?;
                eprintln!(
                    "mf2 export: {count} messages of {source} with {}'s translations to {}",
                    args.locale,
                    file.display()
                );
            }
            None => print!("{text}"),
        }
        return Ok(());
    }
    let loaded = mf2_build::loader::for_path(&path).load(&path)?;
    let text = json::write(
        loaded
            .records
            .iter()
            .map(|r| (r.id.as_str(), r.source.as_str())),
    );
    match &args.out {
        Some(file) => {
            write(file, &text)?;
            eprintln!(
                "mf2 export: {} messages of {} to {}",
                loaded.records.len(),
                args.locale,
                file.display()
            );
        }
        None => print!("{text}"),
    }
    Ok(())
}

pub(crate) fn import(dir: &Path, args: &ImportArgs) -> Result<()> {
    let layout = Layout::new(dir);
    let config = Config::load(dir)?;
    let text = read(&args.file)?;
    let verb = if args.dry_run {
        "would change"
    } else {
        "changed"
    };
    let plan = if xliff::xml::is_xliff(&text) {
        let source = &config.source_locale;
        let source_path = locale_path(&layout, source)?;
        let path = locale_path(&layout, &args.locale)?;
        let imported = xliff::import(
            &source_path,
            source,
            &path,
            &args.locale,
            &text,
            xliff::Checks::default(),
        )?;
        Plan {
            summary: if imported.refused.is_empty() {
                format!(
                    "mf2 import: {} message(s) {verb}, {} added, in {}",
                    imported.changed, imported.added, args.locale
                )
            } else {
                format!(
                    "mf2 import: {} unit(s) refused; {} message(s) {verb}",
                    imported.refused.len(),
                    imported.changed + imported.added
                )
            },
            rewrites: imported.rewrites,
            refused: imported.refused,
        }
    } else {
        json_plan(&layout, &config, args, &text, verb)?
    };

    for line in &plan.refused {
        eprintln!("{line}");
    }
    let findings = if plan.rewrites.is_empty() {
        Vec::new()
    } else {
        let features = crate::check::features(dir, &args.features);
        checked(&layout, &config, &features, &plan.rewrites)?
    };
    let mut report = String::new();
    for finding in &findings {
        finding.write_text(&mut report);
    }
    eprint!("{report}");
    let errors = findings.iter().filter(|d| d.level == Level::Error).count();
    if errors > 0 {
        eprintln!(
            "mf2 import: {errors} error(s) that {} does not have now; nothing {}",
            args.locale,
            if args.dry_run {
                "would be written"
            } else {
                "was written"
            }
        );
        return Err(Error::Corpus);
    }
    for rewrite in &plan.rewrites {
        if args.dry_run {
            println!("would rewrite {}", rewrite.path.display());
        } else {
            write(&rewrite.path, &rewrite.text)?;
        }
    }
    eprintln!("{}", plan.summary);
    if plan.refused.is_empty() {
        Ok(())
    } else {
        Err(Error::Corpus)
    }
}

/// A file an import would write, with its new text.
pub(crate) struct Rewrite {
    pub(crate) path: PathBuf,
    pub(crate) text: String,
}

/// What an import would do, before it is checked: the files it would
/// rewrite; what of the document it could not take, one line each (the
/// command then exits 1, after writing the rest); and the line that sums it
/// up.
struct Plan {
    rewrites: Vec<Rewrite>,
    refused: Vec<String>,
    summary: String,
}

/// A flat JSON document into the locale: new values for the ids it has.
fn json_plan(
    layout: &Layout,
    config: &Config,
    args: &ImportArgs,
    text: &str,
    verb: &str,
) -> Result<Plan> {
    let incoming: BTreeMap<String, String> = json::read(text)
        .map_err(|e| Error::Usage(format!("{}: {}", args.file.display(), e.message)))?
        .into_iter()
        .map(|pair| (pair.id, pair.source))
        .collect();

    let path = locale_path(layout, &args.locale)?;
    if path.is_file() {
        // A flat JSON locale: the file *is* the values.
        let text = json::write(
            incoming
                .iter()
                .map(|(id, source)| (id.as_str(), source.as_str())),
        );
        let now = read(&path)?;
        return Ok(Plan {
            summary: format!(
                "mf2 import: {} message(s) into {}",
                incoming.len(),
                path.display()
            ),
            rewrites: if text == now {
                Vec::new()
            } else {
                vec![Rewrite { path, text }]
            },
            refused: Vec::new(),
        });
    }

    // A resource locale: rewrite the values in place, leaving every section,
    // comment and property as it stands.
    let loaded = resource::Resources.load(&path)?;
    let known: BTreeSet<&str> = loaded.records.iter().map(|r| r.id.as_str()).collect();
    let mut changed = 0usize;
    let mut rewrites = Vec::new();
    for file in &loaded.files {
        let (resource, diagnostics) = parse(&file.text);
        if !diagnostics.is_empty() {
            return Err(Error::Usage(format!(
                "{}: has a syntax error; run `mf2 check` first",
                file.path.display()
            )));
        }
        let out = resource.map_values(|info, value| {
            let id = info.full_id().to_string();
            match incoming.get(&id) {
                Some(source) if *source != value => {
                    changed += 1;
                    std::borrow::Cow::Owned(source.clone())
                }
                _ => value,
            }
        });
        let text = serialize_with(&out, &resource::FMT_STYLE)
            .map_err(|e| Error::Usage(format!("{}: {e}", file.path.display())))?;
        if text != file.text {
            rewrites.push(Rewrite {
                path: file.path.clone(),
                text,
            });
        }
    }

    // An id the locale does not have needs a file and a section to go in,
    // which flat JSON does not say: it is refused, and XLIFF — which carries
    // where each message goes — is named as the way to add it. An id the
    // source does not have is no message at all.
    let source_path = locale_path(layout, &config.source_locale)?;
    let source = mf2_build::loader::for_path(&source_path).load(&source_path)?;
    let in_source: BTreeSet<&str> = source.records.iter().map(|r| r.id.as_str()).collect();
    let (new, strange): (Vec<&str>, Vec<&str>) = incoming
        .keys()
        .map(String::as_str)
        .filter(|id| !known.contains(id))
        .partition(|id| in_source.contains(id));
    let mut refused = Vec::new();
    if !new.is_empty() {
        refused.push(format!(
            "mf2 import: {} message(s) {} does not have yet were left out: {}; JSON import \
             changes the messages a language has, and XLIFF adds the others where the source \
             has them (`mf2 export {} --format xliff`)",
            new.len(),
            args.locale,
            first_ids(&new),
            args.locale
        ));
    }
    if !strange.is_empty() {
        refused.push(format!(
            "mf2 import: {} id(s) are not messages of {} and were left out: {}",
            strange.len(),
            config.source_locale,
            first_ids(&strange)
        ));
    }
    Ok(Plan {
        rewrites,
        refused,
        summary: format!("mf2 import: {changed} message(s) {verb} in {}", args.locale),
    })
}

/// How many ids a refusal names before it counts the rest.
const IDS_SHOWN: usize = 10;

/// The first [`IDS_SHOWN`] of `ids`, then how many more there are.
fn first_ids(ids: &[&str]) -> String {
    let shown = ids.len().min(IDS_SHOWN);
    let list = ids[..shown].join(", ");
    match ids.len() - shown {
        0 => list,
        more => format!("{list}, and {more} more"),
    }
}

/// Every check `mf2 check` makes, on the corpus as `rewrites` would leave it
/// — a copy of `locales/`, so that nothing is written — less what the corpus
/// reports as it stands. An import is refused for an error it brings, not
/// for one that is already there: in another language, or one it would
/// raise only because the features were not the build's.
fn checked(
    layout: &Layout,
    config: &Config,
    features: &Features,
    rewrites: &[Rewrite],
) -> Result<Vec<Diagnostic>> {
    let scratch = std::env::temp_dir().join(format!("mf2-import-{}", std::process::id()));
    let _ = std::fs::remove_dir_all(&scratch);
    let copy = scratch.join("locales");
    let result = check_copy(layout, config, features, rewrites, &scratch, &copy);
    let _ = std::fs::remove_dir_all(&scratch);
    result
}

fn check_copy(
    layout: &Layout,
    config: &Config,
    features: &Features,
    rewrites: &[Rewrite],
    scratch: &Path,
    copy: &Path,
) -> Result<Vec<Diagnostic>> {
    copy_dir(&layout.locales, copy)?;
    for rewrite in rewrites {
        let within = rewrite.path.strip_prefix(&layout.locales).map_err(|_| {
            Error::Usage(format!(
                "{}: not under {}",
                rewrite.path.display(),
                layout.locales.display()
            ))
        })?;
        write(&copy.join(within), &rewrite.text)?;
    }
    let check = |root: &Path| {
        Build::at(root, scratch.join("out"))
            .config(config.clone())
            .features(features.clone())
            // Nothing is written or compressed: only the report counts.
            .emit(Emit::Module)
            .check()
    };
    let before = check(&layout.root)?.report;
    let after = check(scratch)?.report;
    Ok(brought(&before, after, copy, &layout.locales))
}

/// What `after` reports that `before` does not. A finding is the same one
/// wherever the file moved it, so lines and columns are not compared; the
/// copy's paths are put back to the corpus's first.
fn brought(before: &Report, after: Report, copy: &Path, real: &Path) -> Vec<Diagnostic> {
    type Key = (
        Level,
        String,
        Option<String>,
        Option<Lint>,
        Option<&'static str>,
        String,
    );
    let key = |d: &Diagnostic| -> Key {
        (
            d.level,
            d.locale.clone(),
            d.id.clone(),
            d.lint,
            d.kind.map(mf2_model::ErrorKind::suite_name),
            d.message.clone(),
        )
    };
    let mut there: BTreeMap<Key, usize> = BTreeMap::new();
    for d in &before.diagnostics {
        *there.entry(key(d)).or_default() += 1;
    }
    let (copy_text, real_text) = (copy.display().to_string(), real.display().to_string());
    let mut out = Vec::new();
    for mut d in after.diagnostics {
        if let Ok(within) = d.file.strip_prefix(copy) {
            d.file = real.join(within);
        }
        d.message = d.message.replace(&copy_text, &real_text);
        match there.get_mut(&key(&d)) {
            Some(n) if *n > 0 => *n -= 1,
            _ => out.push(d),
        }
    }
    out
}

/// Copies the directory `from` to `to`.
fn copy_dir(from: &Path, to: &Path) -> Result<()> {
    std::fs::create_dir_all(to).map_err(|source| Error::io(to, source))?;
    let entries = std::fs::read_dir(from).map_err(|source| Error::io(from, source))?;
    for entry in entries {
        let entry = entry.map_err(|source| Error::io(from, source))?;
        let path = entry.path();
        let target = to.join(entry.file_name());
        if path.is_dir() {
            copy_dir(&path, &target)?;
        } else {
            std::fs::copy(&path, &target).map_err(|source| Error::io(&path, source))?;
        }
    }
    Ok(())
}

/// Where a locale lives: `locales/<tag>/` or `locales/<tag>.json`.
fn locale_path(layout: &Layout, tag: &str) -> Result<PathBuf> {
    let dir = layout.locales.join(tag);
    if dir.is_dir() {
        return Ok(dir);
    }
    let file = layout.locales.join(format!("{tag}.json"));
    if file.is_file() {
        return Ok(file);
    }
    Err(Error::Usage(format!(
        "{}: no locale {tag} (expected {} or {})",
        layout.locales.display(),
        dir.display(),
        file.display()
    )))
}