witforge-cli 0.1.2

Command-line interface for witforge.
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! The `witforge` command-line interface.
//!
//! ```text
//! witforge check <wit-dir> [--lock PATH] [--first | --against PATH] [--released PATH]
//! witforge lock  <wit-dir> [--lock PATH] [--first | --against PATH] [--released PATH]
//! witforge rust  <wit-root> --out DIR [--check] [--released PATH]
//! witforge proto <wit-root> --out DIR [--check] [--released PATH]
//! witforge glue  <wit-root> --out DIR --plain-root PATH --proto-root PATH [--check] [--released PATH]
//! ```

use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;

use anyhow::{Context, Result, bail};
use clap::{Args, Parser, Subcommand};

use witforge::emit;
use witforge::lock::{LockMode, PackageName, WireLock};
use witforge::{
    ContractError, ensure_directory_version, ensure_lockfile_not_released, ensure_not_released,
    ensure_version_directory, is_contract_error, is_released, load, load_released_manifest,
    load_root_released_manifest, model, subset,
};

/// The witforge contract compiler.
#[derive(Debug, Parser)]
#[command(name = "witforge", version, about)]
struct Cli {
    /// Command to run.
    #[command(subcommand)]
    command: Command,
}

#[derive(Debug, Subcommand)]
enum Command {
    /// Check the subset and the wire lockfile without writing.
    Check(Options),
    /// Assign wire numbers to new members and reserve removed ones.
    Lock(Options),
    /// Generate plain Rust types and interface traits.
    Rust(RustOptions),
    /// Generate protobuf schemas.
    Proto(ProtoOptions),
    /// Generate protobuf glue conversions and function helpers.
    Glue(GlueOptions),
}

