Skip to main content

updatehub_sdk/api/
mod.rs

1// Copyright (C) 2019 O.S. Systems Sofware LTDA
2//
3// SPDX-License-Identifier: Apache-2.0
4
5//! Contains all the structures of the request and response
6//! from the agent.
7
8/// Body of `info` response.
9pub mod info;
10
11/// Body of `probe` request and response.
12pub mod probe {
13    use serde::{Deserialize, Serialize};
14
15    #[derive(Clone, Debug, Deserialize, Serialize)]
16    #[serde(deny_unknown_fields)]
17    pub struct Request {
18        pub custom_server: String,
19    }
20
21    #[derive(Clone, Debug, Deserialize, Serialize)]
22    #[serde(rename_all = "snake_case")]
23    pub enum Response {
24        Updating,
25        NoUpdate,
26        TryAgain(i64),
27    }
28}
29
30/// Body of `local_install` request.
31pub mod local_install {
32    use serde::{Deserialize, Serialize};
33
34    #[derive(Deserialize, Clone, Debug, Serialize)]
35    #[serde(deny_unknown_fields)]
36    pub struct Request {
37        pub file: std::path::PathBuf,
38    }
39}
40
41/// Body of `remote_install` request.
42pub mod remote_install {
43    use serde::{Deserialize, Serialize};
44
45    #[derive(Deserialize, Clone, Debug, Serialize)]
46    #[serde(deny_unknown_fields)]
47    pub struct Request {
48        pub url: String,
49    }
50}
51
52/// Body of `state` response.
53pub mod state {
54    use serde::{Deserialize, Serialize};
55
56    #[derive(Clone, Debug, Deserialize, Serialize)]
57    #[serde(rename_all = "lowercase")]
58    pub enum Response {
59        Park,
60        EntryPoint,
61        Poll,
62        Probe,
63        Validation,
64        Download,
65        Install,
66        Reboot,
67        DirectDownload,
68        PrepareLocalInstall,
69        Error,
70    }
71}
72
73/// Body of `abort_download` response.
74///
75/// # Successful case
76///
77/// On a successful request, the body of response is a struct
78/// called `Response` with a successful message.
79///
80/// # Failed case
81///
82/// On a failed request, the body of response is a struct
83/// called `Refused` with a error message.
84pub mod abort_download {
85    use serde::{Deserialize, Serialize};
86
87    #[derive(Clone, Debug, Deserialize, Serialize)]
88    #[serde(deny_unknown_fields)]
89    pub struct Response {
90        pub message: String,
91    }
92
93    #[derive(Clone, Debug, Deserialize, Serialize)]
94    #[serde(deny_unknown_fields)]
95    pub struct Refused {
96        pub error: String,
97    }
98}
99
100/// Body of `log` response.
101pub mod log {
102    use serde::{Deserialize, Serialize};
103    use std::collections::HashMap;
104
105    #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
106    #[serde(rename_all = "lowercase")]
107    pub enum Level {
108        // We use alias here since some string conversions of slog::Level use
109        // short names even when requesting the full name:
110        // https://github.com/slog-rs/slog/issues/282
111        #[serde(alias = "crit")]
112        Critical,
113        #[serde(alias = "erro")]
114        Error,
115        #[serde(alias = "warn")]
116        Warning,
117        #[serde(alias = "info")]
118        Info,
119        #[serde(alias = "debg")]
120        Debug,
121        #[serde(alias = "trce")]
122        Trace,
123    }
124
125    #[derive(Clone, Debug, Deserialize, Serialize)]
126    pub struct Log {
127        pub entries: Vec<Entry>,
128        /// Absolute index of `entries[0]` among all entries ever recorded by
129        /// the agent. It grows monotonically as entries are evicted and
130        /// as a new operation starts recording, so a reader can resume
131        /// from where it stopped without comparing entries.
132        #[serde(default)]
133        pub first_index: usize,
134    }
135
136    #[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
137    #[serde(deny_unknown_fields)]
138    pub struct Entry {
139        level: Level,
140        message: String,
141        time: String,
142        data: HashMap<String, String>,
143    }
144
145    impl core::fmt::Display for Log {
146        fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
147            for entry in &self.entries {
148                writeln!(f, "{}", entry)?;
149            }
150            Ok(())
151        }
152    }
153
154    impl core::fmt::Display for Entry {
155        fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
156            let level = match self.level {
157                Level::Critical => "CRIT",
158                Level::Error => "ERRO",
159                Level::Warning => "WARN",
160                Level::Info => "INFO",
161                Level::Debug => "DEBG",
162                Level::Trace => "TRCE",
163            };
164
165            write!(
166                f,
167                "{timestamp} {level} {msg}",
168                timestamp = self.time,
169                level = level,
170                msg = self.message
171            )?;
172            Ok(())
173        }
174    }
175}