assemblyline_models/types/
ids.rs1use serde_with::{DeserializeFromStr, SerializeDisplay};
2use struct_metadata::Described;
3
4#[cfg(feature = "rand")]
5use rand::RngExt;
6
7use crate::{ElasticMeta, ModelError};
8
9
10
11#[derive(SerializeDisplay, DeserializeFromStr, Debug, Described, Hash, PartialEq, Eq, Clone, Copy)]
13#[metadata_type(ElasticMeta)]
14#[metadata(mapping="keyword")]
15pub struct Sid(u128);
16
17impl std::fmt::Display for Sid {
18 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19 f.write_str(&base62::encode(self.0))
20 }
21}
22
23impl std::str::FromStr for Sid {
24 type Err = ModelError;
25
26 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
27 Ok(Sid(base62::decode(s)?))
28 }
29}
30
31impl Sid {
32 pub fn assign(&self, bins: usize) -> usize {
33 (self.0 % bins as u128) as usize
34 }
35}
36
37#[cfg(feature = "rand")]
38impl rand::distr::Distribution<Sid> for rand::distr::StandardUniform {
39 fn sample<R: rand::prelude::Rng + ?Sized>(&self, rng: &mut R) -> Sid {
40 Sid(rng.random())
41 }
42}
43
44#[derive(SerializeDisplay, DeserializeFromStr, Debug, Hash, PartialEq, Eq, Clone, Copy)]
46pub struct Uuid(uuid::Uuid);
47
48impl Described<ElasticMeta> for Uuid {
49 fn metadata() -> struct_metadata::Descriptor<ElasticMeta> {
50 struct_metadata::Descriptor {
51 docs: None,
52 metadata: ElasticMeta { mapping: Some("keyword"), ..Default::default() },
53 kind: struct_metadata::Kind::String,
54 }
55 }
56}
57
58impl std::fmt::Display for Uuid {
59 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60 f.write_str(&self.0.to_string())
61 }
62}
63
64impl std::str::FromStr for Uuid {
65 type Err = uuid::Error;
66
67 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
68 Ok(Uuid(s.parse()?))
69 }
70}