#[derive(Debug, Args)]
struct RustOptions {
    /// WIT root containing version directories.
    #[arg(value_name = "WIT_ROOT")]
    root: PathBuf,
    /// Output root for generated Rust files.
    #[arg(long, value_name = "DIR")]
    out: PathBuf,
    /// Check generated output without writing.
    #[arg(long)]
    check: bool,
    /// Released-version manifest to validate; defaults to `<WIT_ROOT>/released`.
    #[arg(long, value_name = "PATH")]
    released: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct ProtoOptions {
    /// WIT root containing version directories.
    #[arg(value_name = "WIT_ROOT")]
    root: PathBuf,
    /// Output root for generated proto files.
    #[arg(long, value_name = "DIR")]
    out: PathBuf,
    /// Check generated output without writing.
    #[arg(long)]
    check: bool,
    /// Released-version manifest to validate; defaults to `<WIT_ROOT>/released`.
    #[arg(long, value_name = "PATH")]
    released: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct GlueOptions {
    /// WIT root containing version directories.
    #[arg(value_name = "WIT_ROOT")]
    root: PathBuf,
    /// Output root for generated glue files.
    #[arg(long, value_name = "DIR")]
    out: PathBuf,
    /// Rust module containing the generated plain version modules.
    #[arg(long, value_name = "PATH")]
    plain_root: String,
    /// Rust module containing the generated proto version modules.
    #[arg(long, value_name = "PATH")]
    proto_root: String,
    /// Check generated output without writing.
    #[arg(long)]
    check: bool,
    /// Released-version manifest to validate; defaults to `<WIT_ROOT>/released`.
    #[arg(long, value_name = "PATH")]
    released: Option<PathBuf>,
}

#[derive(Debug, Args)]
struct Options {
    /// WIT version directory.
    #[arg(value_name = "WIT_DIR")]
    dir: PathBuf,
    /// Lockfile path; defaults to `<WIT_DIR>/wire.lock`.
    #[arg(long, value_name = "PATH")]
    lock: Option<PathBuf>,
    /// Treat entries from this previous lockfile as released.
    #[arg(long, value_name = "PATH", conflicts_with = "first")]
    against: Option<PathBuf>,
    /// Treat every existing entry as a draft; for a version with no baseline.
    #[arg(long, conflicts_with = "against")]
    first: bool,
    /// Released-version manifest (`lock` refuses a listed directory); defaults to
    /// `<WIT_DIR>/../released`.
    #[arg(long, value_name = "PATH")]
    released: Option<PathBuf>,
}

/// Exit codes: `0` success; `1` a contract, lockfile, or generated-output violation; `2` a
/// usage, I/O, parse, released-directory, or internal error.
fn main() -> ExitCode {
    let cli = Cli::parse();
    match run(cli) {
        Ok(code) => code,
        Err(err) => {
            eprintln!("witforge: {err:#}");
            ExitCode::from(if is_contract_error(&err) { 1 } else { 2 })
        }
    }
}

fn run(cli: Cli) -> Result<ExitCode> {
    match cli.command {
        Command::Check(options) => check(&options),
        Command::Lock(options) => lock(&options),
        Command::Rust(options) => rust(&options),
        Command::Proto(options) => proto(&options),
        Command::Glue(options) => glue(&options),
    }
}

fn check(options: &Options) -> Result<ExitCode> {
    ensure_version_directory(&options.dir)?;
    // `check` may verify a released directory; the manifest only has to be valid.
    load_released_manifest(&options.dir, options.released.as_deref())?;
    let contract = load(&options.dir)?;
    let violations = subset::check(&contract);
    if !violations.is_empty() {
        // The model of a contract outside the subset is not built, so every locked member
        // would look removed; report only the violations, as `lock` does.
        for violation in &violations {
            eprintln!("error: {violation}");
        }
        return Ok(ExitCode::from(1));
    }
    let model = model::build(&contract)?;
    ensure_directory_version(&options.dir, &model)?;
    let members = model.members()?;
    let lock_path = options
        .lock
        .clone()
        .unwrap_or_else(|| options.dir.join("wire.lock"));
    let (lock, text) = read_lock(&lock_path, options)?;
    let previous = read_previous(options.against.as_deref())?;
    let released = is_released(&options.dir, options.released.as_deref())?;
    let remedy = if released {
        Remedy::Released(options.dir.display().to_string())
    } else {
        Remedy::Lock(lock_command(options))
    };
    let package = package_name(&model);
    for lockfile in std::iter::once(&lock).chain(previous.as_ref()) {
        if let Err(err) = lockfile.check_package(&package) {
            eprintln!("error: {err:#}");
            return Ok(ExitCode::from(1));
        }
    }
    let mode = lock_mode(options, previous.as_ref());
    let drift = match lock.drift_with(&members, mode) {
        Ok(changes) => changes,
        Err(err) => {
            eprintln!("error: {err:#}");
            return Ok(ExitCode::from(1));
        }
    };
    // A lockfile written by hand, or edited by a tool that reorders or reformats it, is drift
    // too: `lock` would rewrite it, and only its canonical form is reviewed. CRLF line endings
    // are read as LF, since a checkout with `core.autocrlf` writes them. A released lockfile is
    // exempt: `lock` refuses to rewrite it, and the freeze check guards it as it was released.
    let canonical = released
        || text
            .as_deref()
            .is_none_or(|text| lock.is_canonical(&text.replace("\r\n", "\n")));
    if !canonical {
        eprintln!(
            "error: lockfile {} is not in the form `witforge lock` writes; {}",
            lock_path.display(),
            remedy.advice(" to rewrite it")
        );
    }
    report_check_changes(&drift, &remedy);
    if drift.is_empty() && canonical {
        Ok(ExitCode::SUCCESS)
    } else {
        Ok(ExitCode::from(1))
    }
}

fn lock(options: &Options) -> Result<ExitCode> {
    ensure_not_released(&options.dir, options.released.as_deref())?;
    let contract = load(&options.dir)?;
    let violations = subset::check(&contract);
    for violation in &violations {
        eprintln!("error: {violation}");
    }
    if !violations.is_empty() {
        bail!(ContractError::new(format!(
            "{} subset violation(s); the lockfile was not updated",
            violations.len()
        )));
    }
    let model = model::build(&contract)?;
    ensure_directory_version(&options.dir, &model)?;
    let members = model.members()?;
    let lock_path = options
        .lock
        .clone()
        .unwrap_or_else(|| options.dir.join("wire.lock"));
    ensure_lockfile_not_released(&lock_path, &options.dir, options.released.as_deref())?;
    let (mut lock, _) = read_lock(&lock_path, options)?;
    let previous = read_previous(options.against.as_deref())?;
    let package = package_name(&model);
    for lockfile in std::iter::once(&lock).chain(previous.as_ref()) {
        lockfile
            .check_package(&package)
            .map_err(|err| ContractError::from_error(&err))?;
    }
    let mode = lock_mode(options, previous.as_ref());
    // Every refusal from reconciling is a lockfile violation, as `check` reports it.
    let changes = lock
        .reconcile_with(&members, mode)
        .map_err(|err| ContractError::from_error(&err))?;
    if !changes.reintroduced.is_empty() {
        for (key, number) in &changes.reintroduced {
            eprintln!("error: {}", reintroduced(key, *number));
        }
        bail!(ContractError::new(format!(
            "{} removed member(s) reintroduced; the lockfile was not updated",
            changes.reintroduced.len()
        )));
    }
    for (key, number) in &changes.dropped {
        println!("dropped  {key} = {number}");
    }
    for (key, _kind, _ty) in &changes.updated {
        println!("updated  {key}");
    }
    for (key, number) in &changes.reserved {
        println!("reserved {key} = {number}");
    }
    for (key, number) in &changes.added {
        println!("added    {key} = {number}");
    }
    lock.write_atomic(&lock_path)?;
    Ok(ExitCode::SUCCESS)
}

fn rust(options: &RustOptions) -> Result<ExitCode> {
    // Generation reproduces released versions without modifying them, so the manifest only has
    // to be valid. The emitter validates every version and lockfile before writing.
    load_root_released_manifest(&options.root, options.released.as_deref())?;
    if options.check {
        let messages = emit::rust::check(&options.root, &options.out)?;
        for message in &messages {
            eprintln!("error: {message}");
        }
        return if messages.is_empty() {
            Ok(ExitCode::SUCCESS)
        } else {
            Ok(ExitCode::from(1))
        };
    }
    emit::rust::generate(&options.root, &options.out)?;
    Ok(ExitCode::SUCCESS)
}

fn proto(options: &ProtoOptions) -> Result<ExitCode> {
    load_root_released_manifest(&options.root, options.released.as_deref())?;
    if options.check {
        let messages = emit::proto::check(&options.root, &options.out)?;
        for message in &messages {
            eprintln!("error: {message}");
        }
        return if messages.is_empty() {
            Ok(ExitCode::SUCCESS)
        } else {
            Ok(ExitCode::from(1))
        };
    }
    emit::proto::generate(&options.root, &options.out)?;
    Ok(ExitCode::SUCCESS)
}

fn glue(options: &GlueOptions) -> Result<ExitCode> {
    // Glue is current-schema output for the highest minor of each major; released plain modules
    // stay frozen. As for the other generators, the manifest only has to be valid.
    load_root_released_manifest(&options.root, options.released.as_deref())?;
    if options.check {
        let messages = emit::glue::check(
            &options.root,
            &options.out,
            &options.plain_root,
            &options.proto_root,
        )?;
        for message in &messages {
            eprintln!("error: {message}");
        }
        return if messages.is_empty() {
            Ok(ExitCode::SUCCESS)
        } else {
            Ok(ExitCode::from(1))
        };
    }
    emit::glue::generate(
        &options.root,
        &options.out,
        &options.plain_root,
        &options.proto_root,
    )?;
    Ok(ExitCode::SUCCESS)
}

fn lock_mode<'a>(options: &Options, previous: Option<&'a WireLock>) -> LockMode<'a> {
    if let Some(previous) = previous {
        LockMode::Against(previous)
    } else if options.first {
        LockMode::First
    } else {
        LockMode::Default
    }
}

