Skip to main content

starmap_cli/
lib.rs

1//! The `starmap` command, which writes, checks, explains and runs an
2//! application's declared environment.
3//!
4//! The library holds the whole command, and the `starmap` binary calls
5//! `run`. It exports the parser, the guide and the failure codes, so the
6//! integration tests can pass them to crumbkit-test's checks beside the
7//! built binary. It is not an API for other crates, so each public item is
8//! hidden from the documentation and outside the semver promise.
9
10mod check;
11mod cli;
12#[doc(hidden)]
13pub mod codes;
14mod diff;
15mod dotenv;
16mod explain;
17mod files;
18mod git;
19mod init;
20mod mode;
21mod run;
22mod scan;
23mod spec;
24
25use std::ffi::OsString;
26use std::fmt;
27use std::process::ExitCode;
28
29use clap::CommandFactory as _;
30use crumbkit::Failure;
31
32#[doc(hidden)]
33pub use crate::cli::{Cli, GUIDE};
34use crate::cli::{CommandsCommand, SchemasCommand, SelfCommand, Verb};
35use crate::diff::RevisionFailure;
36use crate::scan::SourceFailure;
37use crate::spec::SpecFailure;
38
39/// Parses `args`, runs the command and returns the process's exit code. The
40/// first argument is the program's name. `vars` is the process environment,
41/// which `main` reads once (BASE-6), and `check`, `explain` and `run` read
42/// the declared variables from it.
43///
44/// `crumbkit::parse_from` prints help or a refused command line and returns
45/// the exit code. `crumbkit::finish` prints the result or the failure in
46/// the format that the output flags choose, and returns the exit code.
47#[doc(hidden)]
48#[must_use]
49pub fn run<I, T>(args: I, vars: Vec<(OsString, OsString)>) -> ExitCode
50where
51    I: IntoIterator<Item = T>,
52    T: Into<OsString> + Clone,
53{
54    let (cli, mut output) = match crumbkit::parse_from::<Cli, _, _>(Cli::command(), &GUIDE, args) {
55        Ok(parsed) => parsed,
56        Err(stop) => return stop.exit_code(),
57    };
58    match cli.verb {
59        Verb::Init(args) => {
60            let dry_run = output.is_dry_run();
61            crumbkit::finish(output, init::INIT, init::init(&args, dry_run))
62        }
63        Verb::Check(args) => crumbkit::finish(output, check::CHECK, check::check(&args, vars)),
64        Verb::Explain(args) => {
65            crumbkit::finish(output, explain::EXPLAIN, explain::explain(&args, vars))
66        }
67        Verb::Diff(args) => crumbkit::finish(output, diff::DIFF, diff::diff(&args)),
68        Verb::Run(args) => {
69            let no_input = cli.no_input != crumbkit::NoInput::default();
70            #[expect(
71                clippy::disallowed_methods,
72                reason = "run prints its notes on standard error before the child takes the terminal, after which nothing of starmap prints"
73            )]
74            let mut notes = std::io::stderr();
75            run::run(&args, vars, output, no_input, &mut notes)
76        }
77        // `commands list`, `schemas` and `self usage` read the command tree through
78        // `&mut output`, so each runs before `finish` takes `output`.
79        Verb::Commands {
80            command: CommandsCommand::List(args),
81        } => {
82            let result = crumbkit::commands::list(&mut output, &GUIDE, &args);
83            crumbkit::finish(output, crumbkit::commands::COMMANDS_LIST, result)
84        }
85        Verb::Schemas {
86            command: SchemasCommand::List(args),
87        } => {
88            let result = crumbkit::schemas::list(&mut output, &GUIDE, &args);
89            crumbkit::finish(output, crumbkit::schemas::SCHEMAS_LIST, result)
90        }
91        Verb::Schemas {
92            command: SchemasCommand::Show(args),
93        } => {
94            let result = crumbkit::schemas::show(&mut output, &GUIDE, &args);
95            crumbkit::finish(output, crumbkit::schemas::SCHEMAS_SHOW, result)
96        }
97        Verb::SelfCommand {
98            command: SelfCommand::Usage(args),
99        } => {
100            let result =
101                Ok::<_, crumbkit::Error>(crumbkit_usage::findings(&mut output, &GUIDE, &args));
102            crumbkit::finish(output, crumbkit_usage::SELF_USAGE, result)
103        }
104    }
105}
106
107/// A failure of `init`, `check` or `diff`, which read or write the spec,
108/// scan the source or read a git revision.
109#[derive(Debug, Failure)]
110pub(crate) enum Failed {
111    /// The spec cannot be read, parsed or written.
112    #[failure(forward, diagnostic)]
113    Spec(SpecFailure),
114    /// A file under src cannot be read.
115    #[failure(forward)]
116    Source(SourceFailure),
117    /// git cannot read the revision that `diff` names.
118    #[failure(forward)]
119    Revision(RevisionFailure),
120}
121
122impl fmt::Display for Failed {
123    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
124        match self {
125            Self::Spec(failure) => failure.fmt(formatter),
126            Self::Source(failure) => failure.fmt(formatter),
127            Self::Revision(failure) => failure.fmt(formatter),
128        }
129    }
130}
131
132impl std::error::Error for Failed {
133    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
134        match self {
135            Self::Spec(failure) => failure.source(),
136            Self::Source(failure) => failure.source(),
137            Self::Revision(failure) => failure.source(),
138        }
139    }
140}
141
142impl From<SpecFailure> for Failed {
143    fn from(failure: SpecFailure) -> Self {
144        Self::Spec(failure)
145    }
146}
147
148impl From<SourceFailure> for Failed {
149    fn from(failure: SourceFailure) -> Self {
150        Self::Source(failure)
151    }
152}
153
154impl From<RevisionFailure> for Failed {
155    fn from(failure: RevisionFailure) -> Self {
156        Self::Revision(failure)
157    }
158}