Skip to main content

bybit/models/return_codes/
earn.rs

1use crate::Display;
2
3/// Enum representing Bybit API V5 Earn error codes.
4///
5/// See: https://bybit-exchange.github.io/docs/v5/error#earn
6#[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    /// Earn product is not supported.
11    EarnProductNotSupported = 160001,
12
13    /// Insufficient balance in account.
14    InsufficientBalance = 160002,
15
16    /// Invalid subscription amount.
17    InvalidSubscriptionAmount = 160003,
18
19    /// Subscription failed.
20    SubscriptionFailed = 160004,
21
22    /// Redemption failed.
23    RedemptionFailed = 160005,
24
25    /// Invalid redemption amount.
26    InvalidRedemptionAmount = 160006,
27
28    /// Earn product is disabled for this account.
29    EarnProductDisabled = 160007,
30
31    /// Earn product order does not exist.
32    EarnOrderNotExist = 160008,
33}
34
35impl EarnCode {
36    /// Converts an error code (as i32) to an EarnError variant.
37    /// Returns None if the code doesn't match any variant or if the string cannot be parsed.
38    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    /// Returns the error message associated with the error code.
57    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}