Skip to main content

alux_cli/
argument.rs

1/// States how many occurrences an argument takes, as its source's shape says.
2#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3pub enum Arity {
4    /// Exactly one value.
5    One,
6    /// At most one value: `Option` of it.
7    Optional,
8    /// Any number of values, in order: `Vec` of them.
9    Many,
10    /// Whether it is on: `bool`. Given bare it is on; a long spelling may state a truth either way,
11    /// `--dev=false`, except as an alternative of a sum, which is chosen by its spelling alone.
12    Presence,
13    /// A value written only in the option's own word, or none: `-i`, `-i.bak`, `--in-place=.bak`.
14    /// `Option<Option<_>>`: whether it occurs, and the value it was given.
15    OptionalValue,
16}
17
18/// Decodes one word as a value; an interpreter answers it for the values it supports.
19pub trait CliWordAlg<Value, Word: ?Sized> {
20    /// Why a word does not denote a value.
21    type Error;
22
23    /// Decodes `word` as a value.
24    ///
25    /// # Errors
26    ///
27    /// Answers with the interpreter's error when `word` does not denote a value.
28    fn cli_word(&self, word: &Word) -> Result<Value, Self::Error>;
29}
30
31/// States why the words given for an argument do not make its value.
32#[derive(Clone, Debug, PartialEq, Eq)]
33pub enum CliArgumentError<Error> {
34    /// An argument of arity one was not given.
35    Missing(&'static str),
36    /// An argument taking at most one value was given more than once.
37    Repeated(&'static str),
38    /// A word did not decode as the argument's value.
39    Word(&'static str, Error),
40    /// Two alternatives of a sum were given together.
41    Conflict(&'static str, &'static str),
42}