Browse Source

Cleanup refactoring

Kevin Lee 10 months ago
parent
commit
91a848b3f0
1 changed files with 31 additions and 80 deletions
  1. 31 80
      src/ezcad/types.rs

+ 31 - 80
src/ezcad/types.rs

@@ -5,8 +5,10 @@ use std::{
 
 
 use binrw::{binrw, BinRead, BinWrite};
 use binrw::{binrw, BinRead, BinWrite};
 use diff::{Diff, VecDiff};
 use diff::{Diff, VecDiff};
+use num_enum::{IntoPrimitive, TryFromPrimitive};
 use rand::{thread_rng, Rng};
 use rand::{thread_rng, Rng};
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
+use serde_repr::{Deserialize_repr, Serialize_repr};
 use strum::EnumIter;
 use strum::EnumIter;
 
 
 use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
 use crate::{array_of::ArrayOfPrimitive, field_of::FieldOf};
@@ -254,89 +256,38 @@ impl Default for ObjectType {
     }
     }
 }
 }
 
 
-#[derive(Copy, Clone, Debug, Diff, PartialEq, strum::Display, Serialize, Deserialize, EnumIter)]
+#[derive(
+    Copy,
+    Clone,
+    Debug,
+    Diff,
+    PartialEq,
+    strum::Display,
+    Serialize_repr,
+    Deserialize_repr,
+    EnumIter,
+    IntoPrimitive,
+    TryFromPrimitive,
+)]
 #[diff(attr(
 #[diff(attr(
     #[derive(Debug, PartialEq)]
     #[derive(Debug, PartialEq)]
 ))]
 ))]
-#[serde(try_from = "u32")]
-#[serde(into = "u32")]
 #[repr(u32)]
 #[repr(u32)]
 pub enum PulseWidth {
 pub enum PulseWidth {
-    Ns2,
-    Ns4,
-    Ns6,
-    Ns8,
-    Ns12,
-    Ns20,
-    Ns30,
-    Ns45,
-    Ns60,
-    Ns80,
-    Ns100,
-    Ns150,
-    Ns200,
-    Ns250,
-    Ns350,
-    Ns500,
-}
-
-impl From<PulseWidth> for u32 {
-    fn from(value: PulseWidth) -> Self {
-        match value {
-            PulseWidth::Ns2 => 2,
-            PulseWidth::Ns4 => 4,
-            PulseWidth::Ns6 => 6,
-            PulseWidth::Ns8 => 8,
-            PulseWidth::Ns12 => 12,
-            PulseWidth::Ns20 => 20,
-            PulseWidth::Ns30 => 30,
-            PulseWidth::Ns45 => 45,
-            PulseWidth::Ns60 => 60,
-            PulseWidth::Ns80 => 80,
-            PulseWidth::Ns100 => 100,
-            PulseWidth::Ns150 => 150,
-            PulseWidth::Ns200 => 200,
-            PulseWidth::Ns250 => 250,
-            PulseWidth::Ns350 => 350,
-            PulseWidth::Ns500 => 500,
-        }
-    }
-}
-
-pub enum PulseWidthError {
-    Unsupported,
-}
-
-impl Display for PulseWidthError {
-    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
-        match self {
-            PulseWidthError::Unsupported => write!(f, "Unsupported pulse width value"),
-        }
-    }
-}
-
-impl TryFrom<u32> for PulseWidth {
-    type Error = PulseWidthError;
-
-    fn try_from(value: u32) -> Result<Self, Self::Error> {
-        match value {
-            2 => Ok(PulseWidth::Ns2),
-            4 => Ok(PulseWidth::Ns4),
-            6 => Ok(PulseWidth::Ns6),
-            8 => Ok(PulseWidth::Ns8),
-            12 => Ok(PulseWidth::Ns12),
-            20 => Ok(PulseWidth::Ns20),
-            30 => Ok(PulseWidth::Ns30),
-            45 => Ok(PulseWidth::Ns45),
-            60 => Ok(PulseWidth::Ns60),
-            80 => Ok(PulseWidth::Ns80),
-            100 => Ok(PulseWidth::Ns100),
-            150 => Ok(PulseWidth::Ns150),
-            200 => Ok(PulseWidth::Ns200),
-            250 => Ok(PulseWidth::Ns250),
-            350 => Ok(PulseWidth::Ns350),
-            500 => Ok(PulseWidth::Ns500),
-            _ => Err(PulseWidthError::Unsupported),
-        }
-    }
+    Ns2 = 2,
+    Ns4 = 4,
+    Ns6 = 6,
+    Ns8 = 8,
+    Ns12 = 12,
+    Ns20 = 20,
+    Ns30 = 30,
+    Ns45 = 45,
+    Ns60 = 60,
+    Ns80 = 80,
+    Ns100 = 100,
+    Ns150 = 150,
+    Ns200 = 200,
+    Ns250 = 250,
+    Ns350 = 350,
+    Ns500 = 500,
 }
 }