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(())
}
}