use owo_colors::{OwoColorize, Stream};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
pub type LabelID = String;
#[serde_as]
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Clone)]
pub struct Label {
pub id: LabelID,
pub name: String,
pub color: String,
pub order: isize,
pub is_favorite: bool,
}
impl Ord for Label {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.order.cmp(&other.order) {
core::cmp::Ordering::Equal => {}
ord => return ord,
}
self.id.cmp(&other.id)
}
}
impl PartialOrd for Label {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl std::fmt::Display for Label {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
format!("@{}", self.name)
.if_supports_color(Stream::Stdout, |text| text.bright_blue())
.fmt(f)
}
}
#[derive(Debug, Serialize, Deserialize, Default)]
pub struct CreateLabel {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub order: Option<isize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub color: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_favorite: Option<bool>,
}
#[cfg(test)]
impl Label {
pub fn new(id: &str, name: &str) -> Label {
Label {
id: id.to_string(),
name: name.to_string(),
color: "".to_string(),
order: 0,
is_favorite: false,
}
}
}
#[cfg(test)]
mod test {
#[test]
fn succeeds_with_bad_color() {
let label = r#"{"id":"123","name":"hello","color":"wow","order":0,"is_favorite":false}"#;
assert!(serde_json::from_str::<'_, super::Label>(label).is_ok());
}
}