so://phie

verbal

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

blob: aead9b4b16a9ea1616a36670169ba90a73bc1c62 (1789 bytes)[doc][blame][raw]

1use std::fmt::Display;

2use serde::{ Serialize};

3

4mod config;

5

6pub mod model;

7pub mod adapter;

8pub mod middleware;

9

10pub use config::Config;

11

12pub struct Utils {}

13

14impl Utils {

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

16 match value {

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

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

19 }

20 }

21}

22

23#[derive(Debug, Clone)]

24pub struct State {

25 pub config: Config,

26}

27

28impl State {

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

30 Self { config }

31 }

32}

33

34#[derive(Debug, Serialize)]

35pub enum ErrorKind {

36 Identity,

37 Arguments,

38 Query,

39 Fetch,

40 Parse,

41}

42

43#[derive(Debug, Serialize)]

44pub struct Error {

45 kind: ErrorKind,

46 message: String,

47}

48

49impl Error {

50 pub fn new(kind: ErrorKind, message: &str) -> Self {

51 Self { kind, message: message.into() }

52 }

53}

54

55#[derive(Debug, Serialize)]

56pub struct Response<T, E> {

57 pub ok: bool,

58 pub data: Option<T>,

59 pub error: Option<E>,

60}

61

62impl Response<(), ()> {

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

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

65 }

66}

67

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

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

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

71 }

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

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

74 }

75}

76

77

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

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

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

81 }

82}