radio-rs

Place a description here.
git clone git://git.alex.balgavy.eu/reponame.git
Log | Files | Refs

config.rs (1313B)


      1 use crate::*;
      2 use radio::*;
      3 use serde::Deserialize;
      4 use std::fs;
      5 
      6 #[derive(Deserialize, Clone, Copy)]
      7 pub enum PlaybackOptions {
      8     MPC,
      9     MPV,
     10 }
     11 
     12 #[derive(Deserialize, Clone, Copy, Debug)]
     13 pub enum MpcLoadOptions {
     14     ADD,
     15     LOAD,
     16 }
     17 
     18 #[derive(Clone, Copy)]
     19 pub struct RadioOptions {
     20     pub method: PlaybackOptions,
     21     pub load_options: Option<MpcLoadOptions>,
     22 }
     23 
     24 #[derive(Deserialize)]
     25 pub struct ConfigData {
     26     pub config: Config,
     27 }
     28 
     29 #[derive(Deserialize)]
     30 pub struct Config {
     31     pub radios: Vec<Radio>,
     32     pub player: PlaybackOptions,
     33 }
     34 
     35 pub fn read_config() -> ConfigData {
     36     let filename = "config.toml";
     37     let contents = match fs::read_to_string(filename) {
     38         Ok(c) => c,
     39         Err(_) => {
     40             eprintln!("Could not read file `{}`", filename);
     41             exit(1);
     42         }
     43     };
     44     let data: ConfigData = match toml::from_str(&contents) {
     45         Ok(d) => d,
     46         Err(_) => {
     47             eprintln!("Unable to load data from `{}`", filename);
     48             exit(1);
     49         }
     50     };
     51 
     52     let radios = &data.config.radios;
     53     assert!(radios
     54         .iter()
     55         .all(|r| (r.url.is_some() && r.mpc_load_option.is_some()) || (r.radios.is_some())));
     56     assert!(radios
     57         .iter()
     58         .all(|r| r.description.is_some() || r.website.is_some()));
     59 
     60     return data;
     61 }