Skip to main content

a2ui_types/
common.rs

1use serde::{Deserialize, Serialize};
2
3/// Unique identifier for a UI surface.
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
5#[serde(transparent)]
6pub struct SurfaceId(pub String);
7
8impl SurfaceId {
9    pub fn new(id: impl Into<String>) -> Self {
10        Self(id.into())
11    }
12
13    pub fn as_str(&self) -> &str {
14        &self.0
15    }
16}
17
18impl From<&str> for SurfaceId {
19    fn from(s: &str) -> Self {
20        Self(s.to_owned())
21    }
22}
23
24impl From<String> for SurfaceId {
25    fn from(s: String) -> Self {
26        Self(s)
27    }
28}
29
30/// Unique identifier for a component within a surface.
31#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
32#[serde(transparent)]
33pub struct ComponentId(pub String);
34
35impl ComponentId {
36    pub fn new(id: impl Into<String>) -> Self {
37        Self(id.into())
38    }
39
40    pub fn as_str(&self) -> &str {
41        &self.0
42    }
43}
44
45impl From<&str> for ComponentId {
46    fn from(s: &str) -> Self {
47        Self(s.to_owned())
48    }
49}
50
51impl From<String> for ComponentId {
52    fn from(s: String) -> Self {
53        Self(s)
54    }
55}
56
57/// Unique identifier for a component catalog.
58#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
59#[serde(transparent)]
60pub struct CatalogId(pub String);
61
62impl CatalogId {
63    pub fn new(id: impl Into<String>) -> Self {
64        Self(id.into())
65    }
66
67    pub fn as_str(&self) -> &str {
68        &self.0
69    }
70}
71
72impl From<&str> for CatalogId {
73    fn from(s: &str) -> Self {
74        Self(s.to_owned())
75    }
76}
77
78impl From<String> for CatalogId {
79    fn from(s: String) -> Self {
80        Self(s)
81    }
82}
83
84/// The A2UI spec version this library supports.
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
86pub enum SpecVersion {
87    V0_8,
88    V0_9,
89}
90
91impl std::fmt::Display for SpecVersion {
92    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93        match self {
94            SpecVersion::V0_8 => write!(f, "v0.8"),
95            SpecVersion::V0_9 => write!(f, "v0.9"),
96        }
97    }
98}