From b1733fdbfa83de6a1e42483cb280a52ab3a7e3e9 Mon Sep 17 00:00:00 2001 From: lielfr Date: Sat, 10 May 2025 01:15:48 +0300 Subject: [PATCH] remove lossy conversions, fix linting and doc tests, add more tests for Path replacement methods --- .../src/io/embedded/embedded_watcher.rs | 6 ++-- crates/bevy_asset/src/io/source.rs | 4 ++- crates/bevy_asset/src/path.rs | 32 +++++++++++++++++-- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs index f955465be0..c3a400bcd3 100644 --- a/crates/bevy_asset/src/io/embedded/embedded_watcher.rs +++ b/crates/bevy_asset/src/io/embedded/embedded_watcher.rs @@ -3,7 +3,7 @@ use crate::io::{ memory::Dir, AssetSourceEvent, AssetWatcher, }; -use alloc::{boxed::Box, sync::Arc, vec::Vec}; +use alloc::{boxed::Box, string::ToString, sync::Arc, vec::Vec}; use bevy_platform::collections::HashMap; use core::time::Duration; use notify_debouncer_full::{notify::RecommendedWatcher, Debouncer, RecommendedCache}; @@ -40,7 +40,9 @@ impl EmbeddedWatcher { last_event: None, }; let watcher = new_asset_event_debouncer( - root.to_string_lossy().to_string(), + root.to_str() + .expect("non unicode characters found in path") + .to_string(), debounce_wait_time, handler, ) diff --git a/crates/bevy_asset/src/io/source.rs b/crates/bevy_asset/src/io/source.rs index 6496a85a06..d16576dde4 100644 --- a/crates/bevy_asset/src/io/source.rs +++ b/crates/bevy_asset/src/io/source.rs @@ -545,7 +545,9 @@ impl AssetSource { if path.exists() { Some(Box::new( super::file::FileWatcher::new( - path.to_string_lossy().to_string(), + path.to_str() + .expect("non unicode characters found in path") + .to_string(), sender, file_debounce_wait_time, ) diff --git a/crates/bevy_asset/src/path.rs b/crates/bevy_asset/src/path.rs index 9159857147..5c84f1e9a2 100644 --- a/crates/bevy_asset/src/path.rs +++ b/crates/bevy_asset/src/path.rs @@ -638,7 +638,11 @@ impl<'a> From<&'a Path> for AssetPath<'a> { fn from(path: &'a Path) -> Self { Self { source: AssetSourceId::Default, - path: CowArc::from(path.to_string_lossy().to_string()), + path: CowArc::from( + path.to_str() + .expect("non unicode characters found in file path") + .to_string(), + ), label: None, } } @@ -649,7 +653,11 @@ impl From for AssetPath<'static> { fn from(path: PathBuf) -> Self { Self { source: AssetSourceId::Default, - path: CowArc::from(path.to_string_lossy().to_string()), + path: CowArc::from( + path.to_str() + .expect("non unicode characters found in file path") + .to_string(), + ), label: None, } } @@ -1078,4 +1086,24 @@ mod tests { assert_eq!(AssetPath::from("a/b/"), AssetPath::from("a/b")); assert_eq!(AssetPath::from("a/b/#c"), AssetPath::from("a/b#c")); } + + #[test] + fn test_path_components() { + use alloc::vec; + use alloc::vec::Vec; + + assert_eq!( + AssetPath::from("a/b/c") + .path_components() + .collect::>(), + vec!["a", "b", "c"] + ); + + assert_eq!( + AssetPath::from("a/b/c/../d") + .path_components() + .collect::>(), + vec!["a", "b", "c", "..", "d"] + ); + } }