hivemind/core/
verification.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
4pub struct CheckConfig {
5 pub name: String,
6 pub command: String,
7 #[serde(default = "default_required")]
8 pub required: bool,
9 #[serde(default)]
10 pub timeout_ms: Option<u64>,
11}
12
13fn default_required() -> bool {
14 true
15}
16
17impl<'de> Deserialize<'de> for CheckConfig {
18 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
19 where
20 D: serde::Deserializer<'de>,
21 {
22 #[derive(Deserialize)]
23 #[serde(untagged)]
24 enum Wire {
25 String(String),
26 Object {
27 name: String,
28 command: String,
29 #[serde(default = "default_required")]
30 required: bool,
31 #[serde(default)]
32 timeout_ms: Option<u64>,
33 },
34 }
35
36 match Wire::deserialize(deserializer)? {
37 Wire::String(command) => Ok(Self {
38 name: command.clone(),
39 command,
40 required: true,
41 timeout_ms: None,
42 }),
43 Wire::Object {
44 name,
45 command,
46 required,
47 timeout_ms,
48 } => Ok(Self {
49 name,
50 command,
51 required,
52 timeout_ms,
53 }),
54 }
55 }
56}
57
58impl From<&str> for CheckConfig {
59 fn from(command: &str) -> Self {
60 Self {
61 name: command.to_string(),
62 command: command.to_string(),
63 required: true,
64 timeout_ms: None,
65 }
66 }
67}
68
69impl From<String> for CheckConfig {
70 fn from(command: String) -> Self {
71 Self {
72 name: command.clone(),
73 command,
74 required: true,
75 timeout_ms: None,
76 }
77 }
78}
79
80impl CheckConfig {
81 #[must_use]
82 pub fn new(name: impl Into<String>, command: impl Into<String>) -> Self {
83 Self {
84 name: name.into(),
85 command: command.into(),
86 required: true,
87 timeout_ms: None,
88 }
89 }
90
91 #[must_use]
92 pub fn optional(mut self) -> Self {
93 self.required = false;
94 self
95 }
96
97 #[must_use]
98 pub fn with_timeout_ms(mut self, timeout_ms: u64) -> Self {
99 self.timeout_ms = Some(timeout_ms);
100 self
101 }
102}
103
104#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
105pub struct CheckResult {
106 pub name: String,
107 pub passed: bool,
108 pub exit_code: i32,
109 pub output: String,
110 pub duration_ms: u64,
111 pub required: bool,
112}