From 125a9f0fe92a0239f1a39e2c1ed5c7d558d73fca Mon Sep 17 00:00:00 2001 From: quirinecker Date: Sun, 12 Mar 2023 21:44:42 +0100 Subject: [PATCH] made some refactoring --- src/app/commands/serve.rs | 30 +++++++----------------------- src/app/mod.rs | 1 + src/app/watcher/mod.rs | 17 +++++++++++++++++ 3 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 src/app/watcher/mod.rs diff --git a/src/app/commands/serve.rs b/src/app/commands/serve.rs index 51b2a0c..59e00a6 100644 --- a/src/app/commands/serve.rs +++ b/src/app/commands/serve.rs @@ -1,16 +1,16 @@ use colored::Colorize; +use futures::StreamExt; use live_server::listen; use notify::{ event::ModifyKind, - Config, Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher, + Event, EventKind, RecursiveMode, Watcher, }; use std::{env, path::Path}; -use tokio::sync::mpsc::{channel, Receiver}; -use crate::app::builder::{ +use crate::app::{builder::{ asciidoctor::{AsciiDoctorDocsBuilder, AsciiDoctorSlideBuilder}, Builder, -}; +}, watcher::watcher}; pub async fn serve() { @@ -38,11 +38,9 @@ async fn watch(path: &Path) -> notify::Result<()> { watcher.watch(path.as_ref(), RecursiveMode::Recursive)?; - while let Some(res) = rx.recv().await { - match res { - Ok(event) => file_change(event), - Err(e) => println!("watch error: {:?}", e), - } + while let Some(res) = rx.next().await { + let event = res.expect("watching failed"); + file_change(event) } Ok(()) @@ -92,17 +90,3 @@ fn current_dir() -> String { ); } -fn watcher() -> notify::Result<(RecommendedWatcher, Receiver>)> { - let (tx, rx) = channel(1); - - let watcher = RecommendedWatcher::new( - move |res| { - futures::executor::block_on(async { - tx.send(res).await.unwrap(); - }); - }, - Config::default(), - )?; - - Ok((watcher, rx)) -} diff --git a/src/app/mod.rs b/src/app/mod.rs index b12e39b..2f01bea 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -1,6 +1,7 @@ mod commands; pub mod builder; pub mod fs_util; +pub mod watcher; mod args; use std::env; diff --git a/src/app/watcher/mod.rs b/src/app/watcher/mod.rs new file mode 100644 index 0000000..b87393c --- /dev/null +++ b/src/app/watcher/mod.rs @@ -0,0 +1,17 @@ +use futures::{channel::mpsc::{Receiver, channel}, SinkExt}; +use notify::{RecommendedWatcher, Event, Watcher, Config}; + +pub fn watcher() -> notify::Result<(RecommendedWatcher, Receiver>)> { + let (mut tx, rx) = channel(1); + + let watcher = RecommendedWatcher::new( + move |res| { + futures::executor::block_on(async { + tx.send(res).await.unwrap(); + }); + }, + Config::default(), + )?; + + Ok((watcher, rx)) +}