move tune selection logic
This commit is contained in:
parent
0da9a95f2a
commit
6bae986bf7
28
src/main.rs
28
src/main.rs
@ -10,10 +10,8 @@ extern crate rocket_contrib;
|
|||||||
extern crate serde_derive;
|
extern crate serde_derive;
|
||||||
|
|
||||||
mod static_files;
|
mod static_files;
|
||||||
// mod tunes;
|
mod tunes;
|
||||||
|
|
||||||
use glob::glob;
|
|
||||||
use rand::Rng;
|
|
||||||
use rocket_contrib::Template;
|
use rocket_contrib::Template;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
@ -40,33 +38,15 @@ fn index() -> Template {
|
|||||||
fn random() -> Template {
|
fn random() -> Template {
|
||||||
println!("looking for a random tune...");
|
println!("looking for a random tune...");
|
||||||
|
|
||||||
// let (file_name, file_title) = tunes::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 context = RandomContext {
|
let context = RandomContext {
|
||||||
title: String::from(format!("♪ {}", file_title)),
|
title: String::from(format!("♪ {}", file_title)),
|
||||||
music_file: String::from(file_name),
|
music_file: file_name,
|
||||||
music_title: String::from(file_title),
|
music_title: file_title,
|
||||||
interval: 3,
|
interval: 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
println!("rendering template...");
|
|
||||||
|
|
||||||
Template::render("random", &context)
|
Template::render("random", &context)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/tunes.rs
10
src/tunes.rs
@ -6,21 +6,21 @@ use glob::GlobError;
|
|||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
pub fn all() -> Vec<Result<PathBuf, GlobError>> {
|
pub fn all() -> Vec<Result<PathBuf, GlobError>> {
|
||||||
glob("static/sound/*.opus")
|
glob("static/sound/*.mp3")
|
||||||
.expect("Failed to read glob pattern")
|
.expect("Failed to read glob pattern")
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn random_tune<'a>() -> (&'a str, &'a str) {
|
pub fn random_tune() -> (String, String) {
|
||||||
let all_files = all();
|
let all_files = all();
|
||||||
|
|
||||||
let chosen_one = match *rand::thread_rng().choose(&all_files).unwrap() {
|
let chosen_one = match *rand::thread_rng().choose(&all_files).unwrap() {
|
||||||
Ok(ref path) => path,
|
Ok(ref path) => path.to_owned(),
|
||||||
Err(_) => panic!(""),
|
Err(_) => panic!(""),
|
||||||
};
|
};
|
||||||
|
|
||||||
let file_name = chosen_one.file_name().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();
|
let file_title = chosen_one.file_stem().unwrap().to_str().unwrap().to_owned();
|
||||||
|
|
||||||
return (file_name, file_title);
|
return (file_name, file_title);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user