chore: use camino for path handling

This commit is contained in:
Felipe 2023-09-19 15:42:46 -03:00
parent c7c396ac32
commit 933d1a6086
Signed by: pitbuster
SSH key fingerprint: SHA256:HDYu2Pm4/TmSX8GBwV49UvFWr1Ljg8XlHxKeCpjJpOk
3 changed files with 16 additions and 5 deletions

7
Cargo.lock generated
View file

@ -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",
]

View file

@ -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"

View file

@ -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<HashSet<String>> {
let mut result = HashSet::<String>::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::<String>::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());
}
}
}