A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal
blob: 1663b831bc988d6f5d0c74f6f650bcef7d53c40b (1532 bytes)[doc][blame][raw]
1
use crate::error::{Error, ErrorKind};2
use serde::Serialize;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 {14
ok: true,15
data: None,16
error: None,17
})18
}19
pub fn respond(body: Result<tide::Body, tide::Error>) -> tide::Result {20
Ok(tide::Response::builder(200).body(body.unwrap()).build())21
}22
}24
impl<T> Response<T, ()>25
where26
T: Serialize,27
{28
pub fn with(data: T) -> tide::Result {29
Response::respond(tide::Body::from_json(&Self {30
ok: true,31
data: Some(data),32
error: None,33
}))34
}35
pub fn with_option(data: Option<T>) -> Result<tide::Body, tide::Error> {36
tide::Body::from_json(&Self {37
ok: true,38
data,39
error: None,40
})41
}42
}44
impl<E> Response<(), E>45
where46
E: Serialize,47
{48
pub fn with_error(error: E) -> Result<tide::Body, tide::Error> {49
tide::Body::from_json(&Self {50
ok: false,51
data: None,52
error: Some(error),53
})54
}55
}57
impl Response<(), Error> {58
pub fn throw(error_kind: ErrorKind, message: &str) -> tide::Result {59
Response::respond(tide::Body::from_json(&Self {60
ok: false,61
data: None,62
error: Some(Error::new(error_kind, message)),63
}))64
}65
}