veilid_core/veilid_api/types/
veilid_state.rs1use super::*;
2
3#[derive(Debug, PartialEq, Eq, Clone, Copy, Serialize, Deserialize, JsonSchema)]
5#[cfg_attr(
6 all(target_arch = "wasm32", target_os = "unknown"),
7 derive(Tsify),
8 tsify(namespace, from_wasm_abi, into_wasm_abi)
9)]
10#[must_use]
11pub enum AttachmentState {
12 Detached = 0,
13 Attaching = 1,
14 AttachedWeak = 2,
15 AttachedGood = 3,
16 AttachedStrong = 4,
17 FullyAttached = 5,
18 OverAttached = 6,
19 Detaching = 7,
20}
21impl AttachmentState {
22 #[must_use]
23 pub fn is_detached(&self) -> bool {
24 matches!(self, Self::Detached)
25 }
26 #[must_use]
27 pub fn is_attached(&self) -> bool {
28 matches!(
29 self,
30 Self::AttachedWeak
31 | Self::AttachedGood
32 | Self::AttachedStrong
33 | Self::FullyAttached
34 | Self::OverAttached
35 )
36 }
37}
38
39impl fmt::Display for AttachmentState {
40 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
41 let out = match self {
42 AttachmentState::Attaching => "attaching",
43 AttachmentState::AttachedWeak => "attached_weak",
44 AttachmentState::AttachedGood => "attached_good",
45 AttachmentState::AttachedStrong => "attached_strong",
46 AttachmentState::FullyAttached => "fully_attached",
47 AttachmentState::OverAttached => "over_attached",
48 AttachmentState::Detaching => "detaching",
49 AttachmentState::Detached => "detached",
50 };
51 write!(f, "{}", out)
52 }
53}
54
55impl TryFrom<String> for AttachmentState {
56 type Error = ();
57
58 fn try_from(s: String) -> Result<Self, Self::Error> {
59 AttachmentState::try_from(s.as_ref())
60 }
61}
62
63impl TryFrom<&str> for AttachmentState {
64 type Error = ();
65
66 fn try_from(s: &str) -> Result<Self, Self::Error> {
67 Ok(match s {
68 "attaching" => AttachmentState::Attaching,
69 "attached_weak" => AttachmentState::AttachedWeak,
70 "attached_good" => AttachmentState::AttachedGood,
71 "attached_strong" => AttachmentState::AttachedStrong,
72 "fully_attached" => AttachmentState::FullyAttached,
73 "over_attached" => AttachmentState::OverAttached,
74 "detaching" => AttachmentState::Detaching,
75 "detached" => AttachmentState::Detached,
76 _ => return Err(()),
77 })
78 }
79}
80
81#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
83#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
84#[must_use]
85pub struct VeilidStateAttachment {
86 pub state: AttachmentState,
88 pub public_internet_ready: bool,
91 pub local_network_ready: bool,
93 pub uptime: TimestampDuration,
95 pub attached_uptime: Option<TimestampDuration>,
97}
98
99#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
101#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
102#[must_use]
103pub struct PeerTableData {
104 #[schemars(with = "Vec<String>")]
106 #[cfg_attr(
107 all(target_arch = "wasm32", target_os = "unknown"),
108 tsify(type = "string[]")
109 )]
110 pub node_ids: Vec<TypedNodeId>,
111 pub peer_address: String,
113 pub peer_stats: PeerStats,
115}
116
117#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
119#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
120#[must_use]
121pub struct VeilidStateNetwork {
122 pub started: bool,
124 pub bps_down: ByteCount,
126 pub bps_up: ByteCount,
128 pub peers: Vec<PeerTableData>,
131}
132
133#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
135#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
136#[must_use]
137pub struct VeilidRouteChange {
138 #[schemars(with = "Vec<String>")]
140 pub dead_routes: Vec<RouteId>,
141 #[schemars(with = "Vec<String>")]
143 pub dead_remote_routes: Vec<RouteId>,
144}
145
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
151#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
152#[must_use]
153pub struct VeilidStateConfig {
154 pub config: VeilidConfig,
156}
157
158#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
160#[cfg_attr(all(target_arch = "wasm32", target_os = "unknown"), derive(Tsify))]
161#[must_use]
162pub struct VeilidValueChange {
163 #[schemars(with = "String")]
165 pub key: TypedRecordKey,
166 pub subkeys: ValueSubkeyRangeSet,
169 pub count: u32,
173 pub value: Option<ValueData>,
177}
178
179#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
182#[cfg_attr(
183 all(target_arch = "wasm32", target_os = "unknown"),
184 derive(Tsify),
185 tsify(into_wasm_abi)
186)]
187#[serde(tag = "kind")]
188#[must_use]
189pub enum VeilidUpdate {
190 Log(Box<VeilidLog>),
191 AppMessage(Box<VeilidAppMessage>),
192 AppCall(Box<VeilidAppCall>),
193 Attachment(Box<VeilidStateAttachment>),
194 Network(Box<VeilidStateNetwork>),
195 Config(Box<VeilidStateConfig>),
196 RouteChange(Box<VeilidRouteChange>),
197 ValueChange(Box<VeilidValueChange>),
198 Shutdown,
199}
200
201#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
203#[cfg_attr(
204 all(target_arch = "wasm32", target_os = "unknown"),
205 derive(Tsify),
206 tsify(into_wasm_abi)
207)]
208#[must_use]
209pub struct VeilidState {
210 pub attachment: Box<VeilidStateAttachment>,
211 pub network: Box<VeilidStateNetwork>,
212 pub config: Box<VeilidStateConfig>,
213}