From b94f8e694bf01f5dba9ce2c01f589463a3dfbc69 Mon Sep 17 00:00:00 2001 From: Toby Vincent Date: Wed, 9 Oct 2024 18:23:58 -0500 Subject: feat!: rewrite to use traits and streams --- src/status.rs | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/status.rs (limited to 'src/status.rs') 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), +} + +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 From> for Status { + fn from(value: Result) -> 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 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), + } + } +} -- cgit v1.2.3-70-g09d2