@@ -240,6 +240,12 @@ mod schemars08_imp {
240
240
/// )*
241
241
/// };
242
242
/// }
243
+ ///
244
+ /// // Invoke this macro with:
245
+ /// impl_typed_uuid_kind! {
246
+ /// Kind1 => "kind1",
247
+ /// Kind2 => "kind2",
248
+ /// }
243
249
/// ```
244
250
///
245
251
/// [`JsonSchema`]: schemars::JsonSchema
@@ -272,7 +278,7 @@ impl TypedUuidTag {
272
278
/// Panics if the above conditions aren't met. (This is a const fn, so it can't return an
273
279
/// error.)
274
280
pub const fn new ( tag : & ' static str ) -> Self {
275
- match Self :: try_new ( tag) {
281
+ match Self :: try_new_impl ( tag) {
276
282
Ok ( tag) => tag,
277
283
Err ( message) => panic ! ( "{}" , message) ,
278
284
}
@@ -288,8 +294,18 @@ impl TypedUuidTag {
288
294
///
289
295
/// # Errors
290
296
///
291
- /// Returns an error if the above conditions aren't met.
292
- pub const fn try_new ( tag : & ' static str ) -> Result < Self , & ' static str > {
297
+ /// Returns a [`TagError`] if the above conditions aren't met.
298
+ pub const fn try_new ( tag : & ' static str ) -> Result < Self , TagError > {
299
+ match Self :: try_new_impl ( tag) {
300
+ Ok ( tag) => Ok ( tag) ,
301
+ Err ( message) => Err ( TagError {
302
+ input : tag,
303
+ message,
304
+ } ) ,
305
+ }
306
+ }
307
+
308
+ const fn try_new_impl ( tag : & ' static str ) -> Result < Self , & ' static str > {
293
309
if tag. is_empty ( ) {
294
310
return Err ( "tag must not be empty" ) ;
295
311
}
@@ -318,7 +334,7 @@ impl TypedUuidTag {
318
334
}
319
335
320
336
/// Returns the tag as a string.
321
- pub fn as_str ( & self ) -> & ' static str {
337
+ pub const fn as_str ( & self ) -> & ' static str {
322
338
self . 0
323
339
}
324
340
}
@@ -335,6 +351,29 @@ impl AsRef<str> for TypedUuidTag {
335
351
}
336
352
}
337
353
354
+ /// An error that occurred while creating a [`TypedUuidTag`].
355
+ #[ derive( Clone , Debug ) ]
356
+ pub struct TagError {
357
+ /// The input string.
358
+ pub input : & ' static str ,
359
+
360
+ /// The error message.
361
+ pub message : & ' static str ,
362
+ }
363
+
364
+ impl fmt:: Display for TagError {
365
+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
366
+ write ! (
367
+ f,
368
+ "error creating tag from '{}': {}" ,
369
+ self . input, self . message
370
+ )
371
+ }
372
+ }
373
+
374
+ #[ cfg( feature = "std" ) ]
375
+ impl std:: error:: Error for TagError { }
376
+
338
377
/// An error that occurred while parsing a [`TypedUuid`].
339
378
#[ derive( Clone , Debug ) ]
340
379
pub struct ParseError {
@@ -432,7 +471,7 @@ mod tests {
432
471
}
433
472
434
473
for invalid_tag in & [ "" , "1" , "-" , "a1b!" , "a1-b!" , "a1_b:" , "\u{1f4a9} " ] {
435
- assert ! ( TypedUuidTag :: try_new( invalid_tag) . is_err ( ) ) ;
474
+ TypedUuidTag :: try_new ( invalid_tag) . unwrap_err ( ) ;
436
475
}
437
476
}
438
477
}
0 commit comments