use crate::DELIMITER;
use serde::{Deserialize, Serialize};
use std::cmp::Ordering;
use std::fmt;
use validator::{Validate, ValidationErrors};
#[derive(Debug, Copy, Clone, Validate, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct MachineNode {
#[validate(range(min = 0, max = 31))]
pub machine_id: i32,
#[validate(range(min = 0, max = 31))]
pub node_id: i32,
}
impl fmt::Display for MachineNode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}{DELIMITER}{})", self.machine_id, self.node_id)
}
}
impl Default for MachineNode {
fn default() -> Self {
Self {
machine_id: 1,
node_id: 1,
}
}
}
impl MachineNode {
pub fn new(machine_id: i32, node_id: i32) -> Result<Self, ValidationErrors> {
let result = Self {
machine_id,
node_id,
};
result.validate()?;
Ok(result)
}
}
impl Ord for MachineNode {
fn cmp(&self, other: &Self) -> Ordering {
match self.machine_id.cmp(&other.machine_id) {
Ordering::Equal => self.node_id.cmp(&other.node_id),
o => o,
}
}
}
impl PartialOrd for MachineNode {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}