From 6bae986bf75959727bfad7a42842bb1ea3260be5 Mon Sep 17 00:00:00 2001 From: Sebastian Hugentobler Date: Tue, 14 Feb 2017 12:47:20 +0100 Subject: [PATCH] move tune selection logic --- src/main.rs | 28 ++++------------------------ src/tunes.rs | 10 +++++----- 2 files changed, 9 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 02c658f..ba3ff68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,10 +10,8 @@ extern crate rocket_contrib; extern crate serde_derive; mod static_files; -// mod tunes; +mod tunes; -use glob::glob; -use rand::Rng; use rocket_contrib::Template; #[derive(Serialize)] @@ -40,33 +38,15 @@ fn index() -> Template { fn random() -> Template { println!("looking for a random tune..."); - // let (file_name, file_title) = tunes::random_tune(); - - let files = glob("static/sound/*.mp3") - .expect("Failed to read glob pattern") - .collect::>(); - - let chosen_one = match *rand::thread_rng().choose(&files).unwrap() { - Ok(ref path) => path, - Err(_) => panic!(""), - }; - - println!("getting tune information..."); - - let file_name = chosen_one.file_name().unwrap().to_str().unwrap(); - let file_title = chosen_one.file_stem().unwrap().to_str().unwrap(); - - println!("building context..."); + let (file_name, file_title) = tunes::random_tune(); let context = RandomContext { title: String::from(format!("♪ {}", file_title)), - music_file: String::from(file_name), - music_title: String::from(file_title), + music_file: file_name, + music_title: file_title, interval: 3, }; - println!("rendering template..."); - Template::render("random", &context) } diff --git a/src/tunes.rs b/src/tunes.rs index ca54fe0..443d134 100644 --- a/src/tunes.rs +++ b/src/tunes.rs @@ -6,21 +6,21 @@ use glob::GlobError; use rand::Rng; pub fn all() -> Vec> { - glob("static/sound/*.opus") + glob("static/sound/*.mp3") .expect("Failed to read glob pattern") .collect::>() } -pub fn random_tune<'a>() -> (&'a str, &'a str) { +pub fn random_tune() -> (String, String) { let all_files = all(); let chosen_one = match *rand::thread_rng().choose(&all_files).unwrap() { - Ok(ref path) => path, + Ok(ref path) => path.to_owned(), Err(_) => panic!(""), }; - let file_name = chosen_one.file_name().unwrap().to_str().unwrap(); - let file_title = chosen_one.file_stem().unwrap().to_str().unwrap(); + let file_name = chosen_one.file_name().unwrap().to_str().unwrap().to_owned(); + let file_title = chosen_one.file_stem().unwrap().to_str().unwrap().to_owned(); return (file_name, file_title); }