kf_protocol_api/
flv_errors.rs

1//!
2//! # Fluvio Error Codes
3//!
4//! Error code definitions described here.
5//!
6use serde::Serialize;
7
8use flv_util::string_helper::upper_cammel_case_to_sentence;
9use kf_protocol_derive::Encode;
10use kf_protocol_derive::Decode;
11
12// -----------------------------------
13// Error Definition & Implementation
14// -----------------------------------
15
16#[fluvio_kf(encode_discriminant)]
17#[repr(i16)]
18#[derive(Encode, Decode, PartialEq, Debug, Clone, Copy, Serialize)]
19pub enum FlvErrorCode {
20    // Not an error
21    None = 0,
22
23    // Spu errors
24    SpuError = 1,
25    SpuRegisterationFailed = 2,
26    SpuOffline = 3,
27    SpuNotFound = 4,
28    SpuAlreadyExists = 5,
29
30    // Topic errors
31    TopicError = 6,
32    TopicNotFound = 7,
33    TopicAlreadyExists = 8,
34    TopicPendingInitialization = 9,
35    TopicInvalidConfiguration = 10,
36
37    // Partition errors
38    PartitionPendingInitialization = 11,
39    PartitionNotLeader = 12,
40}
41
42impl Default for FlvErrorCode {
43    fn default() -> FlvErrorCode {
44        FlvErrorCode::None
45    }
46}
47
48impl FlvErrorCode {
49
50    pub fn is_ok(&self) -> bool {
51        match self {
52            Self::None => true,
53            _ => false
54        }
55    }
56
57    pub fn to_sentence(&self) -> String {
58        match self {
59            FlvErrorCode::None => "".to_owned(),
60            _ => upper_cammel_case_to_sentence(format!("{:?}", self), true),
61        }
62    }
63
64    pub fn is_error(&self) -> bool {
65        !self.is_ok()
66    }
67}
68
69// -----------------------------------
70// Unit Tests
71// -----------------------------------
72
73#[cfg(test)]
74mod test {
75
76    use std::convert::TryInto;
77
78    use super::FlvErrorCode;
79
80    #[test]
81    fn test_flv_error_code_from_conversion() {
82        let erro_code: FlvErrorCode = (2 as i16).try_into().expect("convert");
83        assert_eq!(erro_code, FlvErrorCode::SpuRegisterationFailed);
84    }
85}