Skip to content

Commit 45b369c

Browse files
committed
doc: Improve and correct normalize() documentation (GitoxideLabs#2074)
1 parent 1dd9329 commit 45b369c

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

gix-path/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ include = ["src/**/*", "LICENSE-*"]
1212
rust-version = "1.70"
1313

1414
[lib]
15-
doctest = false
15+
doctest = true
1616

1717
[dependencies]
1818
gix-trace = { version = "^0.1.12", path = "../gix-trace" }

gix-path/src/convert.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,16 +247,34 @@ pub fn to_windows_separators<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr
247247
///
248248
/// For example, this turns `a/./b/c/.././..` into `a`, and turns `/a/../b/..` into `/`.
249249
///
250-
/// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of
251-
/// the full path to `current_dir`.
250+
/// ```
251+
/// # fn main() {
252+
/// # use std::path::Path;
253+
/// # use gix_path::normalize;
254+
/// for (input, expected) in [
255+
/// ("a/./b/c/.././..", "a"),
256+
/// ("/a/../b/..", "/"),
257+
/// ("/base/a/..", "/base"),
258+
/// ("./a/..", "."),
259+
/// ("./a/../..", "/"),
260+
/// (".///", ".///"),
261+
/// ("a//b", "a//b"),
262+
/// ("/base/../base", "/base"),
263+
/// ] {
264+
/// let input = Path::new(input);
265+
/// let expected = Path::new(expected);
266+
/// assert_eq!(normalize(input.into(), Path::new("/cwd")), Some(expected.into()));
267+
/// }
268+
/// # }
269+
/// ```
252270
///
253-
/// Single `.` components as well as duplicate separators are left untouched.
271+
/// Leading `.` components as well as duplicate separators are left untouched.
254272
///
255273
/// This is particularly useful when manipulating paths that are based on user input, and not
256274
/// resolving intermediate symlinks keeps the path similar to what the user provided. If that's not
257-
/// desirable, use `[realpath()][crate::realpath()` instead.
275+
/// desirable, use [`realpath()`](crate::realpath()) instead.
258276
///
259-
/// Note that we might access the `current_dir` if we run out of path components to pop off, which
277+
/// Note that we will use the `current_dir` if we run out of path components to pop off, which
260278
/// is expected to be absolute as typical return value of `std::env::current_dir()` or
261279
/// `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known. As a `current_dir` like `/c`
262280
/// can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability to

0 commit comments

Comments
 (0)