Skip to main content

openleadr_wire/
values_map.rs

1//! Helper types to realize type values relations
2
3use serde::{Deserialize, Serialize};
4
5/// ValuesMap : Represents one or more values associated with a type.
6///
7/// See enumerations in Definitions for defined string values, or use privately defined strings
8
9#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
10pub struct ValuesMap {
11    /// Represents the nature of values.
12    ///
13    /// See enumerations in Definitions for defined string values, or use privately defined strings
14    #[serde(rename = "type")]
15    pub value_type: ValueType,
16    /// A list of data points. Most often a singular value such as a price.
17    pub values: Vec<Value>,
18}
19
20#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
21pub struct ValueType(
22    #[serde(deserialize_with = "crate::string_within_range_inclusive::<1, 128, _>")] pub String,
23);
24
25#[derive(Clone, Debug, Serialize, Deserialize)]
26#[serde(untagged)]
27pub enum Value {
28    Integer(i64),
29    Number(f64),
30    Boolean(bool),
31    Point(Point),
32    String(String),
33}
34
35impl PartialEq for Value {
36    fn eq(&self, other: &Self) -> bool {
37        match (self, other) {
38            (Self::Integer(s), Self::Integer(o)) => s == o,
39            (Self::Boolean(s), Self::Boolean(o)) => s == o,
40            (Self::Point(s), Self::Point(o)) => s == o,
41            (Self::String(s), Self::String(o)) => s == o,
42            (Self::Number(s), Self::Number(o)) if s.is_nan() && o.is_nan() => true,
43            (Self::Number(s), Self::Number(o)) => s == o,
44            _ => false,
45        }
46    }
47}
48
49impl Eq for Value {}
50
51#[derive(Clone, Debug, PartialEq, Default, Serialize, Deserialize)]
52pub struct Point {
53    /// A value on an x axis.
54    pub x: f32,
55    /// A value on a y axis.
56    pub y: f32,
57}
58
59impl Point {
60    pub fn new(x: f32, y: f32) -> Self {
61        Self { x, y }
62    }
63}