From 95ea352b0aed6deff550b259e573f58dd1e14b19 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Thu, 4 Jan 2024 15:48:35 +0900 Subject: [PATCH] object_id: add fallible version of ObjectId::from_hex() --- lib/src/object_id.rs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/lib/src/object_id.rs b/lib/src/object_id.rs index 734599ef2..76a39e298 100644 --- a/lib/src/object_id.rs +++ b/lib/src/object_id.rs @@ -42,8 +42,16 @@ macro_rules! impl_id_type { Self(bytes.to_vec()) } + /// Parses the given hex string into an ObjectId. + /// + /// The given string is usually a literal, and must be valid. pub fn from_hex(hex: &str) -> Self { - Self(hex::decode(hex).unwrap()) + Self::try_from_hex(hex).unwrap() + } + + /// Parses the given hex string into an ObjectId. + pub fn try_from_hex(hex: &str) -> Result { + hex::decode(hex).map(Self) } }