Skip to main content

backbone_accounting/domain/entity/
normal_balance.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 = "normal_balance", rename_all = "snake_case")]
11pub enum NormalBalance {
12    Debit,
13    Credit,
14}
15
16impl std::fmt::Display for NormalBalance {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            Self::Debit => write!(f, "debit"),
20            Self::Credit => write!(f, "credit"),
21        }
22    }
23}
24
25impl FromStr for NormalBalance {
26    type Err = String;
27
28    fn from_str(s: &str) -> Result<Self, Self::Err> {
29        match s.to_lowercase().as_str() {
30            "debit" => Ok(Self::Debit),
31            "credit" => Ok(Self::Credit),
32            _ => Err(format!("Unknown NormalBalance variant: {}", s)),
33        }
34    }
35}
36
37impl Default for NormalBalance {
38    fn default() -> Self {
39        Self::Debit
40    }
41}