Download crates/forge_services/src/utils/path.rs from SaylorTwift/forgecode: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/SaylorTwift/forgecode/resolve/main/crates/forge_services/src/utils/path.rs
- Command line
-
hf download hf://SaylorTwift/forgecode/crates/forge_services/src/utils/path.rs
-
curl -L -o path.rs https://huggingface.co/SaylorTwift/forgecode/resolve/main/crates/forge_services/src/utils/path.rs
1.4 kB
| use std::path::Path; | |
| use anyhow::bail; | |
| /// Ensures that the given path is absolute | |
| /// | |
| /// # Arguments | |
| /// * `path` - The path to validate | |
| /// | |
| /// # Returns | |
| /// * `Ok(())` if the path is absolute | |
| /// * `Err(String)` with an error message if the path is relative | |
| pub fn assert_absolute_path(path: &Path) -> anyhow::Result<()> { | |
| if !path.is_absolute() { | |
| bail!("Path must be absolute. Please provide an absolute path starting with '/' (Unix) or 'C:\\' (Windows)".to_string()) | |
| } else { | |
| Ok(()) | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| fn test_unix_absolute_path() { | |
| let path = Path::new("/absolute/path"); | |
| assert!(assert_absolute_path(path).is_ok()); | |
| } | |
| fn test_windows_absolute_path() { | |
| let path = Path::new("C:\\Windows\\Path"); | |
| assert!(assert_absolute_path(path).is_ok()); | |
| } | |
| fn test_basic_relative_path() { | |
| let path = Path::new("relative/path"); | |
| assert!(assert_absolute_path(path).is_err()); | |
| } | |
| fn test_current_dir_relative_path() { | |
| let path = Path::new("./current/path"); | |
| assert!(assert_absolute_path(path).is_err()); | |
| } | |
| fn test_parent_dir_relative_path() { | |
| let path = Path::new("../parent/path"); | |
| assert!(assert_absolute_path(path).is_err()); | |
| } | |
| } | |