Skip to content

Commit

Permalink
Merge pull request #4419 from systeminit/fnichol/naxum-extractor-requ…
Browse files Browse the repository at this point in the history
…ired-reply

feat(naxum): add `RequiredReply` extractor
  • Loading branch information
fnichol authored Aug 24, 2024
2 parents eb109df + 3e62312 commit ef48cac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 15 additions & 1 deletion lib/naxum/src/extract/message_parts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use crate::{
MessageHead,
};

use super::{rejection::StringRejection, FromMessage, FromMessageHead};
use super::{
rejection::{NoReplyRejection, StringRejection},
FromMessage, FromMessageHead,
};

#[async_trait]
impl<S, R> FromMessage<S, R> for R
Expand Down Expand Up @@ -45,6 +48,17 @@ impl<S> FromMessageHead<S> for Reply {
}
}

pub struct RequiredReply(pub Subject);

#[async_trait]
impl<S> FromMessageHead<S> for RequiredReply {
type Rejection = NoReplyRejection;

async fn from_message_head(head: &mut Head, _state: &S) -> Result<Self, Self::Rejection> {
Ok(Self(head.reply.clone().ok_or(NoReplyRejection)?))
}
}

pub struct Headers(pub Option<HeaderMap>);

#[async_trait]
Expand Down
9 changes: 9 additions & 0 deletions lib/naxum/src/extract/rejection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ impl error::Error for StringRejection {
}
}

define_rejection! {
#[status_code = 400]
#[body = "Reply was required for message but none was found"]
/// Rejection type for [`RequiredReply`].
///
/// This rejection is used if a reply was expected on a message but one was not provided.
pub struct NoReplyRejection;
}

define_rejection! {
#[status_code = 422]
#[body = "Failed to deserialize the JSON body into the target type"]
Expand Down

0 comments on commit ef48cac

Please sign in to comment.