so://phie

verbal

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

blob: dd59d2bde45236243e40e46d7bd1ebd0c60f5839 (2528 bytes)[doc][blame][raw]

1use env_logger::Builder;

2use env_logger::Target;

3use log::LevelFilter;

4

5pub mod error;

6pub mod middleware;

7pub mod model;

8pub mod route;

9

10mod config;

11mod response;

12mod server;

13

14pub use config::Config;

15pub use response::Response;

16pub use server::Server;

17

18use error::Error;

19

20#[macro_use]

21extern crate log;

22

23#[macro_export]

24macro_rules! abort {

25 ($message:expr) => {{

26 error!("{}", $message);

27 std::process::exit(1)

28 }};

29 ($message:expr, $err:expr) => {{

30 error!("{} Reason: {}", $message, $err);

31 std::process::exit(1)

32 }};

33}

34

35#[macro_export]

36macro_rules! unwrap_result_or_abort {

37 ($target:expr, $message:expr) => {{

38 $target.unwrap_or_else(|err| abort!($message, &err))

39 }};

40}

41

42#[macro_export]

43macro_rules! unwrap_option_or_abort {

44 ($target:expr, $message:expr) => {{

45 $target.unwrap_or_else(|| abort!($message))

46 }};

47}

48

49#[macro_export]

50macro_rules! unwrap_result_or_throw {

51 ($target:expr, $message:expr) => {{

52 match $target {

53 Ok(target) => target,

54 Err(_) => {

55 error!("{}", $message);

56 return $crate::Response::throw($message);

57 }

58 }

59 }};

60}

61

62#[macro_export]

63macro_rules! unwrap_option_or_throw {

64 ($target:expr, $message:expr) => {{

65 match $target {

66 Some(target) => target,

67 None => {

68 error!("{}", $message);

69 return $crate::Response::throw($message);

70 }

71 }

72 }};

73}

74

75pub const APP_NAME: &str = "verbal";

76

77pub type Result<T> = std::result::Result<T, Error>;

78

79pub trait ToSql {

80 fn to_sql(&self) -> Result<String>;

81}

82

83pub trait ToUrl {

84 fn to_url(&self) -> Result<String>;

85}

86

87pub enum Category {

88 Playtime,

89 Latest,

90}

91

92impl Category {

93 pub fn from(kind: &str) -> Result<Self> {

94 match kind {

95 "playtime" => Ok(Self::Playtime),

96 "latest" => Ok(Self::Latest),

97 _ => Err(Error::new("not a valid category!")),

98 }

99 }

100}

101

102impl ToSql for Category {

103 fn to_sql(&self) -> Result<String> {

104 Ok(match self {

105 Self::Playtime => "AND station_stats.playtime > 0 ORDER BY station_stats.playtime DESC",

106 Self::Latest => "AND station_stats.playtime > 0 ORDER BY station_stats.latest_at DESC",

107 }

108 .to_string())

109 }

110}

111

112pub fn init_logger() {

113 Builder::new()

114 .format_timestamp_secs()

115 .filter_module(APP_NAME, LevelFilter::Info)

116 .target(Target::Stdout)

117 .init();

118}