1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
use serde::ser::{SerializeStruct, Serializer};
use serde::{Serialize};

#[derive(Clone, Debug)]
pub enum Value {
    String(String),
    Integer(i64),
    Boolean(bool),
}

/// An stringly typed option that is passed to
#[derive(Clone, Debug)]
pub struct ConfigOption {
    name: String,
    pub(crate) value: Option<Value>,
    default: Value,
    description: String,
}

impl ConfigOption {
    pub fn name(&self) -> &str {
        &self.name
    }
    pub fn default(&self) -> &Value {
        &self.default
    }
}

// When we serialize we don't add the value. This is because we only
// ever serialize when we pass the option back to lightningd during
// the getmanifest call.
impl Serialize for ConfigOption {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut s = serializer.serialize_struct("ConfigOption", 4)?;
        s.serialize_field("name", &self.name)?;
        match &self.default {
            Value::String(ss) => {
                s.serialize_field("type", "string")?;
                s.serialize_field("default", ss)?;
            }
            Value::Integer(i) => {
                s.serialize_field("type", "int")?;
                s.serialize_field("default", i)?;
            }

            Value::Boolean(b) => {
                s.serialize_field("type", "bool")?;
                s.serialize_field("default", b)?;
            }
        }

        s.serialize_field("description", &self.description)?;
        s.end()
    }
}
impl ConfigOption {
    pub fn new(name: &str, default: Value, description: &str) -> Self {
        Self {
            name: name.to_string(),
            default,
            description: description.to_string(),
            value: None,
        }
    }

    pub fn value(&self) -> Value {
        match &self.value {
            None => self.default.clone(),
            Some(v) => v.clone(),
        }
    }

    pub fn description(&self) -> String {
        self.description.clone()
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_option_serialize() {
        let tests = vec![
            (
                ConfigOption::new("name", Value::String("default".to_string()), "description"),
                json!({
                "name": "name",
                        "description":"description",
                        "default": "default",
                        "type": "string",
                    }),
            ),
            (
                ConfigOption::new("name", Value::Integer(42), "description"),
                json!({
                "name": "name",
                        "description":"description",
                        "default": 42,
                        "type": "int",
                    }),
            ),
            (
                ConfigOption::new("name", Value::Boolean(true), "description"),
                json!({
                "name": "name",
                        "description":"description",
                        "default": true,
                        "type": "bool",
                    }),
            ),
        ];

        for (input, expected) in tests.iter() {
            let res = serde_json::to_value(input).unwrap();
            assert_eq!(&res, expected);
        }
    }
}