1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//!
//! # Fluvio Error Codes
//!
//! Error code definitions described here.
//!
use serde::Serialize;

use flv_util::string_helper::upper_cammel_case_to_sentence;
use kf_protocol_derive::Encode;
use kf_protocol_derive::Decode;

// -----------------------------------
// Error Definition & Implementation
// -----------------------------------

#[fluvio_kf(encode_discriminant)]
#[repr(i16)]
#[derive(Encode, Decode, PartialEq, Debug, Clone, Copy, Serialize)]
pub enum FlvErrorCode {
    // Not an error
    None = 0,

    // Spu errors
    SpuError = 1,
    SpuRegisterationFailed = 2,
    SpuOffline = 3,
    SpuNotFound = 4,
    SpuAlreadyExists = 5,

    // Topic errors
    TopicError = 6,
    TopicNotFound = 7,
    TopicAlreadyExists = 8,
    TopicPendingInitialization = 9,
    TopicInvalidConfiguration = 10,

    // Partition errors
    PartitionPendingInitialization = 11,
    PartitionNotLeader = 12,
}

impl Default for FlvErrorCode {
    fn default() -> FlvErrorCode {
        FlvErrorCode::None
    }
}

impl FlvErrorCode {

    pub fn is_ok(&self) -> bool {
        match self {
            Self::None => true,
            _ => false
        }
    }

    pub fn to_sentence(&self) -> String {
        match self {
            FlvErrorCode::None => "".to_owned(),
            _ => upper_cammel_case_to_sentence(format!("{:?}", self), true),
        }
    }

    pub fn is_error(&self) -> bool {
        !self.is_ok()
    }
}

// -----------------------------------
// Unit Tests
// -----------------------------------

#[cfg(test)]
mod test {

    use std::convert::TryInto;

    use super::FlvErrorCode;

    #[test]
    fn test_flv_error_code_from_conversion() {
        let erro_code: FlvErrorCode = (2 as i16).try_into().expect("convert");
        assert_eq!(erro_code, FlvErrorCode::SpuRegisterationFailed);
    }
}