| Crates.io | sugar_path |
| lib.rs | sugar_path |
| version | 1.2.0 |
| created_at | 2022-03-16 18:01:06.952773+00 |
| updated_at | 2024-04-09 02:44:47.357153+00 |
| description | Sugar functions for manipulating paths |
| homepage | |
| repository | https://github.com/hyf0/sugar_path |
| max_upload_size | |
| id | 551331 |
| size | 38,468 |
Sugar functions for manipulating paths.
T: Deref<Target = str> to Path and allows you to use methods of SugarPath on &str or String directly.use std::path::Path;
use sugar_path::SugarPath;
assert_eq!("foo".as_path().join("bar"), Path::new("foo/bar"));
assert_eq!("foo/./bar/../baz".normalize(), "foo/baz".as_path());
use sugar_path::SugarPath;
#[cfg(target_family = "unix")]
let p = "./hello/world".as_path();
#[cfg(target_family = "windows")]
let p = ".\\hello\\world".as_path();
assert_eq!(p.to_slash().unwrap(), "./hello/world");
assert_eq!(p.to_slash_lossy(), "./hello/world");
. or .. segments.use std::path::Path;
use sugar_path::SugarPath;
assert_eq!("foo/./bar/../baz".normalize(), "foo/baz".as_path());
use sugar_path::SugarPath;
assert_eq!("/base".relative("/base/project"), "..".as_path());
assert_eq!("/base".relative("/var/lib"), "../../base".as_path());
std::env::current_dir().unwrap() as the base path.use sugar_path::SugarPath;
let cwd = std::env::current_dir().unwrap();
assert_eq!("hello/world".absolutize(), cwd.join("hello").join("world"));
use sugar_path::SugarPath;
#[cfg(target_family = "unix")]
{
assert_eq!("./world".absolutize_with("/hello"), "/hello/world".as_path());
assert_eq!("../world".absolutize_with("/hello"), "/world".as_path());
}
#[cfg(target_family = "windows")]
{
assert_eq!(".\\world".absolutize_with("C:\\hello"), "C:\\hello\\world".as_path());
assert_eq!("..\\world".absolutize_with("C:\\hello"), "C:\\world".as_path());
}