use arrow_schema::Field;
use conundrum_lang::ecosystem::{db::db_traits::db_field::DatabaseField, error_handling::db_error::DatabaseError};
use fake::Dummy;
use serde::{Deserialize, Serialize};
use std::{fmt::Display, str::FromStr};
#[derive(Clone, Serialize, Deserialize, Debug, specta::Type, Dummy)]
#[serde(tag = "tag", content = "content", try_from = "String")]
pub enum AssignmentStatus {
ToDo,
InProgress,
UpNext,
OnHold,
Complete,
Archived,
Custom(String),
}
impl Display for AssignmentStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let s = match self {
Self::ToDo => "to_do",
Self::InProgress => "in_progress",
Self::UpNext => "up_next",
Self::OnHold => "on_hold",
Self::Complete => "complete",
Self::Archived => "archived",
Self::Custom(s) => s.as_str(),
};
write!(f, "{}", s)
}
}
impl FromStr for AssignmentStatus {
type Err = DatabaseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"to_do" => Self::ToDo,
"in_progress" => Self::InProgress,
"up_next" => Self::UpNext,
"on_hold" => Self::OnHold,
"complete" => Self::Complete,
"archived" => Self::Archived,
_ => Self::Custom(s.to_string()),
})
}
}
impl TryFrom<String> for AssignmentStatus {
type Error = DatabaseError;
fn try_from(value: String) -> Result<Self, Self::Error> {
Self::from_str(value.as_str())
}
}
impl DatabaseField for AssignmentStatus {
fn field_definition(field_key: &'static str, nullable: bool) -> arrow_schema::Field {
Field::new(field_key.to_string(), arrow_schema::DataType::Utf8, nullable)
}
}