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
use uuid::Uuid;

use crate::collections::Metadata;
use crate::errors::{Define, Error, Result};

pub enum IdError {
    Invalid,
}

impl Define for IdError {
    fn define(&self) -> &str {
        match self {
            IdError::Invalid => "id.invalid",
        }
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct StrId {
    id: String,
}

impl StrId {
    pub fn new<S: Into<String>>(id: S) -> Result<StrId> {
        let id = id.into();

        if id.is_empty() {
            return Err(Error::new(
                IdError::Invalid,
                "empty string id",
                Metadata::with("id", id),
            ));
        }

        Ok(StrId { id })
    }

    pub fn generate_uuid() -> Result<StrId> {
        let uuid = Uuid::new_v4();
        StrId::new(uuid.to_string())
    }

    pub fn generate_slug<S: Into<String>>(str: S) -> Result<StrId> {
        StrId::new(slug::slugify(str.into()))
    }

    pub fn value(&self) -> &str {
        &self.id
    }
}

impl ToString for StrId {
    fn to_string(&self) -> String {
        self.id.to_owned()
    }
}