use std::borrow::Cow;
#[cfg(feature = "chrono")]
use chrono::{DateTime, Utc};
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Invalid,
SInt(i64),
UInt(u64),
Float(f64),
String(String),
Bytes(Vec<u8>),
Bool(bool),
Enum(Cow<'static, str>),
#[cfg(feature = "chrono")]
DateTime(DateTime<Utc>),
#[cfg(not(feature = "chrono"))]
DateTime(u32),
Array(Vec<Value>),
}
impl Value {
pub fn is_invalid(&self) -> bool {
matches!(self, Value::Invalid)
}
pub fn as_f64(&self) -> Option<f64> {
match self {
Value::Float(v) => Some(*v),
Value::UInt(v) => Some(*v as f64),
Value::SInt(v) => Some(*v as f64),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match self {
Value::SInt(v) => Some(*v),
Value::UInt(v) => Some(*v as i64),
_ => None,
}
}
pub fn as_u64(&self) -> Option<u64> {
match self {
Value::UInt(v) => Some(*v),
Value::SInt(v) if *v >= 0 => Some(*v as u64),
_ => None,
}
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::String(s) => Some(s.as_str()),
Value::Enum(s) => Some(s),
_ => None,
}
}
#[cfg(feature = "chrono")]
pub fn as_datetime(&self) -> Option<DateTime<Utc>> {
match self {
Value::DateTime(d) => Some(*d),
_ => None,
}
}
#[cfg(not(feature = "chrono"))]
pub fn as_datetime(&self) -> Option<u32> {
match self {
Value::DateTime(s) => Some(*s),
_ => None,
}
}
}
#[derive(Debug, Clone)]
pub struct Field {
pub name: String,
pub kind: FieldKind,
pub value: Value,
pub units: Option<String>,
}
#[derive(Debug, Clone, Copy)]
pub enum FieldKind {
Standard {
field_def_num: u8,
},
Developer {
field_def_num: u8,
developer_data_index: u8,
},
}
#[derive(Debug, Clone)]
pub struct Message {
pub global_mesg_num: u16,
pub name: &'static str,
pub fields: Vec<Field>,
}
impl Message {
pub fn field(&self, name: &str) -> Option<&Field> {
self.fields.iter().find(|f| f.name == name)
}
}