smsir_rust/
result.rs

1
2use serde::{Deserialize, Deserializer};
3
4#[derive(Debug, Clone, Deserialize)]
5pub struct SmsIrResult {
6    #[serde(deserialize_with = "deserialize_status")]
7    pub status: i32,
8    pub message: String,
9    pub data: Option<serde_json::Value>,
10}
11
12fn deserialize_status<'de, D>(deserializer: D) -> Result<i32, D::Error>
13where
14    D: Deserializer<'de>,
15{
16    use serde::de::Error;
17    let val = serde_json::Value::deserialize(deserializer)?;
18    match val {
19        serde_json::Value::Number(n) => n.as_i64().map(|v| v as i32).ok_or_else(|| Error::custom("Invalid number for status")),
20        serde_json::Value::String(s) => s.parse::<i32>().map_err(|_| Error::custom("Invalid string for status")),
21        _ => Err(Error::custom("Unexpected type for status")),
22    }
23}
24
25impl SmsIrResult {
26    pub fn new(status: i32, message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
27        Self {
28            status,
29            message: message.into(),
30            data,
31        }
32    }
33}
34
35/// Status codes for send operations
36#[derive(Debug, Clone, Copy, PartialEq, Eq)]
37pub enum SendStatus {
38    Success = 1,
39    SystemError = 0,
40    InvalidApiKey = 10,
41    ApiKeyDisabled = 11,
42    ApiKeyIpRestricted = 12,
43    UserDisabled = 13,
44    UserSuspended = 14,
45    TooManyRequests = 20,
46    InvalidLineNumber = 101,
47    InsufficientCredit = 102,
48    EmptyText = 103,
49    InvalidMobile = 104,
50    TooManyMobiles = 105,
51    TooManyTexts = 106,
52    EmptyMobiles = 107,
53    EmptyTexts = 108,
54    InvalidSendTime = 109,
55    MobilesTextsCountMismatch = 110,
56    NotFoundById = 111,
57    NoRecordToDelete = 112,
58    TemplateNotFound = 113,
59    ParamTooLong = 114,
60    MobilesInBlacklist = 115,
61    ParamNameEmpty = 116,
62    TextNotApproved = 117,
63    TooManyMessages = 118,
64    UpgradePlanForTemplate = 119,
65    SenderNeedsActivation = 123,
66    Unknown,
67}
68
69impl SendStatus {
70    pub fn from_i32(code: i32) -> Self {
71        use SendStatus::*;
72        match code {
73            1 => Success,
74            0 => SystemError,
75            10 => InvalidApiKey,
76            11 => ApiKeyDisabled,
77            12 => ApiKeyIpRestricted,
78            13 => UserDisabled,
79            14 => UserSuspended,
80            20 => TooManyRequests,
81            101 => InvalidLineNumber,
82            102 => InsufficientCredit,
83            103 => EmptyText,
84            104 => InvalidMobile,
85            105 => TooManyMobiles,
86            106 => TooManyTexts,
87            107 => EmptyMobiles,
88            108 => EmptyTexts,
89            109 => InvalidSendTime,
90            110 => MobilesTextsCountMismatch,
91            111 => NotFoundById,
92            112 => NoRecordToDelete,
93            113 => TemplateNotFound,
94            114 => ParamTooLong,
95            115 => MobilesInBlacklist,
96            116 => ParamNameEmpty,
97            117 => TextNotApproved,
98            118 => TooManyMessages,
99            119 => UpgradePlanForTemplate,
100            123 => SenderNeedsActivation,
101            _ => Unknown,
102        }
103    }
104}
105
106/// Status codes for delivery operations
107#[derive(Debug, Clone, Copy, PartialEq, Eq)]
108pub enum DeliveryStatus {
109    Delivered = 1,
110    NotDelivered = 2,
111    ProcessingInOperator = 3,
112    NotDeliveredToOperator = 4,
113    DeliveredToOperator = 5,
114    Error = 6,
115    Blacklist = 7,
116    Unknown,
117}
118
119impl DeliveryStatus {
120    pub fn from_i32(code: i32) -> Self {
121        use DeliveryStatus::*;
122        match code {
123            1 => Delivered,
124            2 => NotDelivered,
125            3 => ProcessingInOperator,
126            4 => NotDeliveredToOperator,
127            5 => DeliveredToOperator,
128            6 => Error,
129            7 => Blacklist,
130            _ => Unknown,
131        }
132    }
133}