53 lines
1.4 KiB
Rust
53 lines
1.4 KiB
Rust
use std::convert::Infallible;
|
|
use std::ops::FromResidual;
|
|
|
|
use seed::fetch::{FetchError, Request};
|
|
|
|
pub mod admin;
|
|
pub mod public;
|
|
|
|
#[derive(Debug)]
|
|
pub enum NetRes<S> {
|
|
Success(S),
|
|
Error(model::api::Failure),
|
|
Http(FetchError),
|
|
}
|
|
|
|
impl<S> From<FetchError> for NetRes<S> {
|
|
fn from(e: FetchError) -> Self {
|
|
Self::Http(e)
|
|
}
|
|
}
|
|
|
|
impl<S> FromResidual<Result<Infallible, NetRes<S>>> for NetRes<S> {
|
|
fn from_residual(residual: Result<Infallible, NetRes<S>>) -> Self {
|
|
match residual {
|
|
Ok(_s) => unreachable!(),
|
|
Err(s) => s,
|
|
}
|
|
}
|
|
}
|
|
|
|
async fn perform<S: serde::de::DeserializeOwned + 'static>(req: Request<'_>) -> NetRes<S> {
|
|
let res = match req.fetch().await {
|
|
Ok(res) => res,
|
|
Err(err) => return NetRes::Http(err),
|
|
};
|
|
if res.status().is_error() {
|
|
NetRes::Error(match res.json().await {
|
|
Ok(json) => json,
|
|
Err(_err) => match res.text().await {
|
|
Ok(text) => model::api::Failure { errors: vec![text] },
|
|
Err(_err) => model::api::Failure {
|
|
errors: vec!["There was internal server error. Please try later".into()],
|
|
},
|
|
},
|
|
})
|
|
} else {
|
|
match res.json().await {
|
|
Ok(json) => NetRes::Success(json),
|
|
Err(err) => NetRes::Http(err),
|
|
}
|
|
}
|
|
}
|