@@ -3,111 +3,81 @@ use glob;
33use regex;
44use serde_yaml;
55use std:: {
6- error:: Error ,
76 fmt,
87 fmt:: { Display , Formatter } ,
98 io,
109 path:: PathBuf ,
1110 result,
1211} ;
12+ use thiserror:: Error ;
1313/// Shorthand for a Result that returns an `OrcaError`.
1414pub type Result < T > = result:: Result < T , OrcaError > ;
15-
1615/// Possible errors you may encounter.
17- #[ derive( Debug ) ]
16+ #[ derive( Error , Debug ) ]
1817pub ( crate ) enum Kind {
19- /// Returned if a file is not expected to exist.
20- FileExists ( PathBuf ) ,
21- /// Returned if an annotation was expected to exist.
22- NoAnnotationFound ( String , String , String ) ,
23- /// Returned if a model save was attempted without an annotation set.
24- MissingAnnotationOnSave ,
25- /// Returned if an annotation delete was attempted on a model's last annotation.
26- DeletingLastAnnotation ( String , String , String ) ,
27- /// Returned if a regular expression was expected to match.
28- NoRegexMatch ,
29- /// Wrapper around `glob::GlobError`
30- GlobError ( glob:: GlobError ) ,
31- /// Wrapper around `glob::PatternError`
32- GlobPatternError ( glob:: PatternError ) ,
33- /// Wrapper around `regex::Error`
34- RegexError ( regex:: Error ) ,
35- /// Wrapper around `serde_yaml::Error`
36- SerdeYamlError ( serde_yaml:: Error ) ,
37- /// Wrapper around `io::Error`
38- IoError ( io:: Error ) ,
18+ #[ error( "File `{}` already exists." , path. to_string_lossy( ) . bright_cyan( ) ) ]
19+ FileExists { path : PathBuf } ,
20+ #[ error( "No annotation found for `{name}:{version}` {class}." ) ]
21+ NoAnnotationFound {
22+ class : String ,
23+ name : String ,
24+ version : String ,
25+ } ,
26+ #[ error( transparent) ]
27+ GlobPatternError ( #[ from] glob:: PatternError ) ,
28+ #[ error( transparent) ]
29+ RegexError ( #[ from] regex:: Error ) ,
30+ #[ error( transparent) ]
31+ SerdeYamlError ( #[ from] serde_yaml:: Error ) ,
32+ #[ error( transparent) ]
33+ IoError ( #[ from] io:: Error ) ,
3934}
40-
4135/// A stable error API interface.
42- #[ derive( Debug ) ]
43- pub struct OrcaError ( Kind ) ;
44- impl Error for OrcaError { }
36+ #[ derive( Error , Debug ) ]
37+ pub struct OrcaError {
38+ kind : Kind ,
39+ }
4540impl OrcaError {
46- /// Returns `true` if the error was caused by an attempt to delete a model's last annotation.
47- pub const fn is_deleting_last_annotation ( & self ) -> bool {
48- matches ! ( self . 0 , Kind :: DeletingLastAnnotation ( _ , _ , _ ) )
41+ /// Returns `true` if the error was caused by an invalid model annotation.
42+ pub const fn is_invalid_annotation ( & self ) -> bool {
43+ matches ! ( self . kind , Kind :: NoAnnotationFound { .. } )
4944 }
5045}
5146impl Display for OrcaError {
52- fn fmt ( & self , f : & mut Formatter ) -> fmt:: Result {
53- match & self . 0 {
54- Kind :: FileExists ( path) => {
55- write ! (
56- f,
57- "File `{}` already exists." ,
58- path. to_string_lossy( ) . bright_cyan( )
59- )
60- }
61- Kind :: NoAnnotationFound ( class, name, version) => {
62- write ! ( f, "No annotation found for `{name}:{version}` {class}." )
63- }
64- Kind :: MissingAnnotationOnSave => {
65- write ! ( f, "No annotation found when attempting to store." )
66- }
67- Kind :: DeletingLastAnnotation ( class, name, version) => {
68- write ! (
69- f,
70- "Attempted to delete the last annotation for `{name}:{version}` {class}."
71- )
72- }
73- Kind :: NoRegexMatch => {
74- write ! ( f, "No match for regex." )
75- }
76- Kind :: GlobError ( error) => write ! ( f, "{error}" ) ,
77- Kind :: GlobPatternError ( error) => write ! ( f, "{error}" ) ,
78- Kind :: SerdeYamlError ( error) => write ! ( f, "{error}" ) ,
79- Kind :: RegexError ( error) => write ! ( f, "{error}" ) ,
80- Kind :: IoError ( error) => write ! ( f, "{error}" ) ,
81- }
82- }
83- }
84- impl From < glob:: GlobError > for OrcaError {
85- fn from ( error : glob:: GlobError ) -> Self {
86- Self ( Kind :: GlobError ( error) )
47+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> fmt:: Result {
48+ write ! ( f, "{}" , self . kind)
8749 }
8850}
8951impl From < glob:: PatternError > for OrcaError {
9052 fn from ( error : glob:: PatternError ) -> Self {
91- Self ( Kind :: GlobPatternError ( error) )
92- }
93- }
94- impl From < serde_yaml:: Error > for OrcaError {
95- fn from ( error : serde_yaml:: Error ) -> Self {
96- Self ( Kind :: SerdeYamlError ( error) )
53+ Self {
54+ kind : Kind :: GlobPatternError ( error) ,
55+ }
9756 }
9857}
9958impl From < regex:: Error > for OrcaError {
10059 fn from ( error : regex:: Error ) -> Self {
101- Self ( Kind :: RegexError ( error) )
60+ Self {
61+ kind : Kind :: RegexError ( error) ,
62+ }
63+ }
64+ }
65+ impl From < serde_yaml:: Error > for OrcaError {
66+ fn from ( error : serde_yaml:: Error ) -> Self {
67+ Self {
68+ kind : Kind :: SerdeYamlError ( error) ,
69+ }
10270 }
10371}
10472impl From < io:: Error > for OrcaError {
10573 fn from ( error : io:: Error ) -> Self {
106- Self ( Kind :: IoError ( error) )
74+ Self {
75+ kind : Kind :: IoError ( error) ,
76+ }
10777 }
10878}
10979impl From < Kind > for OrcaError {
110- fn from ( error : Kind ) -> Self {
111- Self ( error )
80+ fn from ( kind : Kind ) -> Self {
81+ Self { kind }
11282 }
11383}
0 commit comments