Skip to main content

backbone_inventory/domain/entity/
scrap_state.rs

1use serde::{Deserialize, Serialize};
2use sqlx::Type;
3use std::str::FromStr;
4#[cfg(feature = "openapi")]
5use utoipa::ToSchema;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Type)]
8#[cfg_attr(feature = "openapi", derive(ToSchema))]
9#[serde(rename_all = "snake_case")]
10#[sqlx(type_name = "scrap_state", rename_all = "snake_case")]
11pub enum ScrapState {
12    Draft,
13    Done,
14}
15
16impl std::fmt::Display for ScrapState {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Draft => write!(f, "draft"),
20            Self::Done => write!(f, "done"),
21        }
22    }
23}
24
25impl FromStr for ScrapState {
26    type Err = String;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s.to_lowercase().as_str() {
30            "draft" => Ok(Self::Draft),
31            "done" => Ok(Self::Done),
32            _ => Err(format!("Unknown ScrapState variant: {}", s)),
33        }
34    }
35}
36
37impl Default for ScrapState {
38    fn default() -> Self {
39        Self::Draft
40    }
41}