A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 4c10f0d9c9dd5fdef2e8529886675b0f2f3d90fa (1183 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
}22
pub fn ok() -> tide::Result {23
Response::respond(tide::Body::from_json(&Self {24
ok: true,25
data: None,26
error: None,27
}))28
}29
}31
impl<T> Response<T, ()>32
where33
T: Serialize,34
{35
pub fn with(data: T) -> tide::Result {36
Response::respond(tide::Body::from_json(&Self {37
ok: true,38
data: Some(data),39
error: None,40
}))41
}42
}44
impl Response<(), Error> {45
pub fn throw(message: &str) -> tide::Result {46
Response::respond(tide::Body::from_json(&Self {47
ok: false,48
data: None,49
error: Some(Error::new(message)),50
}))51
}52
}