radio.rs (2804B)
1 use crate::config::*; 2 use serde::Deserialize; 3 use std::process::Command; 4 5 /* TODO: 6 * Radio struct has /either/ (url && mpc load option) or (radios) 7 * Make an enum containing values for that. 8 * 9 * Radios can be a Vec<Radio>, or a function returning Vec<Radio>. 10 * Should use an enum with values for that: 11 * 12 * enum Things { Num(i64), Func(fn()) } 13 * fn doit() { println!("We in function"); } 14 * 15 * fn main() { 16 * let i = Things::Num(42); 17 * if let Things::Num(v) = i { 18 * dbg!(v); 19 * } 20 * 21 * let f = Things::Func(doit); 22 * if let Things::Func(thefunc) = f { 23 * thefunc(); 24 * } 25 * } 26 */ 27 #[derive(Deserialize, Debug)] 28 pub struct Radio { 29 pub name: String, 30 31 // TODO: description & website shouldn't be exclusive 32 pub description: Option<String>, 33 pub website: Option<String>, 34 35 #[serde(alias = "sound")] 36 pub url: Option<String>, 37 pub mpc_load_option: Option<MpcLoadOptions>, 38 pub radios: Option<Vec<Radio>>, 39 } 40 41 impl Radio { 42 pub fn play(&self, options: &RadioOptions) { 43 let stream = self.url.as_ref().unwrap(); 44 println!("Playing {}", stream); 45 match options { 46 RadioOptions { 47 method: PlaybackOptions::MPC, 48 load_options: Some(l), 49 } => { 50 Command::new("mpc") 51 .arg("clear") 52 .stdout(std::process::Stdio::null()) 53 .spawn() 54 .expect("MPC couldn't clear"); 55 match l { 56 MpcLoadOptions::ADD => { 57 Command::new("mpc") 58 .args(&["add", stream]) 59 .spawn() 60 .expect("MPC couldn't add stream"); 61 } 62 MpcLoadOptions::LOAD => { 63 Command::new("mpc") 64 .args(&["load", stream]) 65 .spawn() 66 .expect("MPC couldn't load stream"); 67 } 68 } 69 Command::new("mpc") 70 .arg("play") 71 .stdout(std::process::Stdio::null()) 72 .spawn() 73 .expect("MPC couldn't play"); 74 } 75 RadioOptions { 76 method: PlaybackOptions::MPV, 77 load_options: None, 78 } => { 79 let mut child = Command::new("mpv") 80 .args(&[stream, "--vid=no", "--volume=50"]) 81 .spawn() 82 .expect("MPV couldn't play"); 83 let _ = child.wait(); 84 } 85 _ => panic!("Wrong combination of playback options."), 86 } 87 } 88 }