use crate::customer::Customer;
use std::cmp::Ordering;
#[derive(Debug)]
pub(crate) enum EventType {
Arrival,
StartService,
Departure,
}
impl std::fmt::Display for EventType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
EventType::Arrival => write!(f, "Arrival"),
EventType::StartService => write!(f, "StartService"),
EventType::Departure => write!(f, "Departure"),
}
}
}
impl PartialOrd for EventType {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for EventType {
fn cmp(&self, other: &Self) -> Ordering {
use EventType::*;
match (self, other) {
(Departure, Departure) | (StartService, StartService) | (Arrival, Arrival) => {
Ordering::Equal
}
(Departure, _) => Ordering::Greater,
(_, Departure) => Ordering::Less,
(StartService, _) => Ordering::Greater,
(_, StartService) => Ordering::Less,
}
}
}
impl PartialEq for EventType {
fn eq(&self, other: &Self) -> bool {
self == other
}
}
impl Eq for EventType {}
#[derive(Debug)]
pub(crate) struct Event {
pub(crate) event_type: EventType,
customer: Customer,
pub(crate) t: f64,
}
impl Event {
pub fn new(event_type: EventType, customer: Customer, t: f64) -> Self {
Self {
event_type,
customer,
t,
}
}
pub fn get_customer(&self) -> Customer {
self.customer
}
pub fn get_customer_mut(&mut self) -> &mut Customer {
&mut self.customer
}
}
impl std::fmt::Display for Event {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Event({:15}, {:.4}, {})",
self.event_type,
self.t,
self.get_customer()
)
}
}
impl PartialEq for Event {
fn eq(&self, other: &Self) -> bool {
other.t.eq(&self.t) && self.event_type == other.event_type
}
}
impl Eq for Event {}
impl PartialOrd for Event {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.t == other.t {
return self.event_type.partial_cmp(&other.event_type);
}
other.t.partial_cmp(&self.t)
}
}
impl Ord for Event {
fn cmp(&self, other: &Self) -> Ordering {
self.partial_cmp(&other).unwrap()
}
}