bybit/models/return_codes/
earn.rs1use crate::Display;
2
3#[derive(Clone, Copy, PartialEq, Eq, Hash, Display, derive_more::Debug, derive_more::From)]
7#[repr(i32)]
8#[display("{} - {}", *self as i32, self.message())]
9pub enum EarnCode {
10 EarnProductNotSupported = 160001,
12
13 InsufficientBalance = 160002,
15
16 InvalidSubscriptionAmount = 160003,
18
19 SubscriptionFailed = 160004,
21
22 RedemptionFailed = 160005,
24
25 InvalidRedemptionAmount = 160006,
27
28 EarnProductDisabled = 160007,
30
31 EarnOrderNotExist = 160008,
33}
34
35impl EarnCode {
36 pub fn from_code<T>(code: T) -> Option<Self>
39 where
40 T: Into<i32> + Copy,
41 {
42 let code = code.into();
43 match code {
44 160001 => Some(Self::EarnProductNotSupported),
45 160002 => Some(Self::InsufficientBalance),
46 160003 => Some(Self::InvalidSubscriptionAmount),
47 160004 => Some(Self::SubscriptionFailed),
48 160005 => Some(Self::RedemptionFailed),
49 160006 => Some(Self::InvalidRedemptionAmount),
50 160007 => Some(Self::EarnProductDisabled),
51 160008 => Some(Self::EarnOrderNotExist),
52 _ => None,
53 }
54 }
55
56 pub fn message(&self) -> &'static str {
58 match self {
59 Self::EarnProductNotSupported => "Earn product is not supported.",
60 Self::InsufficientBalance => "Insufficient balance in account.",
61 Self::InvalidSubscriptionAmount => "Invalid subscription amount.",
62 Self::SubscriptionFailed => "Subscription failed.",
63 Self::RedemptionFailed => "Redemption failed.",
64 Self::InvalidRedemptionAmount => "Invalid redemption amount.",
65 Self::EarnProductDisabled => "Earn product is disabled for this account.",
66 Self::EarnOrderNotExist => "Earn product order does not exist.",
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::*;
74
75 type Sut = EarnCode;
76
77 #[test]
78 fn test_from_code_i32() {
79 assert_eq!(Sut::from_code(160001), Some(Sut::EarnProductNotSupported));
80 assert_eq!(Sut::from_code(160008), Some(Sut::EarnOrderNotExist));
81 assert_eq!(Sut::from_code(99999), None);
82 }
83
84 #[test]
85 fn test_display() {
86 let error = Sut::EarnProductNotSupported;
87 assert_eq!(error.to_string(), "160001 - Earn product is not supported.");
88 let error = Sut::EarnOrderNotExist;
89 assert_eq!(
90 error.to_string(),
91 "160008 - Earn product order does not exist."
92 );
93 }
94}