Skip to content

Commit 2e5291c

Browse files
committed
prepare for version 0.3.0
Apologies for the churn, but making this change is better now rather than later. Hopefully this will be the last change in this series.
1 parent c2d9373 commit 2e5291c

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

CHANGELOG.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
# Changelog
22

3+
## [0.3.0] - 2024-02-02
4+
5+
### Breaking changes
6+
7+
- `TypedUuidTag::try_new` returns a new `TagError` type rather than just a raw `&'static str`.
8+
9+
### Changed
10+
11+
- `TypedUuidTag::as_str` is now a `const fn`.
12+
313
## [0.2.1] - 2024-02-02
414

515
Documentation improvements.
@@ -18,6 +28,7 @@ Documentation improvements.
1828

1929
Initial release.
2030

31+
[0.3.0]: https://github.com/oxidecomputer/newtype-uuid/releases/newtype-uuid-0.3.0
2132
[0.2.1]: https://github.com/oxidecomputer/newtype-uuid/releases/newtype-uuid-0.2.1
2233
[0.2.0]: https://github.com/oxidecomputer/newtype-uuid/releases/newtype-uuid-0.2.0
2334
[0.1.0]: https://github.com/oxidecomputer/newtype-uuid/releases/newtype-uuid-0.1.0

src/lib.rs

+44-5
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,12 @@ mod schemars08_imp {
240240
/// )*
241241
/// };
242242
/// }
243+
///
244+
/// // Invoke this macro with:
245+
/// impl_typed_uuid_kind! {
246+
/// Kind1 => "kind1",
247+
/// Kind2 => "kind2",
248+
/// }
243249
/// ```
244250
///
245251
/// [`JsonSchema`]: schemars::JsonSchema
@@ -272,7 +278,7 @@ impl TypedUuidTag {
272278
/// Panics if the above conditions aren't met. (This is a const fn, so it can't return an
273279
/// error.)
274280
pub const fn new(tag: &'static str) -> Self {
275-
match Self::try_new(tag) {
281+
match Self::try_new_impl(tag) {
276282
Ok(tag) => tag,
277283
Err(message) => panic!("{}", message),
278284
}
@@ -288,8 +294,18 @@ impl TypedUuidTag {
288294
///
289295
/// # Errors
290296
///
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> {
293309
if tag.is_empty() {
294310
return Err("tag must not be empty");
295311
}
@@ -318,7 +334,7 @@ impl TypedUuidTag {
318334
}
319335

320336
/// Returns the tag as a string.
321-
pub fn as_str(&self) -> &'static str {
337+
pub const fn as_str(&self) -> &'static str {
322338
self.0
323339
}
324340
}
@@ -335,6 +351,29 @@ impl AsRef<str> for TypedUuidTag {
335351
}
336352
}
337353

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+
338377
/// An error that occurred while parsing a [`TypedUuid`].
339378
#[derive(Clone, Debug)]
340379
pub struct ParseError {
@@ -432,7 +471,7 @@ mod tests {
432471
}
433472

434473
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();
436475
}
437476
}
438477
}

0 commit comments

Comments
 (0)