so://phie

verbal

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]

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

30

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

32where

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

43

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