openconfiguration/
status.rs

1use serde::{Deserialize, Serialize};
2use serde_repr::*;
3
4use crate::impl_visitable_noop;
5
6#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
7#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
8#[cfg_attr(feature = "schema", schemars(deny_unknown_fields))]
9#[serde(rename_all = "camelCase")]
10/// Should be handled as additional information for e.g. debugging.
11/// Contains information if an object is valid.
12/// If not, a status code and maybe an additional message inform about what is wrong.
13pub struct Status {
14    /// Describes status in short.
15    /// Default is 200, no errors detected.
16    pub code: StatusCode,
17
18    /// Optional message string contains additional information to better understand the status code.
19    pub message: Option<String>,
20}
21
22impl_visitable_noop!(Status);
23
24impl Status {
25    pub fn new_ok() -> Status {
26        Status {
27            code: StatusCode::Ok,
28            message: None,
29        }
30    }
31
32    pub fn new(code: StatusCode, message: Option<String>) -> Status {
33        Status { code, message }
34    }
35}
36
37impl Default for Status {
38    fn default() -> Self {
39        Status::new_ok()
40    }
41}
42
43#[derive(Serialize_repr, Deserialize_repr, Debug, PartialEq, Clone, Copy)]
44#[cfg_attr(feature = "schema", derive(schemars::JsonSchema_repr))]
45#[repr(u16)]
46/// Defines status codes on product and scene level.
47/// 200 is default. All other codes than 200 are errors.
48/// Codes <= 1000 are reserved except the predefined ones.
49/// Codes > 1000 are user defined error codes.
50pub enum StatusCode {
51    /// No errors detected.
52    Ok = 200,
53
54    /// Not sufficient defined or invalid parameters.
55    BadRequest = 400,
56
57    /// Data for further processing not found.
58    NotFound = 404,
59
60    /// One or more errors occurred.
61    Conflict = 409,
62
63    /// An error during data processing occurred.
64    InternalServerError = 500,
65
66    /// Service for data processing/gathering not available.
67    ServiceUnavailable = 503,
68}