so://phie

verbal

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]

1use crate::error::Error;

2use crate::unwrap_result_or_throw;

3use serde::Serialize;

4

5#[derive(Debug, Serialize)]

6pub struct Response<T, E> {

7 pub ok: bool,

8 pub data: Option<T>,

9 pub error: Option<E>,

10}

11

12impl 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}

22

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

24where

25 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}

35

36impl 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}