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
use std::error::Error as ErrorImpl;

use crate::printer::arg_string;

pub enum Error {
    InvalidState(&'static str),
    InvalidInput(char, &'static str, &'static str),
    MissingArgValue(char, &'static str),
    ConstructionError(char, &'static str, String), // TODO: would be nice to keep the typed-error
    PositionalConstructionError(&'static str, String), // TODO: would be nice to keep the original
    SubConstructionError(&'static str, String), // TODO: would be nice to keep the typed-error
    ValuedArgInRun(char, String), // offending short, run it was contained in

    NestedGroup(&'static str, &'static str), // existing, attempted
    PrinterMissingGroup(&'static str),

    MissingArgument(String),
    MissingPositional(String),
    MultipleVariadic(&'static str),
    UnorderedPositionals(&'static str),
}

impl std::error::Error for Error {
    fn description(&self) -> &str {
        match self {
            Error::InvalidState(_) => {
                "invalid parser state"
            }
            Error::InvalidInput(_, _, _) => {
                "invalid input"
            }
            Error::MissingArgValue(_, _) => {
                "missing argument value"
            }
            Error::ConstructionError(_, _, _) => {
                "failed to construct target from string"
            }
            Error::PositionalConstructionError(_, _) => {
                "failed to construct positional target from string"
            }
            Error::SubConstructionError(_, _) => {
                "failed to construct subcommand from string"
            }
            Error::ValuedArgInRun(_, _) => {
                "short-code runs only support valued-args as the last character in the run"
            }

            Error::NestedGroup(_, _) => {
                "groups cannot be nested"
            }
            Error::PrinterMissingGroup(_) => {
                "cannot add option to unknown group"
            }


            Error::MissingArgument(_) => {
                "required argument was not given"
            }
            Error::MissingPositional(_) => {
                "required positional was not given"
            }
            Error::MultipleVariadic(_) => {
                "second declared variadic positional has no effect"
            }
            Error::UnorderedPositionals(_) => {
                "declaring a positional after a variadic positional has no effect"
            }
        }
    }
}

impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Error::InvalidState(desc) => {
                write!(f, "{}: {}", self.description(), desc)
            }
            Error::InvalidInput(short, long, desc) => {
                write!(f, "{}: {} {}", self.description(), arg_string(*short, long, false), desc)
            }
            Error::MissingArgValue(short, long) => {
                write!(f, "{} for {}", self.description(), arg_string(*short, long, false))
            }
            Error::ConstructionError(short, long, err) => {
                write!(f, "{} for {}: {}", self.description(),
                    arg_string(*short, long, false), err)
            }
            Error::PositionalConstructionError(name, err) => {
                write!(f, "{} for {}: {}", self.description(), name, err)
            }
            Error::SubConstructionError(name, err) => {
                write!(f, "{} for {}: {}", self.description(), name, err)
            }
            Error::ValuedArgInRun(short, run) => {
                write!(f, "{}: {} is within {}", self.description(), short, run)
            }


            Error::NestedGroup(orig, attempt) => {
                write!(f, "{} ({} within {})", self.description(), attempt, orig)
            }
            Error::PrinterMissingGroup(name) => {
                write!(f, "{}: {}", self.description(), name)
            }

            Error::MissingArgument(a) => {
                write!(f, "{}: {}", self.description(), a)
            }
            Error::MissingPositional(a) => {
                write!(f, "{}: {}", self.description(), a)
            }
            Error::MultipleVariadic(p) => {
                write!(f, "{}: {}", self.description(), p)
            }
            Error::UnorderedPositionals(p) => {
                write!(f, "{}: {}", self.description(), p)
            }
        }
    }
}

impl std::fmt::Debug for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self)
    }
}