A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 584080a1bd2ddbe4e089ec7dbdae36edd579b9e3 (1003 bytes)[doc][blame][raw]
1
use crate::error::Error;2
use crate::unwrap_result_or_throw;3
use serde::Serialize;5
#[derive(Debug, Serialize)]6
pub struct Response<T, E> {7
pub ok: bool,8
pub data: Option<T>,9
pub error: Option<E>,10
}12
impl Response<(), ()> {13
pub fn respond(body: Result<tide::Body, tide::Error>) -> tide::Result {14
Ok(tide::Response::builder(200)15
.body(unwrap_result_or_throw!(16
body,17
"cannot acquire response body!"18
))19
.build())20
}21
}23
impl<T> Response<T, ()>24
where25
T: Serialize,26
{27
pub fn with(data: T) -> tide::Result {28
Response::respond(tide::Body::from_json(&Self {29
ok: true,30
data: Some(data),31
error: None,32
}))33
}34
}36
impl Response<(), Error> {37
pub fn throw(message: &str) -> tide::Result {38
Response::respond(tide::Body::from_json(&Self {39
ok: false,40
data: None,41
error: Some(Error::new(message)),42
}))43
}44
}