zinit_client/
models.rs

1use chrono::{DateTime, Utc};
2use futures::Stream;
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::fmt;
6use std::pin::Pin;
7
8use crate::error::Result;
9
10/// Service state
11#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
12#[serde(rename_all = "PascalCase")]
13pub enum ServiceState {
14    /// Service state is unknown
15    Unknown,
16    /// Service is blocked by dependencies
17    Blocked,
18    /// Service is spawned but not yet running
19    Spawned,
20    /// Service is running
21    Running,
22    /// Service completed successfully (for oneshot services)
23    Success,
24    /// Service exited with an error
25    Error,
26    /// Service test command failed
27    TestFailure,
28}
29
30impl fmt::Display for ServiceState {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match self {
33            ServiceState::Unknown => write!(f, "Unknown"),
34            ServiceState::Blocked => write!(f, "Blocked"),
35            ServiceState::Spawned => write!(f, "Spawned"),
36            ServiceState::Running => write!(f, "Running"),
37            ServiceState::Success => write!(f, "Success"),
38            ServiceState::Error => write!(f, "Error"),
39            ServiceState::TestFailure => write!(f, "TestFailure"),
40        }
41    }
42}
43
44/// Service target state
45#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
46#[serde(rename_all = "PascalCase")]
47pub enum ServiceTarget {
48    /// Service should be running
49    Up,
50    /// Service should be stopped
51    Down,
52}
53
54impl fmt::Display for ServiceTarget {
55    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
56        match self {
57            ServiceTarget::Up => write!(f, "Up"),
58            ServiceTarget::Down => write!(f, "Down"),
59        }
60    }
61}
62
63/// Service status information
64#[derive(Debug, Clone, Serialize, Deserialize)]
65pub struct ServiceStatus {
66    /// Service name
67    pub name: String,
68    /// Process ID (0 if not running)
69    pub pid: u32,
70    /// Current state
71    pub state: ServiceState,
72    /// Target state
73    pub target: ServiceTarget,
74    /// Dependencies and their states
75    pub after: HashMap<String, String>,
76}
77
78/// Log entry from a service
79#[derive(Debug, Clone, Serialize, Deserialize)]
80pub struct LogEntry {
81    /// Timestamp of the log entry
82    pub timestamp: DateTime<Utc>,
83    /// Service that generated the log
84    pub service: String,
85    /// Log message
86    pub message: String,
87}
88
89/// Stream of log entries
90pub struct LogStream {
91    /// Inner stream of log entries
92    pub(crate) inner: Pin<Box<dyn Stream<Item = Result<LogEntry>> + Send>>,
93}
94
95impl Stream for LogStream {
96    type Item = Result<LogEntry>;
97
98    fn poll_next(
99        mut self: Pin<&mut Self>,
100        cx: &mut std::task::Context<'_>,
101    ) -> std::task::Poll<Option<Self::Item>> {
102        Pin::new(&mut self.inner).poll_next(cx)
103    }
104}
105
106/// Response from the Zinit API
107#[derive(Debug, Clone, Deserialize)]
108pub(crate) struct Response {
109    /// Response state (ok or error)
110    pub state: ResponseState,
111    /// Response body
112    pub body: serde_json::Value,
113}
114
115/// Response state
116#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
117#[serde(rename_all = "lowercase")]
118pub(crate) enum ResponseState {
119    /// Success response
120    Ok,
121    /// Error response
122    Error,
123}