typed_oid/lib.rs
1#![doc = include_str!("../README.md")]
2#![cfg_attr(docsrs, feature(doc_cfg))]
3
4pub mod error;
5mod oid;
6mod oidstr;
7mod prefix;
8mod uuid;
9
10pub use crate::{
11 error::{Error, Result},
12 oid::Oid,
13 oidstr::OidStr,
14 prefix::Prefix,
15};
16
17/// Defines the converting a type to a prefix of an OID
18///
19/// > **NOTE**
20/// > This should be a static representation of the type irrelevant of any
21/// > value in a type instance
22pub trait OidPrefix {
23 /// Get the static string representation of the prefix.
24 ///
25 /// The default representation is to use the type name itself.
26 fn prefix() -> &'static str { std::any::type_name::<Self>().split(':').last().unwrap() }
27
28 /// A partial equality check for the prefix. This is useful in cases when
29 /// converting from a string to an Typed-OID where the type and string
30 /// prefix are not the same.
31 ///
32 /// # Examples
33 ///
34 /// ```rust
35 /// # use typed_oid::{Oid, OidPrefix};
36 /// #[derive(Debug)]
37 /// struct A;
38 /// impl OidPrefix for A {
39 /// fn str_partial_eq(s: &str) -> bool { "apple" == s }
40 /// }
41 ///
42 /// let oid: Oid<A> = "apple-4GKFGPRVND4QT3PDR90PDKF66O".parse().unwrap();
43 /// ```
44 fn str_partial_eq(s: &str) -> bool { Self::prefix() == s }
45}