so://phie

verbal

A mobile app for listening to radio stations from around the globe
git clone git://soophie.de/verbal

blob: cd18c5e5420979b4a90a60ad4506a78e64eca587 (847 bytes)[doc][blame][raw]

1use crate::error::Error;

2use serde::Serialize;

3

4#[derive(Debug, Serialize)]

5pub struct Response<T, E> {

6 pub ok: bool,

7 pub data: Option<T>,

8 pub error: Option<E>,

9}

10

11impl Response<(), ()> {

12 pub fn respond(body: Result<tide::Body, tide::Error>) -> tide::Result {

13 Ok(tide::Response::builder(200).body(body.unwrap()).build())

14 }

15}

16

17impl<T> Response<T, ()>

18where

19 T: Serialize,

20{

21 pub fn with(data: T) -> tide::Result {

22 Response::respond(tide::Body::from_json(&Self {

23 ok: true,

24 data: Some(data),

25 error: None,

26 }))

27 }

28}

29

30impl Response<(), Error> {

31 pub fn throw(message: &str) -> tide::Result {

32 Response::respond(tide::Body::from_json(&Self {

33 ok: false,

34 data: None,

35 error: Some(Error::new(message)),

36 }))

37 }

38}