use actix_web::{error, Error, HttpRequest, HttpResponse, HttpResponseBuilder}; use serde::ser::SerializeStruct; use serde::{Serialize, Serializer}; use serde_json::json; pub mod v1; enum EmgauwaJsonPayLoadError { Error(error::JsonPayloadError), } impl Serialize for EmgauwaJsonPayLoadError { fn serialize(&self, serializer: S) -> Result where S: Serializer, { let mut s = serializer.serialize_struct("error", 3)?; s.serialize_field("type", "json-payload-error")?; s.serialize_field("code", &400)?; s.serialize_field( "description", &match self { EmgauwaJsonPayLoadError::Error(err) => format!("{}", err), }, )?; s.end() } } pub fn json_error_handler(err: error::JsonPayloadError, _: &HttpRequest) -> Error { error::InternalError::from_response( "", HttpResponse::BadRequest() .content_type("application/json") .json(EmgauwaJsonPayLoadError::Error(err)), ) .into() } pub trait EmgauwaMessage { fn emgauwa_message(self, message: &str) -> HttpResponse; } impl EmgauwaMessage for HttpResponseBuilder { fn emgauwa_message(mut self, message: &str) -> HttpResponse { self.json(json!({ "message": message })) } }