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]
1
use std::fmt::Display;2
use serde::{ Serialize};4
mod config;6
pub mod model;7
pub mod adapter;8
pub mod middleware;10
pub use config::Config;12
pub struct Utils {}14
impl 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
}23
#[derive(Debug, Clone)]24
pub struct State {25
pub config: Config,26
}28
impl State {29
pub fn new(config: Config) -> Self {30
Self { config }31
}32
}34
#[derive(Debug, Serialize)]35
pub enum ErrorKind {36
Identity,37
Arguments,38
Query,39
Fetch,40
Parse,41
}43
#[derive(Debug, Serialize)]44
pub struct Error {45
kind: ErrorKind,46
message: String,47
}49
impl Error {50
pub fn new(kind: ErrorKind, message: &str) -> Self {51
Self { kind, message: message.into() }52
}53
}55
#[derive(Debug, Serialize)]56
pub struct Response<T, E> {57
pub ok: bool,58
pub data: Option<T>,59
pub error: Option<E>,60
}62
impl Response<(), ()> {63
pub fn success() -> Result<tide::Body, tide::Error> {64
tide::Body::from_json(&Self { ok: true, data: None, error: None })65
}66
}68
impl<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
}78
impl<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
}