feat: try from okid
This commit is contained in:
parent
2bd505a110
commit
0ce12fc868
4 changed files with 34 additions and 1 deletions
|
@ -34,3 +34,14 @@ impl FromStr for Fingerprint {
|
|||
Ok(Fingerprint(u64::from_be_bytes(hash)))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<OkId> for u64 {
|
||||
type Error = super::Error;
|
||||
|
||||
fn try_from(value: OkId) -> Result<Self, Self::Error> {
|
||||
match value.digest {
|
||||
Digest::Fingerprint(Fingerprint(value)) => Ok(value),
|
||||
_ => Err(super::Error::InvalidHashType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -627,7 +627,7 @@ impl std::convert::AsRef<[u8]> for OkId {
|
|||
}
|
||||
|
||||
/// Create a path-safe string from an OkId
|
||||
pub fn pathsafe(id: OkId) -> impl AsRef<[u8]> {
|
||||
pub fn pathsafe(id: OkId) -> String {
|
||||
format!("1/{}", id.to_string().replace(SEPARATOR, "/"))
|
||||
}
|
||||
|
||||
|
|
11
src/ulid.rs
11
src/ulid.rs
|
@ -39,3 +39,14 @@ impl From<Ulid> for ulid::Ulid {
|
|||
ulid::Ulid(val.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<OkId> for ulid::Ulid {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: OkId) -> Result<Self, Self::Error> {
|
||||
match value.digest {
|
||||
super::Digest::Ulid(ulid) => Ok(ulid.into()),
|
||||
_ => Err(crate::Error::InvalidHashType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
11
src/uuid.rs
11
src/uuid.rs
|
@ -39,3 +39,14 @@ impl From<Uuid> for uuid::Uuid {
|
|||
uuid::Uuid::from_u128(val.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<OkId> for uuid::Uuid {
|
||||
type Error = crate::Error;
|
||||
|
||||
fn try_from(value: OkId) -> Result<Self, Self::Error> {
|
||||
match value.digest {
|
||||
super::Digest::Uuid(uuid) => Ok(uuid.into()),
|
||||
_ => Err(crate::Error::InvalidHashType),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue