Skip to content

Commit

Permalink
refactor: replace String and PathBuf with &str and &Path
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Dec 16, 2024
1 parent c28f177 commit 90e12ba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
20 changes: 10 additions & 10 deletions src/file_layer/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
use std::{path::PathBuf, sync::Arc, time::Duration};
use std::{
path::{Path, PathBuf},
sync::Arc,
time::Duration,
};

use notify::{Error, RecommendedWatcher, RecursiveMode};
use notify_debouncer_full::{
Expand All @@ -14,7 +18,7 @@ use tokio::{
};

pub(crate) async fn create_watcher(
root: PathBuf,
root: &Path,
) -> Result<
(
Debouncer<RecommendedWatcher, RecommendedCache>,
Expand Down Expand Up @@ -91,8 +95,8 @@ pub async fn watch(
let target_name = &e.event.paths[1];
log::debug!(
"[RENAME] {} -> {}",
strip_prefix(source_name, &root_path),
strip_prefix(target_name, &root_path)
strip_prefix(source_name, &root_path).display(),
strip_prefix(target_name, &root_path).display(),
);
files_changed = true;
}
Expand Down Expand Up @@ -127,10 +131,6 @@ pub async fn watch(
}
}

fn strip_prefix(path: &std::path::Path, prefix: &std::path::PathBuf) -> String {
path.strip_prefix(prefix)
.unwrap()
.to_str()
.unwrap()
.to_string()
fn strip_prefix<'a>(path: &'a Path, prefix: &Path) -> &'a Path {
path.strip_prefix(prefix).unwrap()
}
2 changes: 1 addition & 1 deletion src/http_layer/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::net::IpAddr;
use local_ip_address::local_ip;
use tokio::net::TcpListener;

pub(crate) async fn create_listener(addr: String) -> Result<TcpListener, String> {
pub(crate) async fn create_listener(addr: &str) -> Result<TcpListener, String> {
match tokio::net::TcpListener::bind(&addr).await {
Ok(listener) => {
let port = listener.local_addr().unwrap().port();
Expand Down
16 changes: 9 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ use http_layer::{
use local_ip_address::local_ip;
use notify::RecommendedWatcher;
use notify_debouncer_full::{DebouncedEvent, Debouncer, RecommendedCache};
use std::{error::Error, net::IpAddr, path::PathBuf, sync::Arc};
use std::{
error::Error,
net::IpAddr,
path::{Path, PathBuf},
sync::Arc,
};
use tokio::{
net::TcpListener,
sync::{broadcast, mpsc::Receiver},
Expand Down Expand Up @@ -109,12 +114,9 @@ impl Listener {
/// listen("127.0.0.1:8080", "./").await?.start(Options::default()).await
/// }
/// ```
pub async fn listen<A: Into<String>, R: Into<PathBuf>>(
addr: A,
root: R,
) -> Result<Listener, String> {
let tcp_listener = create_listener(addr.into()).await?;
let (debouncer, root_path, rx) = create_watcher(root.into()).await?;
pub async fn listen(addr: impl AsRef<str>, root: impl AsRef<Path>) -> Result<Listener, String> {
let tcp_listener = create_listener(addr.as_ref()).await?;
let (debouncer, root_path, rx) = create_watcher(root.as_ref()).await?;

Ok(Listener {
tcp_listener,
Expand Down

0 comments on commit 90e12ba

Please sign in to comment.