@@ -208,16 +208,32 @@ impl fmt::Display for OnTheFlyLimit {
208208 }
209209 }
210210}
211+
212+ /// An error which can be returned when parsing an on-the-fly limit.
213+ #[ derive( Debug ) ]
214+ pub struct ParseOnTheFlyLimitError {
215+ msg : String ,
216+ kind : ErrorKind ,
217+ }
218+ impl ParseOnTheFlyLimitError {
219+ fn new ( msg : String , kind : ErrorKind ) -> Self {
220+ Self { msg, kind }
221+ }
222+ fn to_clap_error ( & self ) -> Error {
223+ Error :: with_description ( self . msg . clone ( ) , self . kind )
224+ }
225+ }
226+ impl fmt:: Display for ParseOnTheFlyLimitError {
227+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> fmt:: Result {
228+ fmt:: Display :: fmt ( & self . to_clap_error ( ) , f)
229+ }
230+ }
231+ impl std:: error:: Error for ParseOnTheFlyLimitError { }
232+
211233impl FromStr for OnTheFlyLimit {
212- type Err = Error ;
234+ type Err = ParseOnTheFlyLimitError ;
213235
214236 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
215- if s. is_empty ( ) {
216- return Err ( Error :: with_description (
217- "" . to_string ( ) ,
218- ErrorKind :: EmptyValue ,
219- ) ) ;
220- }
221237 // parse longest prefix until a number is encountered
222238 let split = s
223239 . char_indices ( )
@@ -229,7 +245,7 @@ impl FromStr for OnTheFlyLimit {
229245 if number. is_empty ( ) {
230246 Ok ( Self :: None )
231247 } else {
232- Err ( Error :: with_description (
248+ Err ( ParseOnTheFlyLimitError :: new (
233249 format ! (
234250 "invalid number '{}' for value 'none' [must be empty]" ,
235251 number
@@ -238,28 +254,28 @@ impl FromStr for OnTheFlyLimit {
238254 ) )
239255 }
240256 } else if !matches ! ( value, "n" | "e" | "s" | "t" | "m" ) {
241- Err ( Error :: with_description (
257+ Err ( ParseOnTheFlyLimitError :: new (
242258 format ! (
243259 "invalid value '{}' [possible values: none, n<num>, e<num>, s<num>, t<num>, m<num>]" ,
244260 value
245261 ) ,
246262 ErrorKind :: InvalidValue ,
247263 ) )
248264 } else if number. is_empty ( ) {
249- Err ( Error :: with_description (
265+ Err ( ParseOnTheFlyLimitError :: new (
250266 format ! ( "no number for value '{}'" , value) ,
251267 ErrorKind :: ValueValidation ,
252268 ) )
253269 } else {
254270 let num = number. parse :: < u64 > ( ) . map_err ( |e| {
255- Error :: with_description (
271+ ParseOnTheFlyLimitError :: new (
256272 format ! ( "could not parse number '{}': {}" , number, e) ,
257273 ErrorKind :: ValueValidation ,
258274 )
259275 } ) ?;
260276 const LIMIT : u64 = 1 << 16 ;
261277 if num == 0 || num >= LIMIT {
262- Err ( Error :: with_description (
278+ Err ( ParseOnTheFlyLimitError :: new (
263279 format ! (
264280 "number '{}' out of range [must be greater than 0 and less than {}]" ,
265281 num, LIMIT
0 commit comments