diff options
author | Toby Vincent <tobyv@tobyvin.dev> | 2024-10-09 18:23:58 -0500 |
---|---|---|
committer | Toby Vincent <tobyv@tobyvin.dev> | 2024-10-09 18:23:58 -0500 |
commit | b94f8e694bf01f5dba9ce2c01f589463a3dfbc69 (patch) | |
tree | c787530e63fb510db31533166edf1b9ff54be62a /src/status.rs | |
parent | 117d33fc478bf529094850b1fe40c558f04c9865 (diff) |
feat!: rewrite to use traits and streams
Diffstat (limited to 'src/status.rs')
-rw-r--r-- | src/status.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/status.rs b/src/status.rs new file mode 100644 index 0000000..6e674c8 --- /dev/null +++ b/src/status.rs @@ -0,0 +1,50 @@ +use axum::response::sse::Event; +use serde::{Deserialize, Serialize}; + +#[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), + } + } +} |