#path #relative-path #path-slash

sugar_path

Sugar functions for manipulating paths

14 releases (3 stable)

1.2.0 Apr 9, 2024
0.0.12 Feb 28, 2023
0.0.9 Nov 15, 2022
0.0.5 May 29, 2022
0.0.3 Mar 19, 2022

#137 in Algorithms

Download history 576/week @ 2024-01-28 282/week @ 2024-02-04 147/week @ 2024-02-11 436/week @ 2024-02-18 481/week @ 2024-02-25 771/week @ 2024-03-03 1075/week @ 2024-03-10 1000/week @ 2024-03-17 1063/week @ 2024-03-24 1046/week @ 2024-03-31 2885/week @ 2024-04-07 910/week @ 2024-04-14 701/week @ 2024-04-21 589/week @ 2024-04-28 710/week @ 2024-05-05 478/week @ 2024-05-12

2,560 downloads per month
Used in 11 crates (9 directly)

MIT license

22KB
272 lines

sugar_path

document crate version MIT

Sugar functions for manipulating paths.

Main functionalities

  • SugarPath::as_path makes it easy to convert 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");
use std::path::Path;
use sugar_path::SugarPath;
assert_eq!("foo/./bar/../baz".normalize(), "foo/baz".as_path());
  • SugarPath::relative allows you to get the relative path from the given path to the target path.
use sugar_path::SugarPath;
assert_eq!("/base".relative("/base/project"), "..".as_path());
assert_eq!("/base".relative("/var/lib"), "../../base".as_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());
}
  • For more details, please refer to the SugarPath.

No runtime deps