Skip to content

Commit bb64ee1

Browse files
authored
Merge pull request #1843 from EliahKagan/convert-doc
Fix a doc in `gix_path::convert`, revise others, and refactor slightly
2 parents b4fe425 + 42875c9 commit bb64ee1

File tree

1 file changed

+37
-29
lines changed

1 file changed

+37
-29
lines changed

Diff for: gix-path/src/convert.rs

+37-29
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ pub fn try_os_str_into_bstr(path: Cow<'_, OsStr>) -> Result<Cow<'_, BStr>, Utf8E
4545
}
4646
}
4747

48-
/// Convert the given path either into its raw bytes on unix or its UTF8 encoded counterpart on windows.
48+
/// Convert the given path either into its raw bytes on Unix or its UTF-8 encoded counterpart on Windows.
4949
///
50-
/// On windows, if the source Path contains ill-formed, lone surrogates, the UTF-8 conversion will fail
50+
/// On Windows, if the source `Path`` contains ill-formed, lone surrogates, the UTF-8 conversion will fail
5151
/// causing `Utf8Error` to be returned.
5252
pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>, Utf8Error> {
5353
let path = path.into();
@@ -86,7 +86,7 @@ pub fn try_into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Result<Cow<'a, BStr>
8686
Ok(path_str)
8787
}
8888

89-
/// Similar to [`try_into_bstr()`] but **panics** if malformed surrogates are encountered on windows.
89+
/// Similar to [`try_into_bstr()`] but **panics** if malformed surrogates are encountered on Windows.
9090
pub fn into_bstr<'a>(path: impl Into<Cow<'a, Path>>) -> Cow<'a, BStr> {
9191
try_into_bstr(path).expect("prefix path doesn't contain ill-formed UTF-8")
9292
}
@@ -101,11 +101,11 @@ pub fn join_bstr_unix_pathsep<'a, 'b>(base: impl Into<Cow<'a, BStr>>, path: impl
101101
base
102102
}
103103

104-
/// Given `input` bytes, produce a `Path` from them ignoring encoding entirely if on unix.
104+
/// Given `input` bytes, produce a `Path` from them ignoring encoding entirely if on Unix.
105105
///
106-
/// On windows, the input is required to be valid UTF-8, which is guaranteed if we wrote it before. There are some potential
107-
/// git versions and windows installation which produce mal-formed UTF-16 if certain emojies are in the path. It's as rare as
108-
/// it sounds, but possible.
106+
/// On Windows, the input is required to be valid UTF-8, which is guaranteed if we wrote it before.
107+
/// There are some potential Git versions and Windows installations which produce malformed UTF-16
108+
/// if certain emojis are in the path. It's as rare as it sounds, but possible.
109109
pub fn try_from_byte_slice(input: &[u8]) -> Result<&Path, Utf8Error> {
110110
#[cfg(unix)]
111111
let p = {
@@ -131,7 +131,7 @@ pub fn try_from_bstr<'a>(input: impl Into<Cow<'a, BStr>>) -> Result<Cow<'a, Path
131131
}
132132
}
133133

134-
/// Similar to [`try_from_bstr()`], but **panics** if malformed surrogates are encountered on windows.
134+
/// Similar to [`try_from_bstr()`], but **panics** if malformed surrogates are encountered on Windows.
135135
pub fn from_bstr<'a>(input: impl Into<Cow<'a, BStr>>) -> Cow<'a, Path> {
136136
try_from_bstr(input).expect("prefix path doesn't contain ill-formed UTF-8")
137137
}
@@ -205,7 +205,8 @@ pub fn to_native_separators<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr>
205205
p
206206
}
207207

208-
/// Convert paths with slashes to backslashes on windows and do nothing on unix, but **panics** if malformed surrogates are encountered on windows.
208+
/// Convert paths with slashes to backslashes on Windows and do nothing on Unix,
209+
/// but **panic** if unpaired surrogates are encountered on Windows.
209210
pub fn to_native_path_on_windows<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, std::path::Path> {
210211
#[cfg(not(windows))]
211212
{
@@ -217,47 +218,52 @@ pub fn to_native_path_on_windows<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a,
217218
}
218219
}
219220

220-
/// Replaces windows path separators with slashes, but only do so on windows.
221+
/// Replace Windows path separators with slashes, but only do so on Windows.
221222
pub fn to_unix_separators_on_windows<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr> {
222223
#[cfg(windows)]
223224
{
224-
replace(path, b'\\', b'/')
225+
to_unix_separators(path)
225226
}
226227
#[cfg(not(windows))]
227228
{
228229
path.into()
229230
}
230231
}
231232

