use crate::implement_from_wrapper;
use std::fmt::Display;
#[derive(Debug, Clone, PartialEq)]
pub enum AttributeValue {
Boolean(bool),
Float32(f32),
Float64(f64),
Int16(i16),
Int32(i32),
Int64(i64),
Int8(i8),
Null,
String(String),
UInt16(u16),
UInt32(u32),
UInt64(u64),
UInt8(u8),
Usize(usize),
}
implement_from_wrapper!(AttributeValue, Boolean, bool);
implement_from_wrapper!(AttributeValue, Float32, f32);
implement_from_wrapper!(AttributeValue, Float64, f64);
implement_from_wrapper!(AttributeValue, Int16, i16);
implement_from_wrapper!(AttributeValue, Int32, i32);
implement_from_wrapper!(AttributeValue, Int64, i64);
implement_from_wrapper!(AttributeValue, Int8, i8);
implement_from_wrapper!(AttributeValue, String, String);
implement_from_wrapper!(AttributeValue, UInt16, u16);
implement_from_wrapper!(AttributeValue, UInt32, u32);
implement_from_wrapper!(AttributeValue, UInt64, u64);
implement_from_wrapper!(AttributeValue, UInt8, u8);
implement_from_wrapper!(AttributeValue, Usize, usize);
impl From<&str> for AttributeValue {
fn from(value: &str) -> Self {
value.to_string().into()
}
}
impl<T: Into<AttributeValue>> From<Option<T>> for AttributeValue {
fn from(value: Option<T>) -> Self {
match value {
Some(value) => value.into(),
None => AttributeValue::Null,
}
}
}
impl Display for AttributeValue {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
AttributeValue::Boolean(value) => write!(f, "{}", value),
AttributeValue::Float32(value) => write!(f, "{}", value),
AttributeValue::Float64(value) => write!(f, "{}", value),
AttributeValue::Int16(value) => write!(f, "{}", value),
AttributeValue::Int32(value) => write!(f, "{}", value),
AttributeValue::Int64(value) => write!(f, "{}", value),
AttributeValue::Int8(value) => write!(f, "{}", value),
AttributeValue::Null => write!(f, "null"),
AttributeValue::String(value) => write!(f, "{}", value),
AttributeValue::UInt16(value) => write!(f, "{}", value),
AttributeValue::UInt32(value) => write!(f, "{}", value),
AttributeValue::UInt64(value) => write!(f, "{}", value),
AttributeValue::UInt8(value) => write!(f, "{}", value),
AttributeValue::Usize(value) => write!(f, "{}", value),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_attribute_value_from() {
assert_eq!(AttributeValue::from(false), AttributeValue::Boolean(false));
assert_eq!(AttributeValue::from(0_f32), AttributeValue::Float32(0_f32));
assert_eq!(AttributeValue::from(0_f64), AttributeValue::Float64(0_f64));
assert_eq!(AttributeValue::from(0_i16), AttributeValue::Int16(0_i16));
assert_eq!(AttributeValue::from(0_i32), AttributeValue::Int32(0_i32));
assert_eq!(AttributeValue::from(0_i64), AttributeValue::Int64(0_i64));
assert_eq!(AttributeValue::from(0_i8), AttributeValue::Int8(0_i8));
assert_eq!(
AttributeValue::from("foo".to_string()),
AttributeValue::String("foo".to_string())
);
assert_eq!(AttributeValue::from(0_u16), AttributeValue::UInt16(0_u16));
assert_eq!(AttributeValue::from(0_u32), AttributeValue::UInt32(0_u32));
assert_eq!(AttributeValue::from(0_u64), AttributeValue::UInt64(0_u64));
assert_eq!(AttributeValue::from(0_u8), AttributeValue::UInt8(0_u8));
assert_eq!(
AttributeValue::from(0_usize),
AttributeValue::Usize(0_usize)
);
assert_eq!(
AttributeValue::from("foo"),
AttributeValue::String("foo".to_string())
);
}
#[test]
fn test_attribute_value_from_option() {
assert_eq!(
AttributeValue::from(Some(false)),
AttributeValue::Boolean(false)
);
assert_eq!(
AttributeValue::from(Some(0_f32)),
AttributeValue::Float32(0_f32)
);
assert_eq!(
AttributeValue::from(Some(0_f64)),
AttributeValue::Float64(0_f64)
);
assert_eq!(
AttributeValue::from(Some(0_i16)),
AttributeValue::Int16(0_i16)
);
assert_eq!(
AttributeValue::from(Some(0_i32)),
AttributeValue::Int32(0_i32)
);
assert_eq!(
AttributeValue::from(Some(0_i64)),
AttributeValue::Int64(0_i64)
);
assert_eq!(AttributeValue::from(Some(0_i8)), AttributeValue::Int8(0_i8));
assert_eq!(
AttributeValue::from(Some("foo".to_string())),
AttributeValue::String("foo".to_string())
);
assert_eq!(
AttributeValue::from(Some(0_u16)),
AttributeValue::UInt16(0_u16)
);
assert_eq!(
AttributeValue::from(Some(0_u32)),
AttributeValue::UInt32(0_u32)
);
assert_eq!(
AttributeValue::from(Some(0_u64)),
AttributeValue::UInt64(0_u64)
);
assert_eq!(
AttributeValue::from(Some(0_u8)),
AttributeValue::UInt8(0_u8)
);
assert_eq!(
AttributeValue::from(Some(0_usize)),
AttributeValue::Usize(0_usize)
);
assert_eq!(
AttributeValue::from(Some("foo")),
AttributeValue::String("foo".to_string())
);
assert_eq!(AttributeValue::from(None::<bool>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<f32>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<f64>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<i16>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<i32>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<i64>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<i8>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<String>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<u16>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<u32>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<u64>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<u8>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<usize>), AttributeValue::Null);
assert_eq!(AttributeValue::from(None::<&str>), AttributeValue::Null);
}
#[test]
fn test_attribute_value_display() {
assert_eq!(format!("{}", AttributeValue::Boolean(false)), "false");
assert_eq!(format!("{}", AttributeValue::Float32(0_f32)), "0");
assert_eq!(format!("{}", AttributeValue::Float64(0_f64)), "0");
assert_eq!(format!("{}", AttributeValue::Int16(0_i16)), "0");
assert_eq!(format!("{}", AttributeValue::Int32(0_i32)), "0");
assert_eq!(format!("{}", AttributeValue::Int64(0_i64)), "0");
assert_eq!(format!("{}", AttributeValue::Int8(0_i8)), "0");
assert_eq!(format!("{}", AttributeValue::Null), "null");
assert_eq!(
format!("{}", AttributeValue::String("foo".to_string())),
"foo"
);
assert_eq!(format!("{}", AttributeValue::UInt16(0_u16)), "0");
assert_eq!(format!("{}", AttributeValue::UInt32(0_u32)), "0");
assert_eq!(format!("{}", AttributeValue::UInt64(0_u64)), "0");
assert_eq!(format!("{}", AttributeValue::UInt8(0_u8)), "0");
assert_eq!(format!("{}", AttributeValue::Usize(0_usize)), "0");
}
}