A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: f4c4dca738990594a0d20c80bfb02719ca23b65a (1396 bytes)[doc][blame][raw]
1
use serde::Serialize;2
use crate::error::{Error, ErrorKind};4
#[derive(Debug, Serialize)]5
pub struct Response<T, E> {6
pub ok: bool,7
pub data: Option<T>,8
pub error: Option<E>,9
}11
impl Response<(), ()> {12
pub fn success() -> Result<tide::Body, tide::Error> {13
tide::Body::from_json(&Self { ok: true, data: None, error: None })14
}15
pub fn respond(body: Result<tide::Body, tide::Error>) -> tide::Result {16
Ok(17
tide::Response::builder(200)18
.body(body.unwrap())19
.build()20
)21
}22
}24
impl<T> Response<T, ()> where T: Serialize {25
pub fn with(data: T) -> tide::Result {26
Response::respond(tide::Body::from_json(&Self { ok: true, data: Some(data), error: None }))27
}28
pub fn with_option(data: Option<T>) -> Result<tide::Body, tide::Error> {29
tide::Body::from_json(&Self { ok: true, data, error: None })30
}31
}33
impl<E> Response<(), E> where E: Serialize {34
pub fn with_error(error: E) -> Result<tide::Body, tide::Error> {35
tide::Body::from_json(&Self { ok: false, data: None, error: Some(error) })36
}37
}39
impl Response<(), Error> {40
pub fn throw(error_kind: ErrorKind, message: &str) -> tide::Result {41
Response::respond(tide::Body::from_json(&Self { ok: false, data: None, error: Some(Error::new(error_kind, message)) }))42
}43
}