Skip to main content

mant_protocol/
update.rs

1//! Process result contracts for explicit cache mutations.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6/// How an explicit tldr cache refresh changed local state.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
8#[serde(rename_all = "kebab-case")]
9pub enum TldrCacheAction {
10    /// A cache did not exist and was cloned.
11    Cloned,
12    /// An existing cache advanced or was refreshed.
13    Updated,
14}
15
16/// Result of an explicit `mant --update-tldr` operation.
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
18#[serde(rename_all = "camelCase")]
19pub struct TldrCacheUpdate {
20    /// Mutation performed by the client-specific update path.
21    pub action: TldrCacheAction,
22    /// Updated cache directory, when the client exposes it.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub cache_dir: Option<String>,
25    /// External tldr client used for the operation, when applicable.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub client: Option<String>,
28    /// Trimmed human-readable client output, when useful.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub output: Option<String>,
31    /// Resulting cache revision, when discoverable.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub revision: Option<String>,
34}
35
36#[cfg(test)]
37mod tests {
38    use serde_json::json;
39
40    use super::{TldrCacheAction, TldrCacheUpdate};
41
42    #[test]
43    fn cache_update_uses_a_stable_camel_case_shape() {
44        let update = TldrCacheUpdate {
45            action: TldrCacheAction::Cloned,
46            cache_dir: Some("/cache/mant/tldr-pages".to_owned()),
47            client: None,
48            output: None,
49            revision: Some("abc123".to_owned()),
50        };
51
52        assert_eq!(
53            serde_json::to_value(update).expect("serialize update"),
54            json!({
55                "action": "cloned",
56                "cacheDir": "/cache/mant/tldr-pages",
57                "revision": "abc123"
58            })
59        );
60    }
61}