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
use std::{fmt, time::Duration};

// TODO: Make `scalar` module public instead of renaming and re-exporting all types?
mod scalar;
pub use self::scalar::{Type as ScalarType, Value as ScalarValue};

pub trait ToValueType {
    fn to_value_type(&self) -> ValueType;
}

/// Enumeration of value types
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ValueType {
    /// Scalar type
    Scalar(ScalarType),

    /// Time duration, e.g. a timeout
    Duration,

    /// Text data
    String,

    /// Binary data
    Bytes,
}

// TODO: Use short identifiers?
const TYPE_STR_DURATION: &str = "duration";
const TYPE_STR_STRING: &str = "string";
const TYPE_STR_BYTES: &str = "bytes";

impl ValueType {
    #[must_use]
    pub const fn to_scalar(self) -> Option<ScalarType> {
        match self {
            Self::Scalar(s) => Some(s),
            _ => None,
        }
    }

    #[must_use]
    pub const fn is_scalar(self) -> bool {
        self.to_scalar().is_some()
    }

    #[must_use]
    pub const fn from_scalar(scalar: ScalarType) -> Self {
        Self::Scalar(scalar)
    }

    const fn as_str(self) -> &'static str {
        match self {
            Self::Scalar(s) => s.as_str(),
            Self::Duration => TYPE_STR_DURATION,
            Self::String => TYPE_STR_STRING,
            Self::Bytes => TYPE_STR_BYTES,
        }
    }

    #[must_use]
    pub fn try_from_str(s: &str) -> Option<Self> {
        ScalarType::try_from_str(s).map(Into::into).or(match s {
            TYPE_STR_DURATION => Some(Self::Duration),
            TYPE_STR_STRING => Some(Self::String),
            TYPE_STR_BYTES => Some(Self::Bytes),
            _ => None,
        })
    }
}

impl From<ScalarType> for ValueType {
    fn from(from: ScalarType) -> Self {
        Self::from_scalar(from)
    }
}

impl fmt::Display for ValueType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// A value representation within a MSR system.
///
/// TODO: Split into a separate type for simple, copyable values and
/// an enclosing type that includes the complex, non-real-time-safe
/// values?
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    /// Scalar value (real-time safe)
    ///
    /// This variant can safely be used in real-time contexts.
    Scalar(ScalarValue),

    /// Duration, e.g. a timeout
    ///
    /// This variant can safely be used in real-time contexts.
    Duration(Duration),

    /// Variable-size text data
    ///
    /// This variant must not be used in real-time contexts.
    String(String),

    /// Variable-size binary data
    ///
    /// This variant must not be used in real-time contexts.
    Bytes(Vec<u8>),
}

impl From<Duration> for Value {
    fn from(from: Duration) -> Value {
        Self::Duration(from)
    }
}

impl From<String> for Value {
    fn from(from: String) -> Value {
        Self::String(from)
    }
}

impl From<Vec<u8>> for Value {
    fn from(from: Vec<u8>) -> Value {
        Self::Bytes(from)
    }
}

impl Value {
    #[must_use]
    pub const fn to_type(&self) -> ValueType {
        match self {
            Self::Scalar(value) => ValueType::Scalar(value.to_type()),
            Self::Duration(_) => ValueType::Duration,
            Self::String(_) => ValueType::String,
            Self::Bytes(_) => ValueType::Bytes,
        }
    }

    #[must_use]
    pub const fn to_scalar(&self) -> Option<ScalarValue> {
        match self {
            Self::Scalar(scalar) => Some(*scalar),
            _ => None,
        }
    }

    #[must_use]
    pub const fn from_scalar(scalar: ScalarValue) -> Self {
        Self::Scalar(scalar)
    }

    pub fn to_i32(&self) -> Option<i32> {
        self.to_scalar().and_then(ScalarValue::to_i32)
    }

    pub fn to_u32(&self) -> Option<u32> {
        self.to_scalar().and_then(ScalarValue::to_u32)
    }

    pub fn to_i64(&self) -> Option<i64> {
        self.to_scalar().and_then(ScalarValue::to_i64)
    }

    pub fn to_u64(&self) -> Option<u64> {
        self.to_scalar().and_then(ScalarValue::to_u64)
    }

    pub fn to_f32(&self) -> Option<f32> {
        self.to_scalar().and_then(ScalarValue::to_f32)
    }

    pub fn to_f64(&self) -> Option<f64> {
        self.to_scalar().and_then(ScalarValue::to_f64)
    }
}

impl ToValueType for Value {
    fn to_value_type(&self) -> ValueType {
        self.to_type()
    }
}

impl<S> From<S> for Value
where
    S: Into<ScalarValue>,
{
    fn from(from: S) -> Self {
        Value::Scalar(from.into())
    }
}