fn read_previous(path: Option<&Path>) -> Result<Option<WireLock>> {
    match path {
        Some(path) => read_required_lock(path).map(Some),
        None => Ok(None),
    }
}

fn package_name(model: &model::Model) -> PackageName {
    PackageName {
        namespace: model.package.namespace.clone(),
        name: model.package.name.clone(),
    }
}

/// Read the current lockfile and its text. Only `--first` treats a missing one as empty: in
/// default mode a fresh lockfile would renumber a new minor that should continue the previous
/// one, and `--against` develops a copy of the previous lockfile.
fn read_lock(path: &Path, options: &Options) -> Result<(WireLock, Option<String>)> {
    match fs::read_to_string(path) {
        Ok(text) => {
            let lock = WireLock::parse(&text).with_context(|| format!("in {}", path.display()))?;
            Ok((lock, Some(text)))
        }
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => {
            if options.first {
                return Ok((WireLock::default(), None));
            }
            if let Some(previous) = &options.against {
                bail!(
                    "lockfile {} does not exist; a new minor starts as a copy of the previous \
                     minor's lockfile: copy {} to {} first, then rerun with `--against`",
                    path.display(),
                    previous.display(),
                    path.display()
                );
            }
            bail!(
                "lockfile {path} does not exist; for the first version of a package or of a new \
                 major, create it with `{first}`; for a new minor, copy the previous minor's \
                 wire.lock to {path} and run `{against}`",
                path = path.display(),
                first = lock_command_with(options, Some("--first")),
                against =
                    lock_command_with(options, Some("--against <previous minor's wire.lock>")),
            );
        }
        Err(err) => Err(err).with_context(|| format!("failed to read {}", path.display())),
    }
}

