Skip to main content

subc_control/
scope_served_by.rs

1//! Owner-only scope service history, separate from the exhaustive control enums.
2
3use serde::{Deserialize, Deserializer, Serialize, Serializer};
4
5pub const SCOPE_SERVED_BY_OP: &str = "scope.served_by";
6
7/// Ask about an epoch owned by the registered module sending the request.
8#[non_exhaustive]
9#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
10pub struct ScopeServedByRequest {
11    op: ScopeServedByOp,
12    pub scope_ref: String,
13    pub scope_epoch: u64,
14    pub module_id: String,
15}
16
17impl ScopeServedByRequest {
18    pub fn new(scope_ref: String, scope_epoch: u64, module_id: String) -> Self {
19        Self {
20            op: ScopeServedByOp::ServedBy,
21            scope_ref,
22            scope_epoch,
23            module_id,
24        }
25    }
26}
27
28/// Only `Served` establishes that the target acknowledged a scoped bind.
29#[non_exhaustive]
30#[derive(Debug, Clone, PartialEq, Eq)]
31pub enum ScopeServedByAnswer {
32    Served,
33    NotServed,
34    Unknown,
35    Other(String),
36}
37
38impl Serialize for ScopeServedByAnswer {
39    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
40        serializer.serialize_str(match self {
41            Self::Served => "served",
42            Self::NotServed => "not_served",
43            Self::Unknown => "unknown",
44            Self::Other(value) => value,
45        })
46    }
47}
48
49impl<'de> Deserialize<'de> for ScopeServedByAnswer {
50    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
51        Ok(match String::deserialize(deserializer)?.as_str() {
52            "served" => Self::Served,
53            "not_served" => Self::NotServed,
54            "unknown" => Self::Unknown,
55            other => Self::Other(other.to_owned()),
56        })
57    }
58}
59
60/// Volatile bind history in one daemon incarnation; unknown never means served.
61#[non_exhaustive]
62#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
63pub struct ScopeServedByReply {
64    op: ScopeServedByOp,
65    pub answer: ScopeServedByAnswer,
66    pub daemon_incarnation: String,
67}
68
69impl ScopeServedByReply {
70    pub fn new(answer: ScopeServedByAnswer, daemon_incarnation: String) -> Self {
71        Self {
72            op: ScopeServedByOp::ServedBy,
73            answer,
74            daemon_incarnation,
75        }
76    }
77}
78
79#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
80enum ScopeServedByOp {
81    #[serde(rename = "scope.served_by")]
82    ServedBy,
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn served_by_wire_is_open_but_requires_the_right_op() {
91        let request = ScopeServedByRequest::new("head".into(), 7, "tool".into());
92        assert_eq!(
93            serde_json::to_value(&request).unwrap(),
94            serde_json::json!({
95                "op": "scope.served_by", "scope_ref": "head", "scope_epoch": 7, "module_id": "tool"
96            })
97        );
98        for (wire, answer) in [
99            ("served", ScopeServedByAnswer::Served),
100            ("not_served", ScopeServedByAnswer::NotServed),
101            ("unknown", ScopeServedByAnswer::Unknown),
102            ("later", ScopeServedByAnswer::Other("later".into())),
103        ] {
104            let value = serde_json::json!({"op":"scope.served_by", "answer":wire, "daemon_incarnation":"d"});
105            let reply: ScopeServedByReply = serde_json::from_value(value.clone()).unwrap();
106            assert_eq!(reply.answer, answer);
107            assert_eq!(serde_json::to_value(reply).unwrap(), value);
108        }
109        assert!(serde_json::from_value::<ScopeServedByReply>(
110            serde_json::json!({"op":"scope.describe", "answer":"served", "daemon_incarnation":"d"})
111        )
112        .is_err());
113        assert!(serde_json::from_value::<ScopeServedByAnswer>(serde_json::json!(42)).is_err());
114    }
115}