Skip to main content

sc_lint/
contract.rs

1use serde::Serialize;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub(crate) struct ServiceName(&'static str);
5
6impl ServiceName {
7    // Static command/service identifiers are fixed at compile time, so Cow
8    // would add generality without any real construction-site benefit here.
9    pub(crate) const fn new(value: &'static str) -> Self {
10        debug_assert!(!value.is_empty(), "service names must not be empty");
11        Self(value)
12    }
13
14    pub(crate) const fn as_str(&self) -> &str {
15        self.0
16    }
17}
18
19impl AsRef<str> for ServiceName {
20    fn as_ref(&self) -> &str {
21        self.as_str()
22    }
23}
24
25impl std::fmt::Display for ServiceName {
26    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
27        formatter.write_str(self.as_str())
28    }
29}
30
31#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
32pub struct CommandEnvelope<T>
33where
34    T: Serialize,
35{
36    pub ok: bool,
37    /// Commands are normalized to stable dotted identifiers so logs and machine envelopes share the same contract key.
38    pub command: String,
39    #[serde(skip_serializing_if = "Option::is_none")]
40    pub data: Option<T>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub error: Option<crate::CliError>,
43    #[serde(default)]
44    pub diagnostics: Vec<String>,
45}
46
47impl<T> CommandEnvelope<T>
48where
49    T: Serialize,
50{
51    pub fn success(command: &str, data: T) -> Self {
52        Self {
53            ok: true,
54            command: command.to_string(),
55            data: Some(data),
56            error: None,
57            diagnostics: Vec::new(),
58        }
59    }
60}
61
62impl CommandEnvelope<serde_json::Value> {
63    pub fn failure(command: &str, error: crate::CliError) -> Self {
64        Self {
65            ok: false,
66            command: command.to_string(),
67            data: None,
68            error: Some(error),
69            diagnostics: Vec::new(),
70        }
71    }
72}