changelog shortlog graph tags branches changeset files revisions annotate raw help

Mercurial > core / rust/lib/obj/src/id.rs

changeset 17: c7165d93a9eb
child: 255000153a76
author: ellis <ellis@rwest.io>
date: Sun, 22 Oct 2023 23:03:15 -0400
permissions: -rw-r--r--
description: add obj and net src
1 //! rlib/obj/src/id.rs --- rlib::obj::id
2 //!
3 //! primitive ID types.
4 
5 pub use hash::Id;
6 pub use rusty_ulid::{self, Ulid};
7 use std::{fmt, str::FromStr};
8 pub use uuid::Uuid;
9 /// Identity trait
10 ///
11 /// Defines Identity-related behaviors
12 pub trait Identity: Sized {
13  /// return the hashed bytes of an ObjectId
14  fn id(&self) -> Id;
15 }
16 
17 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
18 pub struct ObjectId(u128);
19 
20 pub struct NameSpace {
21  pub prefix: Option<String>,
22  pub capacity: u64,
23  pub route: Vec<Id>,
24  pub key: Option<Id>,
25 }
26 
27 pub struct Domain {
28  pub ns: NameSpace,
29  pub id: Id,
30 }
31 
32 impl From<Uuid> for ObjectId {
33  fn from(uuid: Uuid) -> Self {
34  ObjectId(uuid.as_u128())
35  }
36 }
37 
38 impl From<Ulid> for ObjectId {
39  fn from(ulid: Ulid) -> Self {
40  ObjectId(u128::from(ulid))
41  }
42 }
43 
44 impl From<u128> for ObjectId {
45  fn from(src: u128) -> Self {
46  ObjectId(src)
47  }
48 }
49 
50 impl FromStr for ObjectId {
51  type Err = ();
52  fn from_str(input: &str) -> std::result::Result<ObjectId, Self::Err> {
53  match input {
54  i => Ok(ObjectId(u128::from(Ulid::from_str(i).unwrap()))),
55  }
56  }
57 }
58 
59 impl fmt::Display for ObjectId {
60  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
61  match *self {
62  ObjectId(i) => {
63  write!(f, "{}", Ulid::from(i))
64  }
65  }
66  }
67 }