use owo_colors::{OwoColorize, Stream};
use serde::{Deserialize, Serialize};
use super::ProjectID;
pub type SectionID = String;
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Section {
pub id: SectionID,
pub project_id: ProjectID,
pub section_order: isize,
pub name: String,
}
impl Ord for Section {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.section_order.cmp(&other.section_order) {
core::cmp::Ordering::Equal => {}
ord => return ord,
}
self.id.cmp(&other.id)
}
}
impl PartialOrd for Section {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::fmt::Display for Section {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{} {}",
self.id
.if_supports_color(Stream::Stdout, |text| text.bright_yellow()),
self.name
)
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct CreateSection {
pub name: String,
pub project_id: ProjectID,
#[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<isize>,
}
#[cfg(test)]
impl Section {
pub fn new(id: &str, project_id: &str, name: &str) -> Section {
Section {
id: id.to_string(),
project_id: project_id.to_string(),
name: name.to_string(),
section_order: 0,
}
}
}