Skip to main content

backbone_accounting/domain/entity/
journal_source.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 = "journal_source", rename_all = "snake_case")]
11pub enum JournalSource {
12    Manual,
13    Order,
14    Payment,
15    Settlement,
16    Adjustment,
17    Import,
18    System,
19    Recurring,
20}
21
22impl std::fmt::Display for JournalSource {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            Self::Manual => write!(f, "manual"),
26            Self::Order => write!(f, "order"),
27            Self::Payment => write!(f, "payment"),
28            Self::Settlement => write!(f, "settlement"),
29            Self::Adjustment => write!(f, "adjustment"),
30            Self::Import => write!(f, "import"),
31            Self::System => write!(f, "system"),
32            Self::Recurring => write!(f, "recurring"),
33        }
34    }
35}
36
37impl FromStr for JournalSource {
38    type Err = String;
39
40    fn from_str(s: &str) -> Result<Self, Self::Err> {
41        match s.to_lowercase().as_str() {
42            "manual" => Ok(Self::Manual),
43            "order" => Ok(Self::Order),
44            "payment" => Ok(Self::Payment),
45            "settlement" => Ok(Self::Settlement),
46            "adjustment" => Ok(Self::Adjustment),
47            "import" => Ok(Self::Import),
48            "system" => Ok(Self::System),
49            "recurring" => Ok(Self::Recurring),
50            _ => Err(format!("Unknown JournalSource variant: {}", s)),
51        }
52    }
53}
54
55impl Default for JournalSource {
56    fn default() -> Self {
57        Self::Manual
58    }
59}