Skip to content

Commit

Permalink
feat: allow UUID to come from string-ish conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kbknapp committed Apr 12, 2024
1 parent 7fc10c9 commit d6e0094
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 20 deletions.
21 changes: 6 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,15 @@ fn run_oidstr() -> Result<()> {
let oid = OidStr::new_v7_now("EXA")?;
println!("OidStr from UUIDv7: {oid}");
}
// OIDs can also be created from the raw parts
let oid = OidStr::try_with_uuid("EXA", "b3cfdafa-3fec-41e2-82bf-ff881131abf1")?;
println!("OidStr from UUID: {oid}");
// OIDs can be parsed from strings, however the "value" must be a valid
// base32hex (no pad) encoded UUID
let oid: OidStr = "EXA-4GKFGPRVND4QT3PDR90PDKF66O".parse()?;
println!("OidStr from string: {oid}");
// OIDs can also be created from the raw parts
let oid = OidStr::with_uuid(
"EXA",
"b3cfdafa-3fec-41e2-82bf-ff881131abf1"
.parse::<Uuid>()?
)?;
println!("OidStr from raw parts: {oid}");
// One can retrieve the various parts of the OID if needed
println!("Components of {oid}:");
println!("\tPrefix: {}", oid.prefix());
Expand All @@ -136,18 +131,14 @@ fn run_oid() -> Result<()> {
let oid: Oid<EXA> = Oid::new_v7_now();
println!("Oid<EXA> with new UUIDv7: {oid}");
}
// Or by giving a UUID
let oid: Oid<EXA> = Oid::try_with_uuid("b3cfdafa-3fec-41e2-82bf-ff881131abf1")?;
println!("Oid<EXA> with new UUID: {oid}");
// We can go the other direction and parse a string to a Oid<EXA>
let oid: Oid<EXA> = "EXA-4GKFGPRVND4QT3PDR90PDKF66O".parse()?;
println!("Oid<EXA> with from string: {oid}");
// TOIDs can also be created from the raw parts
let oid: Oid<EXA> = Oid::with_uuid(
"b3cfdafa-3fec-41e2-82bf-ff881131abf1"
.parse::<Uuid>()?
);
println!("Oid from raw parts: {oid}");
// One can retrieve the various parts of the OID if needed
println!("Components of {oid}:");
println!("\tPrefix: {}", oid.prefix());
Expand Down
10 changes: 7 additions & 3 deletions src/oid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ use crate::{
/// impl OidPrefix for B {}
///
/// // The same UUID for both
/// let uuid = Uuid::new_v4();
/// let oid_a: Oid<A> = Oid::with_uuid(uuid.clone());
/// let oid_b: Oid<B> = Oid::with_uuid(uuid);
/// let oid_a: Oid<A> = Oid::try_with_uuid("b3cfdafa-3fec-41e2-82bf-ff881131abf1").unwrap();
/// let oid_b: Oid<B> = Oid::try_with_uuid("b3cfdafa-3fec-41e2-82bf-ff881131abf1").unwrap();
///
/// // This fails to compile because `Oid<A>` is a different type than `Oid<B>` and no
/// // PartialEq or Eq is implemented between these two types.
Expand Down Expand Up @@ -70,6 +69,11 @@ impl<P: OidPrefix> Oid<P> {
}
}

/// Attempts to create a new Oid with a given string-ish UUID
pub fn try_with_uuid<S: AsRef<str>>(uuid: S) -> Result<Self> {
Ok(Self::with_uuid(uuid.as_ref().try_into()?))
}

/// Get the [`Prefix`] of the TOID
///
/// # Panics
Expand Down
22 changes: 20 additions & 2 deletions src/oidstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ impl OidStr {
where
P: TryInto<Prefix, Error = Error>,
{
Self::with_uuid(prefix, Uuid::new_v4())
Ok(Self {
prefix: prefix.try_into()?,
uuid: Uuid::new_v4(),
})
}

/// Create a new OID with a given [`Prefix`] and generating a new UUIDv7
Expand All @@ -40,7 +43,10 @@ impl OidStr {
where
P: TryInto<Prefix, Error = Error>,
{
Self::with_uuid(prefix, Uuid::new_v7(Timestamp::now(NoContext)))
Ok(Self {
prefix: prefix.try_into()?,
uuid: Uuid::new_v7(Timestamp::now(NoContext)),
})
}

/// Create a new OID with a given [`Prefix`] and generating a new UUIDv7
Expand Down Expand Up @@ -68,6 +74,18 @@ impl OidStr {
})
}

/// Create a new OID with a given [`Prefix`] and a given string-ish UUID.
///
/// > **NOTE:** The Prefix must be ASCII characters of `A-Z,a-z,0-9` (this
/// > restriction is arbitrary and could be lifted in the future.
pub fn try_with_uuid<P, S>(prefix: P, uuid: S) -> Result<Self>
where
P: TryInto<Prefix, Error = Error>,
S: AsRef<str>,
{
Self::with_uuid(prefix, uuid.as_ref().try_into()?)
}

/// Get the [`Prefix`] of the OID
pub fn prefix(&self) -> &Prefix { &self.prefix }

Expand Down

0 comments on commit d6e0094

Please sign in to comment.