1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
    Appellation: ids <module>
    Contrib: FL03 <jo3mccain@icloud.com>
    Description: ... Summary ...
*/
use crate::{generate_random_number, generate_random_string, BsonOid};
use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;
use strum::{Display, EnumString, EnumVariantNames};

#[derive(
    Clone,
    Debug,
    Deserialize,
    Display,
    EnumString,
    EnumVariantNames,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    Serialize,
    SmartDefault,
)]
#[strum(serialize_all = "snake_case")]
pub enum Id {
    Int(i64),
    #[default]
    Object(BsonOid),
    String(String),
    Null,
}

impl Id {
    pub fn gen_rint() -> Self {
        Self::Int(generate_random_number())
    }
    //
    pub fn gen_rstr(len: Option<usize>) -> Self {
        Self::String(generate_random_string(len.unwrap_or(12)))
    }
}

impl From<i64> for Id {
    fn from(data: i64) -> Self {
        Self::Int(data)
    }
}

impl From<BsonOid> for Id {
    fn from(data: BsonOid) -> Self {
        Self::Object(data)
    }
}

impl From<String> for Id {
    fn from(data: String) -> Self {
        Self::String(data)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_ids() {
        let actual = Id::default();
        let expected = actual.clone();
        assert_eq!(actual, expected)
    }
}