|
|
@ -4,13 +4,13 @@ use log::error;
|
|
|
|
// Trait to allow for conversion from rmpv::Value to any other data type.
|
|
|
|
// Trait to allow for conversion from rmpv::Value to any other data type.
|
|
|
|
// Note: Feel free to implement this trait for custom types in each subsystem.
|
|
|
|
// Note: Feel free to implement this trait for custom types in each subsystem.
|
|
|
|
// The reverse conversion (MyType->Value) can be performed by implementing `From<MyType> for Value`
|
|
|
|
// The reverse conversion (MyType->Value) can be performed by implementing `From<MyType> for Value`
|
|
|
|
pub trait FromValue {
|
|
|
|
pub trait ParseFromValue {
|
|
|
|
fn from_value(&mut self, value: Value);
|
|
|
|
fn parse_from_value(&mut self, value: Value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FromValue implementations for most typical types
|
|
|
|
// FromValue implementations for most typical types
|
|
|
|
impl FromValue for f32 {
|
|
|
|
impl ParseFromValue for f32 {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_f64() {
|
|
|
|
if value.is_f64() {
|
|
|
|
*self = value.as_f64().unwrap() as f32;
|
|
|
|
*self = value.as_f64().unwrap() as f32;
|
|
|
|
} else if value.is_i64() {
|
|
|
|
} else if value.is_i64() {
|
|
|
@ -23,8 +23,8 @@ impl FromValue for f32 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for u64 {
|
|
|
|
impl ParseFromValue for u64 {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_u64() {
|
|
|
|
if value.is_u64() {
|
|
|
|
*self = value.as_u64().unwrap();
|
|
|
|
*self = value.as_u64().unwrap();
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -33,8 +33,8 @@ impl FromValue for u64 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for u32 {
|
|
|
|
impl ParseFromValue for u32 {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_u64() {
|
|
|
|
if value.is_u64() {
|
|
|
|
*self = value.as_u64().unwrap() as u32;
|
|
|
|
*self = value.as_u64().unwrap() as u32;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -43,8 +43,8 @@ impl FromValue for u32 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for i32 {
|
|
|
|
impl ParseFromValue for i32 {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_i64() {
|
|
|
|
if value.is_i64() {
|
|
|
|
*self = value.as_i64().unwrap() as i32;
|
|
|
|
*self = value.as_i64().unwrap() as i32;
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -53,8 +53,8 @@ impl FromValue for i32 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for String {
|
|
|
|
impl ParseFromValue for String {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_str() {
|
|
|
|
if value.is_str() {
|
|
|
|
*self = String::from(value.as_str().unwrap());
|
|
|
|
*self = String::from(value.as_str().unwrap());
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
@ -63,8 +63,8 @@ impl FromValue for String {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl FromValue for bool {
|
|
|
|
impl ParseFromValue for bool {
|
|
|
|
fn from_value(&mut self, value: Value) {
|
|
|
|
fn parse_from_value(&mut self, value: Value) {
|
|
|
|
if value.is_bool() {
|
|
|
|
if value.is_bool() {
|
|
|
|
*self = value.as_bool().unwrap();
|
|
|
|
*self = value.as_bool().unwrap();
|
|
|
|
} else if value.is_u64() {
|
|
|
|
} else if value.is_u64() {
|
|
|
@ -81,7 +81,7 @@ mod tests {
|
|
|
|
use super::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_f32() {
|
|
|
|
fn test_parse_from_value_f32() {
|
|
|
|
let mut v0: f32 = 0.0;
|
|
|
|
let mut v0: f32 = 0.0;
|
|
|
|
let v1 = Value::from(1.0);
|
|
|
|
let v1 = Value::from(1.0);
|
|
|
|
let v2 = Value::from(-1);
|
|
|
|
let v2 = Value::from(-1);
|
|
|
@ -90,76 +90,76 @@ mod tests {
|
|
|
|
let v2p = -1.0;
|
|
|
|
let v2p = -1.0;
|
|
|
|
let v3p = std::u64::MAX as f32;
|
|
|
|
let v3p = std::u64::MAX as f32;
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
v0.from_value(v2);
|
|
|
|
v0.parse_from_value(v2);
|
|
|
|
assert_eq!(v0, v2p, "v0 should equal {} but is actually {}", v2p, v0);
|
|
|
|
assert_eq!(v0, v2p, "v0 should equal {} but is actually {}", v2p, v0);
|
|
|
|
v0.from_value(v3);
|
|
|
|
v0.parse_from_value(v3);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from("asd"));
|
|
|
|
v0.parse_from_value(Value::from("asd"));
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_u64() {
|
|
|
|
fn test_parse_from_value_u64() {
|
|
|
|
let mut v0: u64 = 0;
|
|
|
|
let mut v0: u64 = 0;
|
|
|
|
let v1 = Value::from(std::u64::MAX);
|
|
|
|
let v1 = Value::from(std::u64::MAX);
|
|
|
|
let v1p = std::u64::MAX;
|
|
|
|
let v1p = std::u64::MAX;
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from(-1));
|
|
|
|
v0.parse_from_value(Value::from(-1));
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_u32() {
|
|
|
|
fn test_parse_from_value_u32() {
|
|
|
|
let mut v0: u32 = 0;
|
|
|
|
let mut v0: u32 = 0;
|
|
|
|
let v1 = Value::from(std::u64::MAX);
|
|
|
|
let v1 = Value::from(std::u64::MAX);
|
|
|
|
let v1p = std::u64::MAX as u32;
|
|
|
|
let v1p = std::u64::MAX as u32;
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from(-1));
|
|
|
|
v0.parse_from_value(Value::from(-1));
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_i32() {
|
|
|
|
fn test_parse_from_value_i32() {
|
|
|
|
let mut v0: i32 = 0;
|
|
|
|
let mut v0: i32 = 0;
|
|
|
|
let v1 = Value::from(std::i64::MAX);
|
|
|
|
let v1 = Value::from(std::i64::MAX);
|
|
|
|
let v1p = std::i64::MAX as i32;
|
|
|
|
let v1p = std::i64::MAX as i32;
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from(-1));
|
|
|
|
v0.parse_from_value(Value::from(-1));
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_string() {
|
|
|
|
fn test_parse_from_value_string() {
|
|
|
|
let mut v0: String = "foo".to_string();
|
|
|
|
let mut v0: String = "foo".to_string();
|
|
|
|
let v1 = Value::from("bar");
|
|
|
|
let v1 = Value::from("bar");
|
|
|
|
let v1p = "bar";
|
|
|
|
let v1p = "bar";
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from(-1));
|
|
|
|
v0.parse_from_value(Value::from(-1));
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[test]
|
|
|
|
fn test_from_value_bool() {
|
|
|
|
fn test_parse_from_value_bool() {
|
|
|
|
let mut v0: bool = false;
|
|
|
|
let mut v0: bool = false;
|
|
|
|
let v1 = Value::from(true);
|
|
|
|
let v1 = Value::from(true);
|
|
|
|
let v1p = true;
|
|
|
|
let v1p = true;
|
|
|
@ -168,15 +168,15 @@ mod tests {
|
|
|
|
let v3 = Value::from(1);
|
|
|
|
let v3 = Value::from(1);
|
|
|
|
let v3p = true;
|
|
|
|
let v3p = true;
|
|
|
|
|
|
|
|
|
|
|
|
v0.from_value(v1);
|
|
|
|
v0.parse_from_value(v1);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
assert_eq!(v0, v1p, "v0 should equal {} but is actually {}", v1p, v0);
|
|
|
|
v0.from_value(v2);
|
|
|
|
v0.parse_from_value(v2);
|
|
|
|
assert_eq!(v0, v2p, "v0 should equal {} but is actually {}", v2p, v0);
|
|
|
|
assert_eq!(v0, v2p, "v0 should equal {} but is actually {}", v2p, v0);
|
|
|
|
v0.from_value(v3);
|
|
|
|
v0.parse_from_value(v3);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
|
|
|
|
|
|
|
|
// This is a noop and prints an error
|
|
|
|
// This is a noop and prints an error
|
|
|
|
v0.from_value(Value::from(-1));
|
|
|
|
v0.parse_from_value(Value::from(-1));
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
assert_eq!(v0, v3p, "v0 should equal {} but is actually {}", v3p, v0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|