plate-tool/plate-tool-lib/src/plate.rs

109 lines
2.9 KiB
Rust

use serde::{Deserialize, Serialize};
#[derive(PartialEq, Eq, Default, Clone, Copy, Serialize, Deserialize, Debug)]
pub struct Plate {
pub plate_type: PlateType,
pub plate_format: PlateFormat,
}
impl Plate {
pub fn new(plate_type: PlateType, plate_format: PlateFormat) -> Self {
Plate {
plate_type,
plate_format,
}
}
pub fn size(&self) -> (u8, u8) {
self.plate_format.size()
}
}
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Debug)]
pub enum PlateType {
Source,
Destination,
}
impl Default for PlateType {
fn default() -> Self {
Self::Source
}
}
#[derive(PartialEq, Eq, Clone, Copy, Serialize, Deserialize, Debug)]
pub enum PlateFormat {
W6,
W12,
W24,
W48,
W96,
W384,
W1536,
W3456,
}
impl Default for PlateFormat {
fn default() -> Self {
Self::W96
}
}
impl std::fmt::Display for PlateFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
PlateFormat::W6 => write!(f, "6"),
PlateFormat::W12 => write!(f, "12"),
PlateFormat::W24 => write!(f, "24"),
PlateFormat::W48 => write!(f, "48"),
PlateFormat::W96 => write!(f, "96"),
PlateFormat::W384 => write!(f, "384"),
PlateFormat::W1536 => write!(f, "1536"),
PlateFormat::W3456 => write!(f, "3456"),
}
}
}
impl TryFrom<&str> for PlateFormat {
type Error = ();
fn try_from(value: &str) -> Result<Self, Self::Error> {
let lower = value.to_lowercase();
match lower.trim() {
"w6" | "6" => Ok(PlateFormat::W6),
"w12" | "12" => Ok(PlateFormat::W12),
"w24" | "24" => Ok(PlateFormat::W24),
"w48" | "48" => Ok(PlateFormat::W48),
"w96" | "96" => Ok(PlateFormat::W96),
"w384" | "384" => Ok(PlateFormat::W384),
"w1536" | "1536" => Ok(PlateFormat::W1536),
"w3456" | "3456" => Ok(PlateFormat::W3456),
_ => Err(())
}
}
}
impl From<&PlateFormat> for u16 {
fn from(value: &PlateFormat) -> Self {
match value {
PlateFormat::W6 => 6,
PlateFormat::W12 => 12,
PlateFormat::W24 => 24,
PlateFormat::W48 => 48,
PlateFormat::W96 => 96,
PlateFormat::W384 => 384,
PlateFormat::W1536 => 1536,
PlateFormat::W3456 => 3456,
}
}
}
impl PlateFormat {
pub fn size(&self) -> (u8, u8) {
match self {
PlateFormat::W6 => (2, 3),
PlateFormat::W12 => (3, 4),
PlateFormat::W24 => (4, 6),
PlateFormat::W48 => (6, 8),
PlateFormat::W96 => (8, 12),
PlateFormat::W384 => (16, 24),
PlateFormat::W1536 => (32, 48),
PlateFormat::W3456 => (48, 72),
}
}
}