concord_client/
model.rs

1use std::{fmt::Debug, num::ParseIntError, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4use uuid::Uuid;
5
6pub static USER_AGENT_VALUE: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
7
8#[derive(Debug, Serialize, Deserialize)]
9pub struct SessionToken(String);
10
11impl TryFrom<&SessionToken> for http::HeaderValue {
12    type Error = http::header::InvalidHeaderValue;
13
14    fn try_from(value: &SessionToken) -> Result<Self, Self::Error> {
15        http::HeaderValue::from_str(&value.0)
16    }
17}
18
19#[derive(Clone, Serialize, Deserialize)]
20pub struct ApiToken(String);
21
22impl ApiToken {
23    pub fn new(v: String) -> Self {
24        Self(v)
25    }
26}
27
28impl Debug for ApiToken {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        f.debug_tuple("ApiToken").field(&"********").finish()
31    }
32}
33
34impl TryFrom<&ApiToken> for http::HeaderValue {
35    type Error = http::header::InvalidHeaderValue;
36
37    fn try_from(value: &ApiToken) -> Result<Self, Self::Error> {
38        http::HeaderValue::from_str(&value.0)
39    }
40}
41
42#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
43pub struct ProcessId(Uuid);
44
45impl ProcessId {
46    pub fn new(v: Uuid) -> Self {
47        Self(v)
48    }
49
50    pub fn uuid(&self) -> &Uuid {
51        &self.0
52    }
53}
54
55impl std::fmt::Display for ProcessId {
56    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57        write!(f, "{}", self.0)
58    }
59}
60
61#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
62pub struct AgentId(Uuid);
63
64impl AgentId {
65    pub fn new(v: Uuid) -> Self {
66        Self(v)
67    }
68}
69
70impl std::fmt::Display for AgentId {
71    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72        write!(f, "{}", self.0)
73    }
74}
75
76impl FromStr for AgentId {
77    type Err = uuid::Error;
78
79    fn from_str(s: &str) -> Result<Self, Self::Err> {
80        let v = Uuid::try_parse(s)?;
81        Ok(Self(v))
82    }
83}
84
85impl TryFrom<&AgentId> for http::HeaderValue {
86    type Error = http::header::InvalidHeaderValue;
87
88    fn try_from(value: &AgentId) -> Result<Self, Self::Error> {
89        http::HeaderValue::from_str(&value.0.to_string())
90    }
91}
92
93#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
94pub struct OrganizationId(Uuid);
95
96#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
97pub struct ProjectId(Uuid);
98
99#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
100pub struct RepositoryId(Uuid);
101
102#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
103pub struct UserId(Uuid);
104
105#[derive(Debug, Serialize, Deserialize)]
106#[serde(rename_all = "UPPERCASE")]
107pub enum ProcessStatus {
108    New,
109    Preparing,
110    Enqueued,
111    Waiting,
112    Starting,
113    Running,
114    Suspended,
115    Resuming,
116    Finished,
117    Failed,
118    Cancelled,
119    TimedOut,
120}
121
122impl std::fmt::Display for ProcessStatus {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        match self {
125            ProcessStatus::New => write!(f, "NEW"),
126            ProcessStatus::Preparing => write!(f, "PREPARING"),
127            ProcessStatus::Enqueued => write!(f, "ENQUEUED"),
128            ProcessStatus::Waiting => write!(f, "WAITING"),
129            ProcessStatus::Starting => write!(f, "STARTING"),
130            ProcessStatus::Running => write!(f, "RUNNING"),
131            ProcessStatus::Suspended => write!(f, "SUSPENDED"),
132            ProcessStatus::Resuming => write!(f, "RESUMING"),
133            ProcessStatus::Finished => write!(f, "FINISHED"),
134            ProcessStatus::Failed => write!(f, "FAILED"),
135            ProcessStatus::Cancelled => write!(f, "CANCELLED"),
136            ProcessStatus::TimedOut => write!(f, "TIMED_OUT"),
137        }
138    }
139}
140
141#[derive(Debug, Deserialize)]
142#[serde(rename_all = "UPPERCASE")]
143pub enum ProcessKind {
144    Default,
145    FailureHandler,
146    CancelHandler,
147    TimeoutHandler,
148}
149
150#[derive(Debug, Deserialize)]
151#[serde(rename_all = "camelCase")]
152pub struct StartProcessResponse {
153    pub instance_id: ProcessId,
154}
155
156#[derive(Debug, Deserialize)]
157#[serde(rename_all = "camelCase")]
158pub struct ProcessEntry {
159    pub instance_id: ProcessId,
160    pub parent_instance_id: Option<ProcessId>,
161    pub status: ProcessStatus,
162    pub kind: ProcessKind,
163    pub org_id: Option<OrganizationId>,
164    pub org_name: Option<String>,
165    pub project_id: Option<ProjectId>,
166    pub project_name: Option<String>,
167    pub repo_id: Option<RepositoryId>,
168    pub repo_name: Option<String>,
169    pub repo_url: Option<String>,
170    pub repo_path: Option<String>,
171    pub commit_id: Option<String>,
172    pub commit_branch: Option<String>,
173    // TODO
174    // #[serde(with = "iso8601")]
175    // pub created_at: OffsetDateTime,
176    pub initiator: Option<String>,
177    pub initiator_id: Option<UserId>,
178    pub last_agent_id: Option<String>,
179    // TODO
180    // #[serde(with = "iso8601::option")]
181    // pub start_at: Option<OffsetDateTime>,
182    // TODO
183    // #[serde(with = "iso8601::option")]
184    // pub last_updated_at: Option<OffsetDateTime>,
185    // TODO
186    // #[serde(with = "iso8601::option")]
187    // pub last_run_at: Option<OffsetDateTime>,
188    pub total_runtime_ms: Option<u64>,
189    pub tags: Option<Vec<String>>,
190    pub children_ids: Option<Vec<ProcessId>>,
191    // TODO meta
192    pub handlers: Option<Vec<String>>,
193    // TODO requirements
194    pub disabled: bool,
195    // TODO checkpoints
196    // TODO checkpointRestoreHistory
197    // TODO statusHistory
198    // TODO triggeredBy
199    pub timeout: Option<u64>,
200    pub suspend_timeout: Option<u64>,
201    pub runtime: Option<String>,
202}
203
204#[derive(Debug, Serialize, Deserialize)]
205pub struct SegmentCorrelationId(Uuid);
206
207impl SegmentCorrelationId {
208    pub fn new(v: Uuid) -> Self {
209        Self(v)
210    }
211}
212
213#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
214pub struct LogSegmentId(i64);
215
216impl LogSegmentId {
217    pub fn new(v: i64) -> Self {
218        Self(v)
219    }
220}
221
222impl FromStr for LogSegmentId {
223    type Err = ParseIntError;
224
225    fn from_str(s: &str) -> Result<Self, Self::Err> {
226        Ok(LogSegmentId::new(s.parse()?))
227    }
228}
229
230impl std::fmt::Display for LogSegmentId {
231    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
232        write!(f, "{}", self.0)
233    }
234}
235
236#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
237#[serde(rename_all = "UPPERCASE")]
238pub enum LogSegmentStatus {
239    Ok,
240    Failed,
241    Running,
242    Suspended,
243}
244
245#[derive(Debug, Serialize, Deserialize)]
246pub struct LogSegmentRequest {
247    #[serde(rename = "correlationId")]
248    pub correlation_id: SegmentCorrelationId,
249    pub name: String,
250}
251
252#[derive(Debug, Serialize, Deserialize)]
253pub struct LogSegmentOperationResponse {
254    pub id: LogSegmentId,
255}
256
257#[derive(Debug, Default, Serialize, Deserialize)]
258pub struct LogSegmentUpdateRequest {
259    pub status: Option<LogSegmentStatus>,
260    pub warnings: Option<u16>,
261    pub errors: Option<u16>,
262}