use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(untagged)]
#[derive(Default)]
pub enum Value {
Bool(bool),
I8(i8),
U8(u8),
I16(i16),
U16(u16),
I32(i32),
U32(u32),
I64(i64),
U64(u64),
F32(f32),
F64(f64),
String(String),
Bytes(Vec<u8>),
Array(Vec<Value>),
#[default]
Null,
}
impl Value {
pub fn bool(v: bool) -> Self {
Self::Bool(v)
}
pub fn float(v: f64) -> Self {
Self::F64(v)
}
pub fn int(v: i64) -> Self {
Self::I64(v)
}
pub fn string(v: impl Into<String>) -> Self {
Self::String(v.into())
}
pub fn as_bool(&self) -> Option<bool> {
match self {
Self::Bool(v) => Some(*v),
Self::I8(v) => Some(*v != 0),
Self::U8(v) => Some(*v != 0),
Self::I16(v) => Some(*v != 0),
Self::U16(v) => Some(*v != 0),
Self::I32(v) => Some(*v != 0),
Self::U32(v) => Some(*v != 0),
Self::I64(v) => Some(*v != 0),
Self::U64(v) => Some(*v != 0),
_ => None,
}
}
pub fn as_f64(&self) -> Option<f64> {
match self {
Self::Bool(v) => Some(if *v { 1.0 } else { 0.0 }),
Self::I8(v) => Some(*v as f64),
Self::U8(v) => Some(*v as f64),
Self::I16(v) => Some(*v as f64),
Self::U16(v) => Some(*v as f64),
Self::I32(v) => Some(*v as f64),
Self::U32(v) => Some(*v as f64),
Self::I64(v) => Some(*v as f64),
Self::U64(v) => Some(*v as f64),
Self::F32(v) => Some(*v as f64),
Self::F64(v) => Some(*v),
_ => None,
}
}
pub fn as_i64(&self) -> Option<i64> {
match self {
Self::Bool(v) => Some(if *v { 1 } else { 0 }),
Self::I8(v) => Some(*v as i64),
Self::U8(v) => Some(*v as i64),
Self::I16(v) => Some(*v as i64),
Self::U16(v) => Some(*v as i64),
Self::I32(v) => Some(*v as i64),
Self::U32(v) => Some(*v as i64),
Self::I64(v) => Some(*v),
Self::U64(v) => i64::try_from(*v).ok(),
Self::F32(v) => Some(*v as i64),
Self::F64(v) => Some(*v as i64),
_ => None,
}
}
pub fn as_u16(&self) -> Option<u16> {
self.as_i64().and_then(|v| u16::try_from(v).ok())
}
pub fn as_str(&self) -> Option<&str> {
match self {
Self::String(v) => Some(v.as_str()),
_ => None,
}
}
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Self::Bytes(v) => Some(v.as_slice()),
Self::String(v) => Some(v.as_bytes()),
_ => None,
}
}
pub fn is_numeric(&self) -> bool {
matches!(
self,
Self::I8(_)
| Self::U8(_)
| Self::I16(_)
| Self::U16(_)
| Self::I32(_)
| Self::U32(_)
| Self::I64(_)
| Self::U64(_)
| Self::F32(_)
| Self::F64(_)
)
}
pub fn is_null(&self) -> bool {
matches!(self, Self::Null)
}
pub fn type_name(&self) -> &'static str {
match self {
Self::Bool(_) => "bool",
Self::I8(_) => "i8",
Self::U8(_) => "u8",
Self::I16(_) => "i16",
Self::U16(_) => "u16",
Self::I32(_) => "i32",
Self::U32(_) => "u32",
Self::I64(_) => "i64",
Self::U64(_) => "u64",
Self::F32(_) => "f32",
Self::F64(_) => "f64",
Self::String(_) => "string",
Self::Bytes(_) => "bytes",
Self::Array(_) => "array",
Self::Null => "null",
}
}
pub fn to_registers(&self) -> Vec<u16> {
match self {
Self::Bool(v) => vec![if *v { 1 } else { 0 }],
Self::I8(v) => vec![*v as u16],
Self::U8(v) => vec![*v as u16],
Self::I16(v) => vec![*v as u16],
Self::U16(v) => vec![*v],
Self::I32(v) => {
let bytes = v.to_be_bytes();
vec![
u16::from_be_bytes([bytes[0], bytes[1]]),
u16::from_be_bytes([bytes[2], bytes[3]]),
]
}
Self::U32(v) => {
let bytes = v.to_be_bytes();
vec![
u16::from_be_bytes([bytes[0], bytes[1]]),
u16::from_be_bytes([bytes[2], bytes[3]]),
]
}
Self::F32(v) => {
let bytes = v.to_be_bytes();
vec![
u16::from_be_bytes([bytes[0], bytes[1]]),
u16::from_be_bytes([bytes[2], bytes[3]]),
]
}
Self::F64(v) => {
let bytes = v.to_be_bytes();
vec![
u16::from_be_bytes([bytes[0], bytes[1]]),
u16::from_be_bytes([bytes[2], bytes[3]]),
u16::from_be_bytes([bytes[4], bytes[5]]),
u16::from_be_bytes([bytes[6], bytes[7]]),
]
}
_ => vec![],
}
}
pub fn from_registers(registers: &[u16], value_type: ValueType) -> Option<Self> {
match value_type {
ValueType::Bool => registers.first().map(|r| Value::Bool(*r != 0)),
ValueType::U16 => registers.first().map(|r| Value::U16(*r)),
ValueType::I16 => registers.first().map(|r| Value::I16(*r as i16)),
ValueType::U32 if registers.len() >= 2 => {
let bytes = [registers[0].to_be_bytes(), registers[1].to_be_bytes()];
Some(Value::U32(u32::from_be_bytes([
bytes[0][0],
bytes[0][1],
bytes[1][0],
bytes[1][1],
])))
}
ValueType::I32 if registers.len() >= 2 => {
let bytes = [registers[0].to_be_bytes(), registers[1].to_be_bytes()];
Some(Value::I32(i32::from_be_bytes([
bytes[0][0],
bytes[0][1],
bytes[1][0],
bytes[1][1],
])))
}
ValueType::F32 if registers.len() >= 2 => {
let bytes = [registers[0].to_be_bytes(), registers[1].to_be_bytes()];
Some(Value::F32(f32::from_be_bytes([
bytes[0][0],
bytes[0][1],
bytes[1][0],
bytes[1][1],
])))
}
ValueType::F64 if registers.len() >= 4 => {
let bytes: Vec<u8> = registers[..4]
.iter()
.flat_map(|r| r.to_be_bytes())
.collect();
Some(Value::F64(f64::from_be_bytes([
bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
])))
}
_ => None,
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bool(v) => write!(f, "{}", v),
Self::I8(v) => write!(f, "{}", v),
Self::U8(v) => write!(f, "{}", v),
Self::I16(v) => write!(f, "{}", v),
Self::U16(v) => write!(f, "{}", v),
Self::I32(v) => write!(f, "{}", v),
Self::U32(v) => write!(f, "{}", v),
Self::I64(v) => write!(f, "{}", v),
Self::U64(v) => write!(f, "{}", v),
Self::F32(v) => write!(f, "{:.3}", v),
Self::F64(v) => write!(f, "{:.6}", v),
Self::String(v) => write!(f, "{}", v),
Self::Bytes(v) => write!(f, "[{} bytes]", v.len()),
Self::Array(v) => write!(f, "[{} items]", v.len()),
Self::Null => write!(f, "null"),
}
}
}
impl From<bool> for Value {
fn from(v: bool) -> Self {
Self::Bool(v)
}
}
impl From<i32> for Value {
fn from(v: i32) -> Self {
Self::I32(v)
}
}
impl From<u32> for Value {
fn from(v: u32) -> Self {
Self::U32(v)
}
}
impl From<i64> for Value {
fn from(v: i64) -> Self {
Self::I64(v)
}
}
impl From<u64> for Value {
fn from(v: u64) -> Self {
Self::U64(v)
}
}
impl From<f32> for Value {
fn from(v: f32) -> Self {
Self::F32(v)
}
}
impl From<f64> for Value {
fn from(v: f64) -> Self {
Self::F64(v)
}
}
impl From<String> for Value {
fn from(v: String) -> Self {
Self::String(v)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Self::String(v.to_string())
}
}
impl From<Vec<u8>> for Value {
fn from(v: Vec<u8>) -> Self {
Self::Bytes(v)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ValueType {
Bool,
I8,
U8,
I16,
U16,
I32,
U32,
I64,
U64,
F32,
F64,
String,
Bytes,
}
impl ValueType {
pub fn size_bytes(&self) -> usize {
match self {
Self::Bool | Self::I8 | Self::U8 => 1,
Self::I16 | Self::U16 => 2,
Self::I32 | Self::U32 | Self::F32 => 4,
Self::I64 | Self::U64 | Self::F64 => 8,
Self::String | Self::Bytes => 0, }
}
pub fn register_count(&self) -> usize {
match self {
Self::Bool | Self::I8 | Self::U8 | Self::I16 | Self::U16 => 1,
Self::I32 | Self::U32 | Self::F32 => 2,
Self::I64 | Self::U64 | Self::F64 => 4,
Self::String | Self::Bytes => 0,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_value_conversion() {
let v = Value::F64(123.456);
assert!((v.as_f64().unwrap() - 123.456).abs() < 0.001);
assert_eq!(v.as_i64(), Some(123));
}
#[test]
fn test_value_to_registers() {
let v = Value::F32(1.5);
let regs = v.to_registers();
assert_eq!(regs.len(), 2);
let v2 = Value::from_registers(®s, ValueType::F32).unwrap();
assert!((v2.as_f64().unwrap() - 1.5).abs() < 0.001);
}
#[test]
fn test_value_display() {
assert_eq!(Value::Bool(true).to_string(), "true");
assert_eq!(Value::I32(42).to_string(), "42");
assert_eq!(Value::F64(3.14159).to_string(), "3.141590");
}
}