Enum ajars_actix_web::actix_web::Either [−][src]
pub enum Either<L, R> { Left(L), Right(R), }
Expand description
Combines two extractor or responder types into a single type.
Can be converted to and from an [either::Either
].
Extractor
Provides a mechanism for trying two extractors, a primary and a fallback. Useful for “polymorphic payloads” where, for example, a form might be JSON or URL encoded.
It is important to note that this extractor, by necessity, buffers the entire request payload
as part of its implementation. Though, it does respect any PayloadConfig
maximum size limits.
use actix_web::{post, web, Either}; use serde::Deserialize; #[derive(Deserialize)] struct Info { name: String, } // handler that accepts form as JSON or form-urlencoded. #[post("/")] async fn index(form: Either<web::Json<Info>, web::Form<Info>>) -> String { let name: String = match form { Either::Left(json) => json.name.to_owned(), Either::Right(form) => form.name.to_owned(), }; format!("Welcome {}!", name) }
Responder
It may be desireable to use a concrete type for a response with multiple branches. As long as
both types implement Responder
, so will the Either
type, enabling it to be used as a
handler’s return type.
All properties of a response are determined by the Responder branch returned.
use actix_web::{get, Either, Error, HttpResponse}; #[get("/")] async fn index() -> Either<&'static str, Result<HttpResponse, Error>> { if 1 == 2 { // respond with Left variant Either::Left("Bad data") } else { // respond with Right variant Either::Right( Ok(HttpResponse::Ok() .content_type(mime::TEXT_HTML) .body("<p>Hello!</p>")) ) } }
Variants
A value of type L
.
A value of type R
.
Implementations
Trait Implementations
impl<L, R> FromRequest for Either<L, R> where
R: FromRequest + 'static,
L: FromRequest + 'static,
impl<L, R> FromRequest for Either<L, R> where
R: FromRequest + 'static,
L: FromRequest + 'static,
See here for example of usage as an extractor.
type Error = EitherExtractError<<L as FromRequest>::Error, <R as FromRequest>::Error>
type Error = EitherExtractError<<L as FromRequest>::Error, <R as FromRequest>::Error>
The associated error which can be returned.
type Future = EitherExtractFut<L, R>
type Future = EitherExtractFut<L, R>
Future that resolves to a Self.
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Either<L, R> as FromRequest>::Future
pub fn from_request(
req: &HttpRequest,
payload: &mut Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
) -> <Either<L, R> as FromRequest>::Future
Create a Self from request parts asynchronously.
Create a Self from request head asynchronously. Read more
See here for example of usage as a handler return type.
pub fn respond_to(self, req: &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>
impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
pub fn respond_to(self, req: &HttpRequest) -> HttpResponse<AnyBody>ⓘNotable traits for HttpResponse<AnyBody>
impl Future for HttpResponse<AnyBody> type Output = Result<Response<AnyBody>, Error>;
Convert self to HttpResponse
.
Override a status code for a Responder. Read more
Insert header to the final response. Read more
Auto Trait Implementations
impl<L, R> RefUnwindSafe for Either<L, R> where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> UnwindSafe for Either<L, R> where
L: UnwindSafe,
R: UnwindSafe,