move tune selection logic

This commit is contained in:
Sebastian Hugentobler 2017-02-14 12:47:20 +01:00
parent 0da9a95f2a
commit 6bae986bf7
2 changed files with 9 additions and 29 deletions

View File

@ -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::<Vec<_>>();
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)
}

View File

@ -6,21 +6,21 @@ use glob::GlobError;
use rand::Rng;
pub fn all() -> Vec<Result<PathBuf, GlobError>> {
glob("static/sound/*.opus")
glob("static/sound/*.mp3")
.expect("Failed to read glob pattern")
.collect::<Vec<_>>()
}
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);
}