use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[cfg(feature = "python")]
use pyo3::prelude::*;
#[derive(Default, Clone, Copy, Debug, PartialOrd, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "python", pyclass(eq, eq_int))]
pub enum ObjectiveDirection {
#[default]
Minimise,
Maximise,
}
impl Display for ObjectiveDirection {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ObjectiveDirection::Minimise => f.write_str("minimised"),
ObjectiveDirection::Maximise => f.write_str("maximised"),
}
}
}
#[cfg(feature = "python")]
#[pymethods]
impl ObjectiveDirection {
pub fn __repr__(&self) -> PyResult<String> {
Ok(format!("ObjectiveDirection({self})",))
}
pub fn __str__(&self) -> String {
self.__repr__().unwrap()
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[cfg_attr(feature = "python", pyclass(get_all))]
pub struct Objective {
name: String,
direction: ObjectiveDirection,
}
impl Objective {
pub fn new(name: &str, direction: ObjectiveDirection) -> Self {
Self {
name: name.to_string(),
direction,
}
}
pub fn name(&self) -> String {
self.name.clone()
}
pub fn direction(&self) -> ObjectiveDirection {
self.direction
}
}
impl Display for Objective {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "Objective '{}' is {}", self.name, self.direction)
}
}
#[cfg(feature = "python")]
#[pymethods]
impl Objective {
pub fn __repr__(&self) -> PyResult<String> {
Ok(format!(
"Objective(name='{}', direction='{}')",
self.name, self.direction
))
}
pub fn __str__(&self) -> String {
self.__repr__().unwrap()
}
}