@@ -26,8 +26,7 @@ mod value;
26
26
27
27
pub use value:: * ;
28
28
29
- /// A type alias for errors returned when construction of an annotation fails.
30
- pub type AnnotationsError = KeyValuePairsError < Infallible > ;
29
+ pub type AnnotationsError = KeyValuePairsError ;
31
30
32
31
/// A type alias for errors returned when construction or manipulation of a set
33
32
/// of annotations fails.
@@ -45,13 +44,14 @@ pub type AnnotationError = KeyValuePairError<Infallible>;
45
44
#[ derive( Debug ) ]
46
45
pub struct Annotation ( KeyValuePair < AnnotationValue > ) ;
47
46
48
- impl < T > TryFrom < ( T , T ) > for Annotation
47
+ impl < K , V > TryFrom < ( K , V ) > for Annotation
49
48
where
50
- T : AsRef < str > ,
49
+ K : AsRef < str > ,
50
+ V : AsRef < str > ,
51
51
{
52
52
type Error = AnnotationError ;
53
53
54
- fn try_from ( value : ( T , T ) ) -> Result < Self , Self :: Error > {
54
+ fn try_from ( value : ( K , V ) ) -> Result < Self , Self :: Error > {
55
55
let kvp = KeyValuePair :: try_from ( value) ?;
56
56
Ok ( Self ( kvp) )
57
57
}
@@ -150,6 +150,19 @@ impl TryFrom<BTreeMap<String, String>> for Annotations {
150
150
}
151
151
}
152
152
153
+ impl < const N : usize , K , V > TryFrom < [ ( K , V ) ; N ] > for Annotations
154
+ where
155
+ K : AsRef < str > ,
156
+ V : AsRef < str > ,
157
+ {
158
+ type Error = AnnotationError ;
159
+
160
+ fn try_from ( value : [ ( K , V ) ; N ] ) -> Result < Self , Self :: Error > {
161
+ let kvps = KeyValuePairs :: try_from ( value) ?;
162
+ Ok ( Self ( kvps) )
163
+ }
164
+ }
165
+
153
166
impl FromIterator < KeyValuePair < AnnotationValue > > for Annotations {
154
167
fn from_iter < T : IntoIterator < Item = KeyValuePair < AnnotationValue > > > ( iter : T ) -> Self {
155
168
let kvps = KeyValuePairs :: from_iter ( iter) ;
@@ -174,17 +187,19 @@ impl Annotations {
174
187
Self ( KeyValuePairs :: new_with ( pairs) )
175
188
}
176
189
177
- /// Tries to insert a new [`Annotation`]. It ensures there are no duplicate
178
- /// entries. Trying to insert duplicated data returns an error. If no such
179
- /// check is required, use the `insert` function instead.
180
- pub fn try_insert ( & mut self , annotation : Annotation ) -> Result < & mut Self , AnnotationsError > {
181
- self . 0 . try_insert ( annotation. 0 ) ?;
182
- Ok ( self )
190
+ /// Tries to insert a new annotation by first parsing `annotation` as an
191
+ /// [`Annotation`] and then inserting it into the list. This function will
192
+ /// overwrite any existing annotation already present.
193
+ pub fn parse_insert (
194
+ & mut self ,
195
+ annotation : impl TryInto < Annotation , Error = AnnotationError > ,
196
+ ) -> Result < ( ) , AnnotationError > {
197
+ self . 0 . insert ( annotation. try_into ( ) ?. 0 ) ;
198
+ Ok ( ( ) )
183
199
}
184
200
185
- /// Inserts a new [`Annotation`]. This function will overide any existing
186
- /// annotation already present. If this behaviour is not desired, use the
187
- /// `try_insert` function instead.
201
+ /// Inserts a new [`Annotation`]. This function will overwrite any existing
202
+ /// annotation already present.
188
203
pub fn insert ( & mut self , annotation : Annotation ) -> & mut Self {
189
204
self . 0 . insert ( annotation. 0 ) ;
190
205
self
@@ -196,6 +211,11 @@ impl Annotations {
196
211
// the need to write boilerplate code.
197
212
delegate ! {
198
213
to self . 0 {
214
+ /// Tries to insert a new [`Annotation`]. It ensures there are no duplicate
215
+ /// entries. Trying to insert duplicated data returns an error. If no such
216
+ /// check is required, use [`Annotations::insert`] instead.
217
+ pub fn try_insert( & mut self , #[ newtype] annotation: Annotation ) -> Result <( ) , AnnotationsError >;
218
+
199
219
/// Extends `self` with `other`.
200
220
pub fn extend( & mut self , #[ newtype] other: Self ) ;
201
221
@@ -217,3 +237,23 @@ impl Annotations {
217
237
}
218
238
}
219
239
}
240
+
241
+ #[ cfg( test) ]
242
+ mod test {
243
+ use super :: * ;
244
+
245
+ #[ test]
246
+ fn parse_insert ( ) {
247
+ let mut annotations = Annotations :: new ( ) ;
248
+
249
+ annotations
250
+ . parse_insert ( ( "stackable.tech/managed-by" , "stackablectl" ) )
251
+ . unwrap ( ) ;
252
+
253
+ annotations
254
+ . parse_insert ( ( "stackable.tech/vendor" , "Stäckable" ) )
255
+ . unwrap ( ) ;
256
+
257
+ assert_eq ! ( annotations. len( ) , 2 ) ;
258
+ }
259
+ }
0 commit comments