From fa22be6e0e3e2679b7a041d05d9198f275149bd9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Fri, 27 Oct 2023 13:13:22 +0200 Subject: [PATCH] refactor: state --- Justfile | 11 +++++++++++ src/lib.rs | 1 + src/main.rs | 25 ++++++------------------- src/soundclips.rs | 18 ++++++++++-------- src/state.rs | 21 +++++++++++++++++++++ templates/index.html | 20 +++++++++++++++----- 6 files changed, 64 insertions(+), 32 deletions(-) create mode 100644 Justfile create mode 100644 src/state.rs diff --git a/Justfile b/Justfile new file mode 100644 index 0000000..4a0e2a2 --- /dev/null +++ b/Justfile @@ -0,0 +1,11 @@ +default: + @just hello + +run: + @cargo run main + +bin args='': + @cargo run --bin {{args}} + +hello: + @echo "Hello, world!" diff --git a/src/lib.rs b/src/lib.rs index 8035581..30c17ad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod soundclips; +pub mod state; pub mod vbplay; diff --git a/src/main.rs b/src/main.rs index 9a565e4..c04ae87 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,8 @@ -use axum::extract::{FromRef, State}; +use axum::extract::State; use axum::{routing, Router}; use axum_template::{engine::Engine, Key, RenderHtml}; use minijinja::{path_loader, Environment}; +use state::{AppState, TemplateEngine}; use std::net::SocketAddr; use serde::Serialize; @@ -10,23 +11,9 @@ use std::sync::{Arc, Mutex}; use vbplay::AudioThread; mod soundclips; +mod state; mod vbplay; -type AppEngine = Engine>; - -#[derive(Clone)] -pub struct AppState { - pub engine: AppEngine, - pub clips: Vec, - pub player: Arc>, -} - -impl FromRef for AppEngine { - fn from_ref(state: &AppState) -> Self { - state.engine.clone() - } -} - #[tokio::main] async fn main() { let mut jinja = Environment::new(); @@ -36,7 +23,7 @@ async fn main() { let audio = vbplay::audio_thread(); let audio: Arc> = Arc::new(Mutex::new(audio)); - let state = AppState { + let app_state = AppState { engine: template_engine, clips: soundclips::scan_directory_for_clips("E:/sounds/soundboard", &["mp3"]) .expect("No Soundclips found."), @@ -51,7 +38,7 @@ async fn main() { .route("/", routing::get(home)) .route("/play/:hash", routing::get(play_handler)) .route("/stop", routing::get(stop_handler)) - .with_state(state); + .with_state(app_state); println!("listening on {}", addr); @@ -67,7 +54,7 @@ struct TemplateContext { clips: Vec, } -async fn home(engine: AppEngine, state: State) -> impl axum::response::IntoResponse { +async fn home(engine: TemplateEngine, state: State) -> impl axum::response::IntoResponse { let clips = state.0.clone().clips; // this is not ideal. let context = TemplateContext { clips: clips }; RenderHtml(Key("index.html".to_owned()), engine, context) diff --git a/src/soundclips.rs b/src/soundclips.rs index 5555641..f2234c8 100644 --- a/src/soundclips.rs +++ b/src/soundclips.rs @@ -58,15 +58,17 @@ pub fn scan_directory_for_clips(directory: &str, extensions: &[&str]) -> Option< if path.is_file() { if let Some(ext) = path.extension() { if extensions.iter().any(|&e| ext == OsStr::new(e)) { - let file_name = path.file_name().unwrap().to_str().unwrap().to_string(); - /* - the calls to unwrap() are more or less "safe" because: + if let Some(file_name) = path.file_name().unwrap().to_str() { + /* + the calls to unwrap() are more or less "safe" because: - file_name() only returns None if the path ends in "..", which won't be the case for paths returned by read_dir. - to_str() only returns None if the file name isn't valid Unicode, which is quite rare on most modern file systems. - */ - let sound_clip = SoundClip::new(file_name, directory.to_owned()); - sound_clips.push(sound_clip); + file_name() only returns None if the path ends in "..", which won't be the case for paths returned by read_dir. + to_str() only returns None if the file name isn't valid Unicode, which is quite rare on most modern file systems. + */ + let sound_clip = + SoundClip::new(file_name.to_string(), directory.to_owned()); + sound_clips.push(sound_clip); + } } } } diff --git a/src/state.rs b/src/state.rs new file mode 100644 index 0000000..6e18373 --- /dev/null +++ b/src/state.rs @@ -0,0 +1,21 @@ +use crate::soundclips::SoundClip; +use crate::vbplay::AudioThread; +use axum::extract::FromRef; +use axum_template::engine::Engine; +use minijinja::Environment; +use std::sync::{Arc, Mutex}; + +pub type TemplateEngine = Engine>; + +#[derive(Clone)] +pub struct AppState { + pub engine: TemplateEngine, + pub clips: Vec, + pub player: Arc>, +} + +impl FromRef for TemplateEngine { + fn from_ref(state: &AppState) -> Self { + state.engine.clone() + } +} diff --git a/templates/index.html b/templates/index.html index 700d383..1221979 100644 --- a/templates/index.html +++ b/templates/index.html @@ -13,10 +13,20 @@ - {% for clip in clips %} -
- {% endfor %} - - +
+
+ Category 1 + + +
+
+ {% for clip in clips %} + + {% endfor %} +
+
+ +
+