so://phie

verbal

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

blob: f038310ef0fcafed9b58c24c8467accb897ed347 (1415 bytes)[doc][blame][raw]

1use std::fmt::Display;

2use serde::Serialize;

3

4mod config;

5

6pub mod model;

7pub mod middleware;

8

9pub use config::Config;

10

11pub struct Utils {}

12

13impl Utils {

14 pub fn null<T>(value: &Option<T>) -> String where T: Display {

15 match value {

16 Some(value) => format!("'{}'", &value),

17 None => "NULL".to_string(),

18 }

19 }

20}

21

22#[derive(Debug, Clone)]

23pub struct State {

24 pub config: Config,

25}

26

27impl State {

28 pub fn new(config: Config) -> Self {

29 Self { config }

30 }

31}

32

33#[derive(Debug, Serialize)]

34pub struct Response<T, E> {

35 pub ok: bool,

36 pub data: Option<T>,

37 pub error: Option<E>,

38}

39

40impl Response<(), ()> {

41 pub fn success() -> Result<tide::Body, tide::Error> {

42 tide::Body::from_json(&Self { ok: true, data: None, error: None })

43 }

44}

45

46impl<T> Response<T, ()> where T: Serialize {

47 pub fn with(data: T) -> Result<tide::Body, tide::Error> {

48 tide::Body::from_json(&Self { ok: true, data: Some(data), error: None })

49 }

50 pub fn with_option(data: Option<T>) -> Result<tide::Body, tide::Error> {

51 tide::Body::from_json(&Self { ok: true, data, error: None })

52 }

53}

54

55

56impl<E> Response<(), E> where E: Serialize {

57 pub fn throw(error: E) -> Result<tide::Body, tide::Error> {

58 tide::Body::from_json(&Self { ok: false, data: None, error: Some(error) })

59 }

60}