patternfly_yew/utils/
ouia.rs

1use std::sync::atomic::{AtomicUsize, Ordering};
2use yew::html::IntoPropValue;
3use yew::AttrValue;
4
5#[macro_export]
6macro_rules! ouia {
7    ($framework:literal, $component:literal) => {
8        Ouia::with_full(concat!($framework, "/", $component))
9    };
10    ($component:literal) => {
11        ouia!("PF5", $component)
12    };
13}
14
15pub struct Ouia(OuiaComponentType);
16
17#[derive(Copy, Clone, Debug, Eq, PartialEq)]
18pub struct OuiaComponentType(&'static str);
19
20#[derive(Copy, Clone, Debug, Eq, PartialEq)]
21pub struct OuiaSafe(bool);
22
23impl OuiaSafe {
24    pub const TRUE: OuiaSafe = OuiaSafe(true);
25    pub const FALSE: OuiaSafe = OuiaSafe(false);
26}
27
28impl From<bool> for OuiaSafe {
29    fn from(value: bool) -> Self {
30        Self(value)
31    }
32}
33
34impl IntoPropValue<OuiaSafe> for bool {
35    fn into_prop_value(self) -> OuiaSafe {
36        OuiaSafe(self)
37    }
38}
39
40impl IntoPropValue<AttrValue> for OuiaSafe {
41    fn into_prop_value(self) -> AttrValue {
42        match self.0 {
43            true => AttrValue::Static("true"),
44            false => AttrValue::Static("false"),
45        }
46    }
47}
48
49impl IntoPropValue<Option<AttrValue>> for OuiaSafe {
50    fn into_prop_value(self) -> Option<AttrValue> {
51        Some(self.into_prop_value())
52    }
53}
54
55impl IntoPropValue<AttrValue> for OuiaComponentType {
56    fn into_prop_value(self) -> AttrValue {
57        AttrValue::Static(self.0)
58    }
59}
60
61impl IntoPropValue<Option<AttrValue>> for OuiaComponentType {
62    fn into_prop_value(self) -> Option<AttrValue> {
63        Some(self.into_prop_value())
64    }
65}
66
67impl Ouia {
68    pub const fn with_full(full_component_name: &'static str) -> Self {
69        Self(OuiaComponentType(full_component_name))
70    }
71
72    pub fn generated_id(&self) -> String {
73        let count = counter();
74        format!("OUIA-Generated-{count}")
75    }
76
77    pub const fn component_type(&self) -> OuiaComponentType {
78        self.0
79    }
80}
81
82fn counter() -> usize {
83    static COUNT: AtomicUsize = AtomicUsize::new(0);
84    COUNT.fetch_add(1, Ordering::Relaxed)
85}