use serde::Deserialize;
use serde::Serialize;
use super::McIdentifier;
use super::McText;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct McDimType {
pub value_type: McValueType,
#[serde(skip_serializing_if = "Option::is_none")]
pub x_dim: Option<u16>,
#[serde(skip_serializing_if = "Option::is_none")]
pub y_dim: Option<u16>,
}
impl McDimType {
pub fn new(value_type: McValueType, x_dim: u16, y_dim: u16) -> Self {
McDimType {
value_type,
x_dim: if x_dim <= 1 { None } else { Some(x_dim) },
y_dim: if y_dim <= 1 { None } else { Some(y_dim) },
}
}
pub fn is_basic_type(&self) -> bool {
!matches!(self.value_type, McValueType::Blob(_) | McValueType::TypeDef(_))
}
pub fn is_blob(&self) -> bool {
if let McValueType::Blob(_) = self.value_type {
return true;
}
false
}
pub fn is_typedef(&self) -> bool {
if let McValueType::TypeDef(_) = self.value_type {
return true;
}
false
}
pub fn is_scalar(&self) -> bool {
let x_dim = self.x_dim.unwrap_or(1);
let y_dim = self.y_dim.unwrap_or(1);
x_dim <= 1 && y_dim <= 1
}
pub fn is_array(&self) -> bool {
let x_dim = self.x_dim.unwrap_or(1);
let y_dim = self.y_dim.unwrap_or(1);
x_dim > 1 && y_dim <= 1
}
pub fn is_matrix(&self) -> bool {
let x_dim = self.x_dim.unwrap_or(1);
let y_dim = self.y_dim.unwrap_or(1);
x_dim > 1 && y_dim > 1
}
pub fn get_dim(&self) -> [u16; 2] {
[self.x_dim.unwrap_or(1), self.y_dim.unwrap_or(1)]
}
pub fn get_size(&self) -> usize {
self.value_type.get_size() * self.get_dim()[0] as usize * self.get_dim()[1] as usize
}
}
impl std::fmt::Display for McDimType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self)?;
Ok(())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum McValueType {
Unknown,
Bool,
Ubyte,
Uword,
Ulong,
Ulonglong,
Sbyte,
Sword,
Slong,
Slonglong,
Float32Ieee,
Float64Ieee,
Blob(McText),
TypeDef(McIdentifier),
}
impl McValueType {
pub fn new_typedef<T: Into<McIdentifier>>(name: T) -> Self {
let name: McIdentifier = name.into();
McValueType::TypeDef(name)
}
pub fn new_blob<T: Into<McText>>(text: T) -> Self {
let text: McText = text.into();
McValueType::Blob(text)
}
pub fn get_min(&self) -> Option<f64> {
match self {
McValueType::Bool => Some(0.0),
McValueType::Sbyte => Some(i8::MIN as f64),
McValueType::Sword => Some(i16::MIN as f64),
McValueType::Slong => Some(i32::MIN as f64),
#[allow(clippy::cast_precision_loss)]
McValueType::Slonglong => Some(i64::MIN as f64),
McValueType::Float32Ieee | McValueType::Float64Ieee => Some(-1E32),
McValueType::Ubyte => Some(0.0),
McValueType::Uword => Some(0.0),
McValueType::Ulong => Some(0.0),
McValueType::Ulonglong => Some(0.0),
_ => {
None
}
}
}
pub fn get_max(&self) -> Option<f64> {
match self {
McValueType::Ubyte => Some(u8::MAX as f64),
McValueType::Sbyte => Some(i8::MAX as f64),
McValueType::Uword => Some(u16::MAX as f64),
McValueType::Sword => Some(i16::MAX as f64),
McValueType::Ulong => Some(u32::MAX as f64),
McValueType::Slong => Some(i32::MAX as f64),
#[allow(clippy::cast_precision_loss)]
McValueType::Ulonglong => Some(u64::MAX as f64), #[allow(clippy::cast_precision_loss)]
McValueType::Slonglong => Some(i64::MAX as f64),
McValueType::Float32Ieee => Some(1E32),
McValueType::Float64Ieee => Some(1E32),
McValueType::Bool => Some(1.0),
_ => {
None
}
}
}
pub fn get_size(&self) -> usize {
match self {
McValueType::Ubyte | McValueType::Sbyte | McValueType::Bool => 1,
McValueType::Uword | McValueType::Sword => 2,
McValueType::Ulong | McValueType::Slong | McValueType::Float32Ieee => 4,
McValueType::Ulonglong | McValueType::Slonglong | McValueType::Float64Ieee => 8,
McValueType::Blob(_) => panic!("get_size: Unknown blob size"),
McValueType::TypeDef(_) => {
log::error!("get_size: TypeDef instance size unknown");
1
}
_ => panic!("get_size: Unsupported data type"),
}
}
pub fn from_integer_size(size: usize, signed: bool) -> McValueType {
match (size, signed) {
(1, false) => McValueType::Ubyte,
(1, true) => McValueType::Sbyte,
(2, false) => McValueType::Uword,
(2, true) => McValueType::Sword,
(4, false) => McValueType::Ulong,
(4, true) => McValueType::Slong,
(8, false) => McValueType::Ulonglong,
(8, true) => McValueType::Slonglong,
_ => McValueType::Unknown,
}
}
pub fn from_float_size(size: usize) -> McValueType {
match size {
4 => McValueType::Float32Ieee,
8 => McValueType::Float64Ieee,
_ => McValueType::Unknown,
}
}
}
pub trait McValueTypeTrait {
fn get_type(&self) -> McValueType;
}
impl<T> McValueTypeTrait for std::num::Wrapping<T>
where
T: McValueTypeTrait,
{
fn get_type(&self) -> McValueType {
self.0.get_type()
}
}
impl<T> McValueTypeTrait for Option<T>
where
T: McValueTypeTrait + std::default::Default,
{
fn get_type(&self) -> McValueType {
let x: T = T::default();
x.get_type()
}
}
impl McValueTypeTrait for bool {
fn get_type(&self) -> McValueType {
McValueType::Bool
}
}
impl McValueTypeTrait for i8 {
fn get_type(&self) -> McValueType {
McValueType::Sbyte
}
}
impl McValueTypeTrait for i16 {
fn get_type(&self) -> McValueType {
McValueType::Sword
}
}
impl McValueTypeTrait for i32 {
fn get_type(&self) -> McValueType {
McValueType::Slong
}
}
impl McValueTypeTrait for i64 {
fn get_type(&self) -> McValueType {
McValueType::Slonglong
}
}
impl McValueTypeTrait for isize {
fn get_type(&self) -> McValueType {
McValueType::Slonglong
}
}
impl McValueTypeTrait for u8 {
fn get_type(&self) -> McValueType {
McValueType::Ubyte
}
}
impl McValueTypeTrait for u16 {
fn get_type(&self) -> McValueType {
McValueType::Uword
}
}
impl McValueTypeTrait for u32 {
fn get_type(&self) -> McValueType {
McValueType::Ulong
}
}
impl McValueTypeTrait for u64 {
fn get_type(&self) -> McValueType {
McValueType::Ulonglong
}
}
impl McValueTypeTrait for usize {
fn get_type(&self) -> McValueType {
McValueType::Ulonglong
}
}
impl McValueTypeTrait for f32 {
fn get_type(&self) -> McValueType {
McValueType::Float32Ieee
}
}
impl McValueTypeTrait for f64 {
fn get_type(&self) -> McValueType {
McValueType::Float64Ieee
}
}
#[cfg(test)]
mod mc_type_tests {
use super::*;
fn is_copy<T: Sized + Copy>() {}
fn is_send<T: Sized + Send>() {}
fn is_sync<T: Sized + Sync>() {}
fn is_clone<T: Sized + Clone>() {}
#[test]
fn test_mc_type() {
is_sync::<McValueType>();
is_copy::<McValueType>();
is_send::<McValueType>();
is_clone::<McValueType>();
let t1 = McValueType::Sbyte;
assert_eq!(t1.get_min(), Some(-128.0));
assert_eq!(t1.get_max(), Some(127.0));
assert_eq!(t1.get_size(), 1);
let byte: u8 = 0;
let t2 = byte.get_type();
assert_eq!(t2, McValueType::Ubyte);
}
}