fn read_required_lock(path: &Path) -> Result<WireLock> {
    let text =
        fs::read_to_string(path).with_context(|| format!("failed to read {}", path.display()))?;
    WireLock::parse(&text).with_context(|| format!("in {}", path.display()))
}

/// How to fix what `check` reports: run `lock`, or, for a released version, which `lock`
/// refuses, restore what was released.
enum Remedy {
    /// The `witforge lock` command, with the same mode and paths.
    Lock(String),
    /// The released version directory.
    Released(String),
}

impl Remedy {
    fn advice(&self, purpose: &str) -> String {
        match self {
            Self::Lock(command) => format!("run `{command}`{purpose}"),
            Self::Released(dir) => format!(
                "`{dir}` is released, so `witforge lock` refuses it; restore its released WIT \
                 and wire.lock"
            ),
        }
    }
}

fn report_check_changes(changes: &witforge::lock::Changes, remedy: &Remedy) {
    for (key, _) in &changes.added {
        eprintln!("error: {key} has no wire number; {}", remedy.advice(""));
    }
    for (key, number) in &changes.reserved {
        eprintln!(
            "error: {key} = {number} is no longer in the contract; {}",
            remedy.advice(" to reserve it")
        );
    }
    for (key, number) in &changes.reintroduced {
        eprintln!("error: {}", reintroduced(key, *number));
    }
    for (key, number) in &changes.dropped {
        eprintln!(
            "error: stale draft {key} = {number}; {}",
            remedy.advice(" to drop it")
        );
    }
    for (key, _kind, _ty) in &changes.updated {
        eprintln!(
            "error: draft {key} changed kind or type; {}",
            remedy.advice(" to update it")
        );
    }
}

/// The `witforge lock` command that fixes what `check` reports, with the same mode and paths,
/// each quoted for a POSIX shell.
fn lock_command(options: &Options) -> String {
    let mode = if options.first {
        Some("--first".to_string())
    } else {
        options
            .against
            .as_ref()
            .map(|previous| format!("--against {}", shell_quote(previous)))
    };
    lock_command_with(options, mode.as_deref())
}

/// The `witforge lock` command for `options`' version directory, lockfile, and manifest, with
/// `mode` (such as `--first`) in place of the options' own, each path quoted for a POSIX shell.
fn lock_command_with(options: &Options, mode: Option<&str>) -> String {
    let mut command = format!("witforge lock {}", shell_quote(&options.dir));
    if let Some(lock) = &options.lock {
        let _ = write!(command, " --lock {}", shell_quote(lock));
    }
    if let Some(mode) = mode {
        let _ = write!(command, " {mode}");
    }
    if let Some(released) = &options.released {
        let _ = write!(command, " --released {}", shell_quote(released));
    }
    command
}

/// `path` as one POSIX shell word that witforge reads as a path: unchanged when it holds only
/// characters no shell treats specially, and single-quoted otherwise. A path that starts with
/// `-` is relative, and gets a leading `./`, so the command line does not read it as an option.
fn shell_quote(path: &Path) -> String {
    let mut text = path.to_string_lossy().into_owned();
    if text.starts_with('-') {
        text.insert_str(0, "./");
    }
    let plain = !text.is_empty()
        && text
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || b"%+,-./:=@_".contains(&byte));
    if plain {
        text
    } else {
        format!("'{}'", text.replace('\'', "'\\''"))
    }
}

fn reintroduced(key: &str, number: u32) -> String {
    format!(
        "{key} was removed and its name and number {number} stay reserved; give the member a new name"
    )
}