@@ -89,6 +89,60 @@ pub enum ErrorKind {
89
89
#[ derive( Clone , Copy , Default , PartialEq , Eq , Hash , PartialOrd , Ord ) ]
90
90
pub struct Empty ;
91
91
92
+ /// An error that occurs when a value doesn't match one of the expected options.
93
+ ///
94
+ /// This error is returned by the [`FromParam`] trait implementation generated
95
+ /// by the [`FromParam` derive](macro@rocket::FromParam) when the value of a
96
+ /// dynamic path segment does not match one of the expected variants. The
97
+ /// `value` field will contain the value that was provided, and `options` will
98
+ /// contain each of possible stringified variants.
99
+ ///
100
+ /// [`FromParam`]: trait@rocket::request::FromParam
101
+ ///
102
+ /// # Example
103
+ ///
104
+ /// ```rust
105
+ /// # #[macro_use] extern crate rocket;
106
+ /// use rocket::error::InvalidOption;
107
+ ///
108
+ /// #[derive(FromParam)]
109
+ /// enum MyParam {
110
+ /// FirstOption,
111
+ /// SecondOption,
112
+ /// ThirdOption,
113
+ /// }
114
+ ///
115
+ /// #[get("/<param>")]
116
+ /// fn hello(param: Result<MyParam, InvalidOption<'_>>) {
117
+ /// if let Err(e) = param {
118
+ /// assert_eq!(e.options, &["FirstOption", "SecondOption", "ThirdOption"]);
119
+ /// }
120
+ /// }
121
+ /// ```
122
+ #[ derive( Debug , Clone ) ]
123
+ #[ non_exhaustive]
124
+ pub struct InvalidOption < ' a > {
125
+ /// The value that was provided.
126
+ pub value : & ' a str ,
127
+ /// The expected values: a slice of strings, one for each possible value.
128
+ pub options : & ' static [ & ' static str ] ,
129
+ }
130
+
131
+ impl < ' a > InvalidOption < ' a > {
132
+ #[ doc( hidden) ]
133
+ pub fn new ( value : & ' a str , options : & ' static [ & ' static str ] ) -> Self {
134
+ Self { value, options }
135
+ }
136
+ }
137
+
138
+ impl fmt:: Display for InvalidOption < ' _ > {
139
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
140
+ write ! ( f, "unexpected value {:?}, expected one of {:?}" , self . value, self . options)
141
+ }
142
+ }
143
+
144
+ impl std:: error:: Error for InvalidOption < ' _ > { }
145
+
92
146
impl Error {
93
147
#[ inline( always) ]
94
148
pub ( crate ) fn new ( kind : ErrorKind ) -> Error {
0 commit comments