1use crate::utils::clock::Instant;
4use tokio::task::JoinHandle;
5
6use super::client::ClientId;
7use super::server::ServerId;
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
11pub struct EasyId(u32);
12
13impl EasyId {
14 pub fn new(id: u32) -> Self {
16 Self(id)
17 }
18
19 pub fn as_u32(&self) -> u32 {
21 self.0
22 }
23
24 pub fn from_string(s: &str) -> Option<Self> {
26 let s = s.trim();
27 let id_str = s.strip_prefix("easy-").unwrap_or(s);
28 id_str.parse::<u32>().ok().map(Self)
29 }
30}
31
32impl std::fmt::Display for EasyId {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(f, "easy-{}", self.0)
35 }
36}
37
38#[derive(Debug, Clone, PartialEq, Eq)]
40pub enum EasyStatus {
41 Starting,
43 Running,
45 Stopped,
47 Error(String),
49}
50
51impl EasyStatus {
52 pub fn as_str(&self) -> &str {
53 match self {
54 Self::Starting => "Starting",
55 Self::Running => "Running",
56 Self::Stopped => "Stopped",
57 Self::Error(_) => "Error",
58 }
59 }
60}
61
62impl std::fmt::Display for EasyStatus {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 match self {
65 Self::Starting => write!(f, "Starting"),
66 Self::Running => write!(f, "Running"),
67 Self::Stopped => write!(f, "Stopped"),
68 Self::Error(msg) => write!(f, "Error: {}", msg),
69 }
70 }
71}
72
73#[derive(Debug)]
75pub struct EasyInstance {
76 pub id: EasyId,
78 pub protocol_name: String,
80 pub underlying_protocol: String,
82 pub user_instruction: Option<String>,
84 pub underlying_server_id: Option<ServerId>,
86 pub underlying_client_id: Option<ClientId>,
88 pub status: EasyStatus,
90 pub created_at: Instant,
92 pub status_changed_at: Instant,
94 pub handle: Option<JoinHandle<()>>,
96}
97
98impl EasyInstance {
99 pub fn new(
101 id: EasyId,
102 protocol_name: String,
103 underlying_protocol: String,
104 user_instruction: Option<String>,
105 ) -> Self {
106 let now = Instant::now();
107 Self {
108 id,
109 protocol_name,
110 underlying_protocol,
111 user_instruction,
112 underlying_server_id: None,
113 underlying_client_id: None,
114 status: EasyStatus::Starting,
115 created_at: now,
116 status_changed_at: now,
117 handle: None,
118 }
119 }
120
121 pub fn set_underlying_server(&mut self, server_id: ServerId) {
123 self.underlying_server_id = Some(server_id);
124 }
125
126 pub fn set_underlying_client(&mut self, client_id: ClientId) {
128 self.underlying_client_id = Some(client_id);
129 }
130
131 pub fn set_status(&mut self, status: EasyStatus) {
133 self.status = status;
134 self.status_changed_at = Instant::now();
135 }
136}