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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
use std::collections::HashMap;

pub use serde::{Deserialize, Serialize};
pub use settings_schema_derive::SettingsSchema;

// For the derive macro
pub use serde_json::to_value as to_json_value;

/// The `Switch` is used to represent something that makes sense to specify its state only when it's enabled.
/// This should be used differently than `Option(al)`, that represent a value that can be omitted.
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
#[serde(tag = "state", content = "content")]
pub enum Switch<T> {
    Enabled(T),
    Disabled,
}

impl<T> Switch<T> {
    pub fn as_option(&self) -> Option<&T> {
        match self {
            Self::Enabled(t) => Some(t),
            Self::Disabled => None,
        }
    }

    pub fn into_option(self) -> Option<T> {
        match self {
            Self::Enabled(t) => Some(t),
            Self::Disabled => None,
        }
    }
}

/// Type used to specify the default value for type `Option`.  
/// It allows specifying the set state and its content when it is set.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct OptionalDefault<C> {
    pub set: bool,
    pub content: C,
}

/// Type used to specify the default value for type `Switch`.  
/// It allows setting the enabled state and its content when set to enabled.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct SwitchDefault<C> {
    pub enabled: bool,
    pub content: C,
}

/// Type used to specify the default value for type `Vec`.  
/// It allows setting the default for the vector (all elements) and the default value for new elements.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct VectorDefault<T> {
    pub element: T,
    pub content: Vec<T>,
}

/// Type used to specify the default value for type `Vec<(String, X)>`.  
/// It allows setting the default for the dictionary (all entries) and the default key and value for new entries.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct DictionaryDefault<T> {
    pub key: String,
    pub value: T,
    pub content: Vec<(String, T)>,
}

/// GUI type associated to a numeric node.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum NumericGuiType {
    TextBox,
    UpDown,
    Slider,
}

/// GUI type associated to the choice node.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum ChoiceControlType {
    Dropdown,
    ButtonGroup,
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct NamedEntry<T> {
    pub name: String,
    pub strings: HashMap<String, String>,
    pub content: T,
}

/// Schema base type returned by `<YourStructOrEnum>::schema()`, generated by the macro
/// `#[derive(SettingsSchema)]`. It can be used as is (for Rust based GUIs) or it can be serialized
/// to JSON for creating GUIs in other languages.
#[non_exhaustive]
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(tag = "type", content = "content")]
pub enum SchemaNode {
    Section(Vec<NamedEntry<SchemaNode>>),
    Choice {
        default: String,
        variants: Vec<NamedEntry<Option<SchemaNode>>>,
        gui: Option<ChoiceControlType>,
    },
    Optional {
        default_set: bool,
        content: Box<SchemaNode>,
    },
    Switch {
        default_enabled: bool,
        content: Box<SchemaNode>,
    },
    Boolean {
        default: bool,
    },
    Integer {
        default: i128,
        min: Option<i128>,
        max: Option<i128>,
        step: Option<i128>,
        gui: Option<NumericGuiType>,
    },
    Float {
        default: f64,
        min: Option<f64>,
        max: Option<f64>,
        step: Option<f64>,
        gui: Option<NumericGuiType>,
    },
    Text {
        default: String,
    },
    Array(Vec<SchemaNode>),
    Vector {
        default_element: Box<SchemaNode>,
        default: serde_json::Value,
    },
    Dictionary {
        default_key: String,
        default_value: Box<SchemaNode>,
        default: serde_json::Value,
    },
}