pub enum Either<L, R> {
Left(L),
Right(R),
}Expand description
Accept one of two extractors, whichever succeeds.
The usual case is an endpoint that takes either JSON or a form:
use churust_core::{Churust, Either, Form, TestClient};
use serde::Deserialize;
#[derive(Deserialize)]
struct Note { text: String }
let app = Churust::server()
.routing(|r| {
r.post("/notes", |body: Either<Form<Note>, String>| async move {
match body {
Either::Left(Form(n)) => format!("form: {}", n.text),
Either::Right(raw) => format!("raw: {raw}"),
}
});
})
.build();
let res = TestClient::new(app)
.post("/notes")
.header("content-type", "application/x-www-form-urlencoded")
.body("text=hi")
.send()
.await;
assert_eq!(res.text(), "form: hi");Left is tried first. If it fails, the call is handed to Right — which is
why the call is cloned rather than moved: a failed first attempt must not
have consumed the body. When both fail, the second error is reported,
since the right-hand side is the fallback and its complaint is usually the
more informative one.
Variants§
Trait Implementations§
Auto Trait Implementations§
impl<L, R> Freeze for Either<L, R>
impl<L, R> RefUnwindSafe for Either<L, R>where
L: RefUnwindSafe,
R: RefUnwindSafe,
impl<L, R> Send for Either<L, R>
impl<L, R> Sync for Either<L, R>
impl<L, R> Unpin for Either<L, R>
impl<L, R> UnsafeUnpin for Either<L, R>where
L: UnsafeUnpin,
R: UnsafeUnpin,
impl<L, R> UnwindSafe for Either<L, R>where
L: UnwindSafe,
R: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more