fluvio_sc_schema/
response.rs

1#![allow(clippy::assign_op_pattern)]
2
3//!
4//! # Response Message
5//!
6//! Response sent to client. Sends entity name, error code and error message.
7//!
8use std::fmt::Display;
9
10use fluvio_protocol::{Encoder, Decoder};
11use crate::errors::ErrorCode;
12
13use crate::ApiError;
14
15#[derive(Encoder, Decoder, Default, Debug)]
16pub struct Status {
17    pub name: String,
18    pub error_code: ErrorCode,
19    pub error_message: Option<String>,
20}
21
22impl Display for Status {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        if self.error_code.is_ok() {
25            write!(f, "{}", self.name)
26        } else {
27            write!(f, "{}: {}", self.name, self.error_code)
28        }
29    }
30}
31
32impl Status {
33    pub fn new_ok(name: String) -> Self {
34        Self {
35            name,
36            error_code: ErrorCode::None,
37            error_message: None,
38        }
39    }
40
41    pub fn new(name: String, code: ErrorCode, msg: Option<String>) -> Self {
42        Self {
43            name,
44            error_code: code,
45            error_message: msg,
46        }
47    }
48
49    pub fn is_error(&self) -> bool {
50        self.error_code.is_error()
51    }
52
53    #[allow(clippy::wrong_self_convention)]
54    pub fn as_result(self) -> Result<(), ApiError> {
55        if self.error_code.is_ok() {
56            Ok(())
57        } else {
58            Err(ApiError::Code(self.error_code, self.error_message))
59        }
60    }
61}