@@ -68,7 +68,7 @@ pub enum Error {
68
68
}
69
69
70
70
/** Indicates how an object was looked up (for an `ObjectNotFound` error) */
71
- #[ derive( Debug , Deserialize , PartialEq , Serialize ) ]
71
+ #[ derive( Clone , Debug , Deserialize , PartialEq , Serialize ) ]
72
72
pub enum LookupType {
73
73
/** a specific name was requested */
74
74
ByName ( String ) ,
@@ -78,6 +78,26 @@ pub enum LookupType {
78
78
Other ( String ) ,
79
79
}
80
80
81
+ impl LookupType {
82
+ /// Returns an ObjectNotFound error appropriate for the case where this
83
+ /// lookup failed
84
+ pub fn into_not_found ( self , type_name : ResourceType ) -> Error {
85
+ Error :: ObjectNotFound { type_name, lookup_type : self }
86
+ }
87
+ }
88
+
89
+ impl From < & str > for LookupType {
90
+ fn from ( name : & str ) -> Self {
91
+ LookupType :: ByName ( name. to_owned ( ) )
92
+ }
93
+ }
94
+
95
+ impl From < & Name > for LookupType {
96
+ fn from ( name : & Name ) -> Self {
97
+ LookupType :: from ( name. as_str ( ) )
98
+ }
99
+ }
100
+
81
101
impl Error {
82
102
/**
83
103
* Returns whether the error is likely transient and could reasonably be
@@ -103,28 +123,22 @@ impl Error {
103
123
* name.
104
124
*/
105
125
pub fn not_found_by_name ( type_name : ResourceType , name : & Name ) -> Error {
106
- Error :: ObjectNotFound {
107
- type_name,
108
- lookup_type : LookupType :: ByName ( name. as_str ( ) . to_owned ( ) ) ,
109
- }
126
+ LookupType :: from ( name) . into_not_found ( type_name)
110
127
}
111
128
112
129
/**
113
130
* Generates an [`Error::ObjectNotFound`] error for a lookup by object id.
114
131
*/
115
132
pub fn not_found_by_id ( type_name : ResourceType , id : & Uuid ) -> Error {
116
- Error :: ObjectNotFound { type_name , lookup_type : LookupType :: ById ( * id) }
133
+ LookupType :: ById ( * id) . into_not_found ( type_name )
117
134
}
118
135
119
136
/**
120
137
* Generates an [`Error::ObjectNotFound`] error for some other kind of
121
138
* lookup.
122
139
*/
123
140
pub fn not_found_other ( type_name : ResourceType , message : String ) -> Error {
124
- Error :: ObjectNotFound {
125
- type_name,
126
- lookup_type : LookupType :: Other ( message) ,
127
- }
141
+ LookupType :: Other ( message) . into_not_found ( type_name)
128
142
}
129
143
130
144
/**
0 commit comments