Skip to main content

smbcloud_model/
flags.rs

1use serde::{Deserialize, Serialize};
2
3/// Who a flag is on for, besides everyone it is switched on for outright.
4#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
5pub struct FlagTargets {
6    #[serde(default)]
7    pub emails: Vec<String>,
8    #[serde(default)]
9    pub domains: Vec<String>,
10    /// A stable share of all users, 0 to 100.
11    #[serde(default)]
12    pub percent: u8,
13}
14
15/// A boolean smbCloud Flag, as the owner API returns it.
16#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
17pub struct Flag {
18    pub key: String,
19    pub name: String,
20    #[serde(default)]
21    pub description: String,
22    pub on: bool,
23    #[serde(default)]
24    pub targets: FlagTargets,
25}
26
27/// The fields to create or change. Fields left out are not sent, so an update
28/// only touches what was given.
29#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
30pub struct FlagInput {
31    #[serde(skip_serializing_if = "Option::is_none")]
32    pub key: Option<String>,
33    #[serde(skip_serializing_if = "Option::is_none")]
34    pub name: Option<String>,
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub description: Option<String>,
37    #[serde(skip_serializing_if = "Option::is_none")]
38    pub on: Option<bool>,
39    #[serde(skip_serializing_if = "FlagTargetsInput::is_empty")]
40    pub targets: FlagTargetsInput,
41}
42
43#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
44pub struct FlagTargetsInput {
45    #[serde(skip_serializing_if = "Option::is_none")]
46    pub emails: Option<Vec<String>>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub domains: Option<Vec<String>>,
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub percent: Option<u8>,
51}
52
53impl FlagTargetsInput {
54    pub fn is_empty(&self) -> bool {
55        self.emails.is_none() && self.domains.is_none() && self.percent.is_none()
56    }
57}
58
59#[derive(Debug, Deserialize)]
60pub struct FlagList {
61    pub flags: Vec<Flag>,
62}
63
64#[derive(Debug, Deserialize)]
65pub struct FlagEnvelope {
66    pub flag: Flag,
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72    use serde_json::json;
73
74    #[test]
75    fn a_flag_reads_the_api_shape() {
76        let flag: Flag = serde_json::from_value(json!({
77            "key": "beta", "name": "Beta", "description": "", "on": true,
78            "targets": { "emails": ["a@example.com"], "domains": [], "percent": 10 }
79        }))
80        .expect("flag");
81        assert_eq!(flag.targets.percent, 10);
82        assert_eq!(flag.targets.emails, vec!["a@example.com"]);
83    }
84
85    #[test]
86    fn an_update_sends_only_what_was_given() {
87        let input = FlagInput {
88            on: Some(false),
89            targets: FlagTargetsInput {
90                percent: Some(25),
91                ..Default::default()
92            },
93            ..Default::default()
94        };
95        assert_eq!(
96            serde_json::to_value(&input).expect("json"),
97            json!({ "on": false, "targets": { "percent": 25 } })
98        );
99        assert_eq!(
100            serde_json::to_value(FlagInput::default()).expect("json"),
101            json!({})
102        );
103    }
104}