diff --git a/Cargo.lock b/Cargo.lock index 075a6fb..3e5d2e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -56,6 +56,12 @@ version = "1.0.75" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a4668cab20f66d8d020e1fbc0ebe47217433c1b6c8f2040faf858554e394ace6" +[[package]] +name = "camino" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59e92b5a388f549b863a7bea62612c09f24c8393560709a54558a9abdfb3b9c" + [[package]] name = "clap" version = "4.4.0" @@ -152,6 +158,7 @@ name = "playlist-check" version = "0.1.1" dependencies = [ "anyhow", + "camino", "clap", "m3u", ] diff --git a/Cargo.toml b/Cargo.toml index a983dcd..5efb6b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2021" [dependencies] anyhow = "1.0.75" +camino = "1.1.6" clap = { version = "4.4.0", features = ["derive"] } m3u = "1.0.0" diff --git a/src/main.rs b/src/main.rs index b60c103..3006ce0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,5 @@ use anyhow::Result; +use camino::{Utf8Path, Utf8PathBuf}; use clap::{Parser, Subcommand}; use m3u::Entry; use std::collections::HashSet; @@ -29,13 +30,13 @@ enum Mode { fn get_directories(base_directory: &str, is_relative: bool) -> Result> { let mut result = HashSet::::new(); - for entry in std::fs::read_dir(base_directory)? { + for entry in Utf8Path::new(base_directory).read_dir_utf8()? { let entry = entry?; if entry.metadata()?.is_dir() { let p = if is_relative { - entry.file_name().to_string_lossy().to_string() + entry.file_name().to_string() } else { - entry.path().display().to_string() + entry.path().to_string() }; result.insert(p); } @@ -50,8 +51,9 @@ fn main() -> Result<()> { Mode::Files => { for entry in reader.entry_exts() { if let Entry::Path(p) = entry?.entry { + let p = Utf8PathBuf::try_from(p)?; if std::fs::metadata(&p).is_err() { - println!("{}", p.display()); + println!("{p}"); } } } @@ -63,8 +65,9 @@ fn main() -> Result<()> { let mut playlist_directories = HashSet::::new(); for entry in reader.entry_exts() { if let Entry::Path(p) = entry?.entry { + let p = Utf8PathBuf::try_from(p)?; if let Some(dir) = p.parent() { - playlist_directories.insert(dir.display().to_string()); + playlist_directories.insert(dir.to_string()); } } }