232-
/// Replaces windows path separators with slashes, unconditionally.
233+
/// Replace Windows path separators with slashes, which typically resembles a Unix path, unconditionally.
233234
///
234235
/// **Note** Do not use these and prefer the conditional versions of this method.
235236
// TODO: use https://lib.rs/crates/path-slash to handle escapes
236237
pub fn to_unix_separators<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr> {
237238
replace(path, b'\\', b'/')
238239
}
239240

240-
/// Find backslashes and replace them with slashes, which typically resembles a unix path, unconditionally.
241+
/// Find slashes and replace them with backslashes, unconditionally.
241242
///
242243
/// **Note** Do not use these and prefer the conditional versions of this method.
243244
// TODO: use https://lib.rs/crates/path-slash to handle escapes
244245
pub fn to_windows_separators<'a>(path: impl Into<Cow<'a, BStr>>) -> Cow<'a, BStr> {
245246
replace(path, b'/', b'\\')
246247
}
247248

248-
/// Resolve relative components virtually without accessing the file system, e.g. turn `a/./b/c/.././..` into `a`,
249-
/// without keeping intermediate `..` and `/a/../b/..` becomes `/`.
250-
/// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of the full path to `current_dir`.
251-
/// Note that single `.` components as well as duplicate separators are left untouched.
249+
/// Resolve relative components virtually, eliminating intermediate `..` without accessing the filesystem.
252250
///
253-
/// This is particularly useful when manipulating paths that are based on user input, and not resolving intermediate
254-
/// symlinks keeps the path similar to what the user provided. If that's not desirable, use `[realpath()][crate::realpath()`
255-
/// instead.
251+
/// For example, this turns `a/./b/c/.././..` into `a`, and turns `/a/../b/..` into `/`.
256252
///
257-
/// Note that we might access the `current_dir` if we run out of path components to pop off, which is expected to be absolute
258-
/// as typical return value of `std::env::current_dir()` or `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known.
259-
/// As a `current_dir` like `/c` can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability
260-
/// to produce a logically consistent path.
253+
/// If the input path was relative and ends up being the `current_dir`, `.` is returned instead of
254+
/// the full path to `current_dir`.
255+
///
256+
/// Single `.` components as well as duplicate separators are left untouched.
257+
///
258+
/// This is particularly useful when manipulating paths that are based on user input, and not
259+
/// resolving intermediate symlinks keeps the path similar to what the user provided. If that's not
260+
/// desirable, use `[realpath()][crate::realpath()` instead.
261+
///
262+
/// Note that we might access the `current_dir` if we run out of path components to pop off, which
263+
/// is expected to be absolute as typical return value of `std::env::current_dir()` or
264+
/// `gix_fs::current_dir(…)` when `core.precomposeUnicode` is known. As a `current_dir` like `/c`
265+
/// can be exhausted by paths like `../../r`, `None` will be returned to indicate the inability to
266+
/// produce a logically consistent path.
261267
pub fn normalize<'a>(path: Cow<'a, Path>, current_dir: &Path) -> Option<Cow<'a, Path>> {
262268
use std::path::Component::ParentDir;
263269

@@ -290,14 +296,16 @@ pub fn normalize<'a>(path: Cow<'a, Path>, current_dir: &Path) -> Option<Cow<'a,
290296
.into()
291297
}
292298

293-
/// Rebuild the worktree-relative `relative_path` to be relative to `prefix`, which is the worktree-relative
294-
/// path equivalent to the position of the user, or current working directory.
299+
/// Rebuild the worktree-relative `relative_path` to be relative to `prefix`, which is the
300+
/// worktree-relative path equivalent to the position of the user, or current working directory.
301+
///
295302
/// This is a no-op if `prefix` is empty.
296303
///
297-
/// Note that both `relative_path` and `prefix` are assumed to be [normalized](normalize()), and failure to do so
298-
/// will lead to incorrect results.
304+
/// Note that both `relative_path` and `prefix` are assumed to be [normalized](normalize()), and
305+
/// failure to do so will lead to incorrect results.
299306
///
300-
/// Note that both input paths are expected to be equal in terms of case too, as comparisons will be case-sensitive.
307+
/// Note that both input paths are expected to be equal in terms of case too, as comparisons will
308+
/// be case-sensitive.
301309
pub fn relativize_with_prefix<'a>(relative_path: &'a Path, prefix: &Path) -> Cow<'a, Path> {
302310
if prefix.as_os_str().is_empty() {
303311
return Cow::Borrowed(relative_path);

0 commit comments

Comments
 (0)