patternfly_yew/utils/
mod.rs

1//! Utilities
2
3mod action;
4mod attr_value;
5mod context;
6mod global_close;
7mod html;
8mod ouia;
9mod props;
10mod raw;
11mod styled;
12pub(crate) mod wrap;
13
14pub use action::*;
15pub use attr_value::*;
16pub use context::*;
17pub use global_close::*;
18pub use html::*;
19pub use ouia::*;
20pub use props::*;
21pub use raw::*;
22pub use styled::*;
23
24use std::fmt::{Debug, Display, Formatter};
25use yew::{html::IntoPropValue, AttrValue};
26
27/// Create a random ID.
28///
29/// This is creating a random ID, using a v4 UUID.
30pub fn random_id() -> String {
31    uuid::Uuid::new_v4().to_string()
32}
33
34#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
35pub struct Id(uuid::Uuid);
36
37impl Id {
38    /// Get a new, random ID
39    pub fn new() -> Self {
40        Id(uuid::Uuid::new_v4())
41    }
42}
43
44impl Default for Id {
45    fn default() -> Self {
46        Self::new()
47    }
48}
49
50impl Display for Id {
51    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
52        std::fmt::Display::fmt(&self.0, f)
53    }
54}
55
56impl IntoPropValue<AttrValue> for Id {
57    fn into_prop_value(self) -> AttrValue {
58        self.to_string().into()
59    }
60}
61
62impl IntoPropValue<Option<AttrValue>> for Id {
63    fn into_prop_value(self) -> Option<AttrValue> {
64        Some(self.to_string().into())
65    }
66}