Skip to content

Commit

Permalink
legacy_response: impl actix responder
Browse files Browse the repository at this point in the history
  • Loading branch information
svenrademakers committed Aug 30, 2023
1 parent b7dc5aa commit 6b7585d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
7 changes: 6 additions & 1 deletion bmcd/src/flash_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,12 @@ impl Error for FlashError {}

impl Display for FlashError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
todo!()
match self {
FlashError::InProgress => write!(f, "flashing operation in progress"),
FlashError::UnexpectedCommand => write!(f, "did not expect that command"),
FlashError::Aborted => write!(f, "flash operation was aborted"),
FlashError::MpscError(_) => write!(f, "internal error sending buffers"),
}
}
}

Expand Down
59 changes: 51 additions & 8 deletions bmcd/src/into_legacy_response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use actix_web::{http::StatusCode, HttpResponse, HttpResponseBuilder};
use actix_web::{http::StatusCode, HttpResponse, HttpResponseBuilder, Responder, ResponseError};
use serde_json::json;
use std::fmt::Display;
///
/// Trait is implemented for all types that implement `Into<LegacyResponse>`
pub trait IntoLegacyResponse {
Expand All @@ -8,6 +9,7 @@ pub trait IntoLegacyResponse {

/// Specifies the different repsonses that this legacy API can return. Implements
/// `From<LegacyResponse>` to enforce the legacy json format in the return body.
#[derive(Debug, PartialEq)]
pub enum LegacyResponse {
Success(Option<serde_json::Value>),
Error(StatusCode, &'static str),
Expand All @@ -23,7 +25,7 @@ impl LegacyResponse {
LegacyResponse::Error(StatusCode::NOT_IMPLEMENTED, msg)
}

pub fn stub() -> Self {
pub fn success() -> Self {
LegacyResponse::Success(None)
}
}
Expand All @@ -34,12 +36,6 @@ impl<T: Into<LegacyResponse>> IntoLegacyResponse for T {
}
}

impl IntoLegacyResponse for () {
fn legacy_response(self) -> LegacyResponse {
LegacyResponse::Success(None)
}
}

impl<T: IntoLegacyResponse, E: IntoLegacyResponse> From<Result<T, E>> for LegacyResponse {
fn from(value: Result<T, E>) -> Self {
value.map_or_else(|e| e.legacy_response(), |ok| ok.legacy_response())
Expand All @@ -64,6 +60,12 @@ impl From<serde_json::Value> for LegacyResponse {
}
}

impl From<()> for LegacyResponse {
fn from(_: ()) -> Self {
LegacyResponse::Success(None)
}
}

impl From<anyhow::Error> for LegacyResponse {
fn from(e: anyhow::Error) -> Self {
LegacyResponse::ErrorOwned(
Expand All @@ -73,6 +75,30 @@ impl From<anyhow::Error> for LegacyResponse {
}
}

impl ResponseError for LegacyResponse {}

impl Display for LegacyResponse {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
LegacyResponse::Success(s) => write!(
f,
"success: {}",
s.as_ref().map(|json| json.to_string()).unwrap_or_default()
),
LegacyResponse::Error(code, msg) => write!(f, "{}:{}", code, msg),
LegacyResponse::ErrorOwned(code, msg) => write!(f, "{}:{}", code, msg),
}
}
}

impl Responder for LegacyResponse {
type Body = <HttpResponse as Responder>::Body;

fn respond_to(self, _req: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
self.into()
}
}

pub type LegacyResult<T> = Result<T, LegacyResponse>;

impl From<LegacyResponse> for HttpResponse {
Expand All @@ -97,3 +123,20 @@ impl From<LegacyResponse> for HttpResponse {
HttpResponseBuilder::new(response).json(msg)
}
}

#[derive(Default)]
pub struct Null;

impl Responder for Null {
type Body = <HttpResponse as Responder>::Body;

fn respond_to(self, _: &actix_web::HttpRequest) -> HttpResponse<Self::Body> {
HttpResponse::Ok().into()
}
}

impl From<()> for Null {
fn from(_: ()) -> Self {
Null {}
}
}

0 comments on commit 6b7585d

Please sign in to comment.