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
|
use axum::response::sse::Event;
use serde::{Deserialize, Serialize};
pub type Receiver = tokio::sync::watch::Receiver<Status>;
pub type Sender = tokio::sync::watch::Sender<Status>;
pub use stream::{NamedStatusStream, StatusStream};
pub mod stream;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(rename_all = "lowercase", tag = "status", content = "output")]
pub enum Status {
Ok,
Error(Option<String>),
}
impl Default for Status {
fn default() -> Self {
Status::Error(None)
}
}
impl Status {
pub fn update(&mut self, status: Status) -> bool {
let modif = *self != status;
if modif {
*self = status;
}
modif
}
}
impl<T, E: std::error::Error> From<Result<T, E>> for Status {
fn from(value: Result<T, E>) -> Self {
match value {
Ok(_) => Status::Ok,
Err(err) => Status::Error(Some(err.to_string())),
}
}
}
impl axum::response::IntoResponse for Status {
fn into_response(self) -> axum::response::Response {
axum::Json(self).into_response()
}
}
impl From<Status> for Event {
fn from(value: Status) -> Self {
match value {
Status::Ok => Event::default().event("ok"),
Status::Error(None) => Event::default().event("error"),
Status::Error(Some(msg)) => Event::default().event("error").data(msg),
}
}
}
|