tap_node/storage/
models.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3
4#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
5#[serde(rename_all = "snake_case")]
6pub enum TransactionType {
7    Transfer,
8    Payment,
9}
10
11impl fmt::Display for TransactionType {
12    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
13        match self {
14            TransactionType::Transfer => write!(f, "transfer"),
15            TransactionType::Payment => write!(f, "payment"),
16        }
17    }
18}
19
20impl TryFrom<&str> for TransactionType {
21    type Error = String;
22
23    fn try_from(value: &str) -> Result<Self, Self::Error> {
24        match value {
25            "transfer" => Ok(TransactionType::Transfer),
26            "payment" => Ok(TransactionType::Payment),
27            _ => Err(format!("Invalid transaction type: {}", value)),
28        }
29    }
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
33#[serde(rename_all = "snake_case")]
34pub enum TransactionStatus {
35    Pending,
36    Confirmed,
37    Failed,
38    Cancelled,
39    Reverted,
40}
41
42impl fmt::Display for TransactionStatus {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        match self {
45            TransactionStatus::Pending => write!(f, "pending"),
46            TransactionStatus::Confirmed => write!(f, "confirmed"),
47            TransactionStatus::Failed => write!(f, "failed"),
48            TransactionStatus::Cancelled => write!(f, "cancelled"),
49            TransactionStatus::Reverted => write!(f, "reverted"),
50        }
51    }
52}
53
54impl TryFrom<&str> for TransactionStatus {
55    type Error = String;
56
57    fn try_from(value: &str) -> Result<Self, Self::Error> {
58        match value {
59            "pending" => Ok(TransactionStatus::Pending),
60            "confirmed" => Ok(TransactionStatus::Confirmed),
61            "failed" => Ok(TransactionStatus::Failed),
62            "cancelled" => Ok(TransactionStatus::Cancelled),
63            "reverted" => Ok(TransactionStatus::Reverted),
64            _ => Err(format!("Invalid transaction status: {}", value)),
65        }
66    }
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct Transaction {
71    pub id: i64,
72    pub transaction_type: TransactionType,
73    pub reference_id: String,
74    pub from_did: Option<String>,
75    pub to_did: Option<String>,
76    pub thread_id: Option<String>,
77    pub message_type: String,
78    pub status: TransactionStatus,
79    pub message_json: serde_json::Value,
80    pub created_at: String,
81    pub updated_at: String,
82}
83
84#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
85#[serde(rename_all = "snake_case")]
86pub enum MessageDirection {
87    Incoming,
88    Outgoing,
89}
90
91impl fmt::Display for MessageDirection {
92    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
93        match self {
94            MessageDirection::Incoming => write!(f, "incoming"),
95            MessageDirection::Outgoing => write!(f, "outgoing"),
96        }
97    }
98}
99
100impl TryFrom<&str> for MessageDirection {
101    type Error = String;
102
103    fn try_from(value: &str) -> Result<Self, Self::Error> {
104        match value {
105            "incoming" => Ok(MessageDirection::Incoming),
106            "outgoing" => Ok(MessageDirection::Outgoing),
107            _ => Err(format!("Invalid message direction: {}", value)),
108        }
109    }
110}
111
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct Message {
114    pub id: i64,
115    pub message_id: String,
116    pub message_type: String,
117    pub from_did: Option<String>,
118    pub to_did: Option<String>,
119    pub thread_id: Option<String>,
120    pub parent_thread_id: Option<String>,
121    pub direction: MessageDirection,
122    pub message_json: serde_json::Value,
123    pub created_at: String,
124}