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
//! Error types used by the crate.

use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
    num::NonZeroU64,
};

use twilight_model::application::command::CommandOptionType;

/// Error when parsing a command option.
///
/// This error type is returned by the [`CommandModel::from_interaction`] method.
///
/// [`CommandModel::from_interaction`]: crate::command::CommandModel::from_interaction
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    /// The name of the option field that caused the error.
    pub field: String,
    /// The type of the error.
    pub kind: ParseErrorType,
}

impl Error for ParseError {}

impl Display for ParseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(f, "failed to parse option `{}`: ", self.field)?;

        match &self.kind {
            ParseErrorType::InvalidType(ty) => write!(f, "invalid type, found {}", ty.kind()),
            ParseErrorType::LookupFailed(id) => write!(f, "failed to resolve `{}`", id),
            ParseErrorType::UnknownField => write!(f, "unknown field"),
            ParseErrorType::RequiredField => write!(f, "missing required field"),
        }
    }
}

/// Type of [`ParseError`] that occurred.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ParseErrorType {
    /// Found an invalid option type.
    InvalidType(CommandOptionType),
    /// Failed to resolve data associated with an ID.
    LookupFailed(NonZeroU64),
    /// Missing a required option field.
    RequiredField,
    /// Received an unknown option field.
    UnknownField,
}