wasmer_deploy_schema/
instance.rs1use std::sync::Arc;
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6use crate::schema::{AppMeta, DeployWorkloadToken, WorkloadV2};
7
8pub type RawToken = String;
10
11pub type NodeId = Uuid;
13
14#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, Hash)]
16pub struct InstanceId(Uuid);
17
18impl InstanceId {
19 pub fn new_random() -> Self {
20 Self(Uuid::new_v4())
21 }
22
23 pub fn to_uuid(&self) -> Uuid {
24 self.0
25 }
26
27 pub fn from_uuid(uuid: Uuid) -> Self {
28 Self(uuid)
29 }
30}
31
32impl std::fmt::Display for InstanceId {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(f, "{}", self.0)
35 }
36}
37
38impl std::str::FromStr for InstanceId {
39 type Err = uuid::Error;
40
41 fn from_str(s: &str) -> Result<Self, Self::Err> {
42 Ok(Self(Uuid::parse_str(s)?))
43 }
44}
45
46impl From<uuid::Uuid> for InstanceId {
47 fn from(uuid: uuid::Uuid) -> Self {
48 Self(uuid)
49 }
50}
51
52impl From<InstanceId> for uuid::Uuid {
53 fn from(id: InstanceId) -> Self {
54 id.0
55 }
56}
57
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct WorkloadMeta {
60 pub token: Option<DeployWorkloadToken>,
64
65 pub workload: WorkloadV2,
66
67 pub app_meta: Option<AppMeta>,
69}
70
71impl WorkloadMeta {
72 pub fn agent(&self) -> Option<&str> {
73 if let Some(tok) = &self.token {
74 Some(tok.data.subject())
75 } else {
76 None
77 }
78 }
79}
80
81#[derive(Debug, Clone)]
83pub struct InstanceMeta {
84 pub id: InstanceId,
86 pub workload: Arc<WorkloadMeta>,
88}
89
90impl InstanceMeta {
91 pub fn new(workload: Arc<WorkloadMeta>) -> Self {
92 Self {
93 id: InstanceId::new_random(),
94 workload,
95 }
96 }
97}