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
//! Module defining the error types for this crate.
#[cfg(doc)]
use crate::core::Command;
#[cfg(doc)]
use crate::generate::Config;
#[derive(Debug, Eq, PartialEq)]
#[non_exhaustive]
/// The error type for CLI completion, raised by [`Command::supplement`]
pub enum Error {
/// When asking for a flag where there is none.
/// e.g. `cd --<TAB>`, because there's no flag for `cd` at all
/// (if we ignore the --help flag).
UnexpectedFlag,
/// When a boolean flag got an unexpected `=some_value` part, e.g. `git add -A=value`.
BoolFlagEqualsValue(String),
/// When a flag is supposed to have value, but got none. e.g. `git commit -m --amend`.
/// If you're trying to supply it with a flag-like value, please do `git commit -m=--amend`.
FlagNoValue(String),
/// When a flag in CLI command is not found, e.g. `git --xx=<TAB>`.
/// Or `git --xx lo<TAB>`, though technically the `<TAB>` is not asking for that flag.
/// `supplement` still needs to know the information about the flag to decide if the
/// `lo<TAB>` is asking for a subcommand/arg or the flag's value.
FlagNotFound(String),
/// Attempt to complete an arg/subcommand, when there is none.
/// e.g. `cargo build <TAB>`, becuase there's no more arg or subcommand.
/// Or `cargo build abc -<TAB>`, though technically the `<TAB>` is asking for a flag,
/// but there is this unexpected arg `abc` in front of it.
UnexpectedArg(String),
/// There should be at least 2 args, e.g. `git chec` or `git ''`.
/// NOTE that the empty string is needed if that position is where user want the completion.
/// i.e. `git ''` means `git <TAB>`, which will result in all git's subcommands.
ArgsTooShort,
/// When a flags requires equal but there is none.
RequiresEqual(String),
}
#[cfg(any(feature = "clap-3", feature = "clap-4"))]
#[derive(Debug)]
#[non_exhaustive]
/// Error for code-gen, raised by [`crate::generate`]
pub enum GenerateError {
/// Error when the clap definition has some potential problem.
/// To suppress this, use [`Config::strict`].
/// ```no_run
/// # #[cfg(feature = "clap-3")]
/// # use clap3 as clap;
/// # #[cfg(feature = "clap-4")]
/// # use clap4 as clap;
/// # let cmd: clap::Command = unimplemented!();
///
/// use std::io::stdout;
/// use supplement::generate::{Config, generate};
///
/// // this may raise `Strict` error
/// generate(&mut cmd, Config::new(), &mut stdout());
/// // this will not raise `Strict` error
/// generate(&mut cmd, Config::new().strict(false), &mut stdout());
/// ```
Strict {
msg: &'static str,
id: String,
},
/// See document for [`Config::ignore`] and similar methods.
UnprocessedConfigObj(Vec<Vec<String>>),
/// See document for [`Config::make_custom`].
AlreadyCustom(String),
/// See document for [`Config::make_custom`].
CustomWithoutValue(String),
IO(std::io::Error),
}
#[cfg(any(feature = "clap-3", feature = "clap-4"))]
impl From<std::io::Error> for GenerateError {
fn from(value: std::io::Error) -> Self {
GenerateError::IO(value)
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// TODO: better display?
write!(f, "{:?}", self)
}
}
impl std::error::Error for Error {}