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
//! Utilities

mod action;
mod attr_value;
mod context;
mod global_close;
mod html;
mod ouia;
mod props;
mod raw;
mod styled;
pub(crate) mod wrap;

pub use action::*;
pub use attr_value::*;
pub use context::*;
pub use global_close::*;
pub use html::*;
pub use ouia::*;
pub use props::*;
pub use raw::*;
pub use styled::*;

use std::fmt::{Debug, Display, Formatter};
use yew::{html::IntoPropValue, AttrValue};

/// Create a random ID.
///
/// This is creating a random ID, using a v4 UUID.
pub fn random_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Id(uuid::Uuid);

impl Id {
    /// Get a new, random ID
    pub fn new() -> Self {
        Id(uuid::Uuid::new_v4())
    }
}

impl Default for Id {
    fn default() -> Self {
        Self::new()
    }
}

impl Display for Id {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        std::fmt::Display::fmt(&self.0, f)
    }
}

impl IntoPropValue<AttrValue> for Id {
    fn into_prop_value(self) -> AttrValue {
        self.to_string().into()
    }
}

impl IntoPropValue<Option<AttrValue>> for Id {
    fn into_prop_value(self) -> Option<AttrValue> {
        Some(self.to_string().into())
    }
}