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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
use std::error::Error;
use std::fmt::{self, Display, Formatter};

use serde::{Deserialize, Serialize};

use crate::serial::request_message::RequestMessage;

#[derive(Debug, Serialize, Deserialize)]
pub struct Status {
    pub err_code: i32,
    pub err_msg: String,
    #[serde(skip)]
    pub(crate) cached_size: protobuf::rt::CachedSize,
}

impl Status {
    pub fn new(err_code: i32, err_msg: String) -> Status {
        Status {
            err_code,
            err_msg,
            cached_size: protobuf::rt::CachedSize::new(),
        }
    }

    pub fn error(err_msg: String) -> Status {
        Status {
            err_code: 1,
            err_msg,
            cached_size: protobuf::rt::CachedSize::new(),
        }
    }
    

    pub fn is_erorr(&self) -> bool {
        self.err_code != 0
    }

    pub fn compute_size_with_tag_and_len_return_buffer(&self) -> Vec<u8> {
        let size = self.compute_size_with_tag_and_len();
        let mut buffer: Vec<u8> = Vec::with_capacity(size as usize);
        let mut os = protobuf::CodedOutputStream::vec(&mut buffer);
        self.serial_with_tag_and_len(&mut os);
        drop(os);
        buffer
    }
}

impl Display for Status {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "err_code:{}, err_msg: {}", self.err_code, self.err_msg)
    }
}

impl Error for Status {}

impl Default for Status {
    fn default() -> Self {
        Status {
            err_code: 0,
            err_msg: String::default(),
            cached_size: protobuf::rt::CachedSize::new(),
        }
    }
}

impl RequestMessage for Status {
    fn compute_size(&self) -> u64 {
        let mut my_size = 0;
        my_size += protobuf::rt::int32_size(1, self.err_code);

        my_size += protobuf::rt::string_size(2, &self.err_msg);

        self.cached_size.set(my_size as u32);
        my_size
    }

    fn serial_with_output_stream(
        &self,
        os: &mut protobuf::CodedOutputStream<'_>,
    ) -> Result<(), Status> {
        os.write_int32(1, self.err_code).unwrap();

        os.write_string(2, &self.err_msg).unwrap();

        Ok(())
    }

    fn parse_from_input_stream(
        &mut self,
        is: &mut protobuf::CodedInputStream<'_>,
    ) -> Result<(), Status> {
        while let Some(tag) = is.read_raw_tag_or_eof().unwrap() {
            match tag {
                8 => {
                    self.err_code = is.read_int32().unwrap();
                }
                18 => {
                    self.err_msg = is.read_string().unwrap();
                }
                _ => {}
            }
        }
        Ok(())
    }
}