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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Author: D.S. Ljungmark <spider@skuggor.se>, Modio AB
// SPDX-License-Identifier: AGPL-3.0-or-later
use std::collections::HashMap;

#[derive(Debug)]
pub struct Sensor {
    #[allow(dead_code)]
    pub(crate) name: String,
    #[allow(dead_code)]
    pub(crate) s_id: i64,
}

#[derive(Debug)]
pub struct Metric {
    pub name: String,
    pub value: String,
    pub time: i64,
}

impl From<Metric> for fsipc::legacy::Measure {
    fn from(f: Metric) -> Self {
        // Legacy type was u64, but we want i64 in the database due to SQLite.
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)]
        Self {
            key: f.name,
            value: f.value,
            timestamp: f.time as u64,
        }
    }
}
impl From<fsipc::legacy::Measure> for Metric {
    fn from(f: fsipc::legacy::Measure) -> Self {
        // Legacy type for timestamp was u64, but we want i64 in the database due to SQLite.
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)]
        Self {
            name: f.key,
            value: f.value,
            time: f.timestamp as i64,
        }
    }
}

#[derive(Debug)]
pub struct TXMetric {
    pub(crate) id: i64,
    pub(crate) name: String,
    pub(crate) value: String,
    pub(crate) time: i64,
}

impl From<TXMetric> for fsipc::legacy::PreparedPoint {
    fn from(f: TXMetric) -> Self {
        // Once again i64 vs u64 for a timestamp
        #[allow(clippy::cast_sign_loss)]
        Self {
            id: f.id,
            key: f.name,
            value: f.value,
            timestamp: f.time as u64,
        }
    }
}

#[derive(Debug)]
pub struct Transaction {
    pub(crate) name: String,
    pub(crate) expected: String,
    pub(crate) target: String,
    pub(crate) t_id: i64,
    #[allow(dead_code)]
    pub(crate) status: String,
}

impl From<Transaction> for fsipc::legacy::Transaction {
    fn from(f: Transaction) -> Self {
        // t_id is a number, should be i64 according to sqlite, but was u64 on our wire
        // interface.
        #[allow(clippy::cast_sign_loss, clippy::cast_possible_wrap)]
        Self {
            key: f.name,
            expected: f.expected,
            target: f.target,
            t_id: f.t_id as u64,
        }
    }
}

pub type ValueMap = HashMap<u32, String>;

#[derive(Debug, Eq, PartialEq)]
pub enum SensorMode {
    ReadOnly,
    ReadWrite,
    WriteOnly,
}
impl SensorMode {
    // Convenience function for stringification
    #[must_use]
    pub const fn as_str(&self) -> &str {
        match self {
            SensorMode::ReadOnly => "ro",
            SensorMode::ReadWrite => "rw",
            SensorMode::WriteOnly => "wo",
        }
    }
    // Map a string to a value, wrapped in an option.
    // Makes the code that reads from the database easier, and keeps the string reps in this
    // enum impl
    #[must_use]
    pub fn maybe_from_str(value: &str) -> Option<Self> {
        match value {
            "ro" => Some(Self::ReadOnly),
            "rw" => Some(Self::ReadWrite),
            "wo" => Some(Self::WriteOnly),
            _ => None,
        }
    }
}

impl From<fsipc::logger1::SensorMode> for SensorMode {
    fn from(other: fsipc::logger1::SensorMode) -> SensorMode {
        match other {
            fsipc::logger1::SensorMode::ReadOnly => SensorMode::ReadOnly,
            fsipc::logger1::SensorMode::ReadWrite => SensorMode::ReadWrite,
            fsipc::logger1::SensorMode::WriteOnly => SensorMode::WriteOnly,
        }
    }
}

// fsipc does not import this type for obvious reasons.
// thus we can only implement "Into" but not "From"
#[allow(clippy::from_over_into)]
impl Into<fsipc::logger1::SensorMode> for SensorMode {
    fn into(self) -> fsipc::logger1::SensorMode {
        match self {
            SensorMode::ReadOnly => fsipc::logger1::SensorMode::ReadOnly,
            SensorMode::ReadWrite => fsipc::logger1::SensorMode::ReadWrite,
            SensorMode::WriteOnly => fsipc::logger1::SensorMode::WriteOnly,
        }
    }
}

#[test]
fn enum_roundtrip() {
    fn around(b: &SensorMode) -> SensorMode {
        let sval = b.as_str();
        SensorMode::maybe_from_str(sval).unwrap()
    }
    assert_eq!(SensorMode::ReadOnly, around(&SensorMode::ReadOnly));
    assert_eq!(SensorMode::ReadWrite, around(&SensorMode::ReadWrite));
    assert_eq!(SensorMode::WriteOnly, around(&SensorMode::WriteOnly));
}

#[derive(Debug)]
pub struct Metadata {
    pub n: String,
    pub u: Option<String>,
    pub name: Option<String>,
    pub description: Option<String>,
    pub value_map: Option<ValueMap>,
    pub mode: Option<SensorMode>,
}

#[derive(Debug)]
pub struct MetadataBuilder {
    n: String,
    u: Option<String>,
    name: Option<String>,
    description: Option<String>,
    value_map: Option<ValueMap>,
    mode: Option<SensorMode>,
}

impl Metadata {
    #[must_use]
    pub fn builder(n: String) -> MetadataBuilder {
        MetadataBuilder::new(n)
    }
}

/// Metadata builder, used to slim down code that creates a Metadata object
impl MetadataBuilder {
    #[must_use]
    pub fn new(n: String) -> Self {
        Self {
            n,
            u: None,
            name: None,
            description: None,
            value_map: None,
            mode: None,
        }
    }

    #[must_use]
    pub fn name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }
    #[must_use]
    pub fn unit(mut self, u: String) -> Self {
        self.u = Some(u);
        self
    }
    #[must_use]
    pub fn description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    #[must_use]
    pub fn value_map(mut self, value_map: ValueMap) -> Self {
        self.value_map = Some(value_map);
        self
    }

    #[must_use]
    pub fn mode(mut self, mode: SensorMode) -> Self {
        self.mode = Some(mode);
        self
    }

    #[must_use]
    pub fn mode_string(mut self, value: &str) -> Self {
        self.mode = SensorMode::maybe_from_str(value);
        self
    }

    /// Parse a value map from a string.
    /// Does not return errors as it is assumed that the consumer of such an error would not be
    /// happy.
    #[must_use]
    pub fn value_map_string(mut self, value: &str) -> Self {
        let value_map = serde_json::from_str::<ValueMap>(value);
        if value_map.is_err() {
            log::error!("Error decoding json: {:?}", value);
        };
        self.value_map = value_map.ok();
        self
    }

    #[must_use]
    pub fn from_pair(self, tag: &str, value: String) -> Self {
        match tag {
            "unit" => self.unit(value),
            "description" => self.description(value),
            "name" => self.name(value),
            "enum" => self.value_map_string(&value),
            "mode" => self.mode_string(&value),
            _ => {
                log::error!("Strange decoding from_pair? {:?} {:?}", tag, &value);
                self
            }
        }
    }

    #[must_use]
    pub fn build(self) -> Metadata {
        let Self {
            n,
            u,
            name,
            description,
            value_map,
            mode,
        } = self;
        Metadata {
            n,
            u,
            name,
            description,
            value_map,
            mode,
        }
    }
}