Skip to content

Commit 34deb0b

Browse files
committed
refactor
1 parent 40e8e51 commit 34deb0b

File tree

4 files changed

+46
-40
lines changed

4 files changed

+46
-40
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gix-fs/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
description = "A crate providing file system specific utilities to `gitoxide`"
99
authors = ["Sebastian Thiel <[email protected]>"]
1010
edition = "2021"
11-
rust-version = "1.70"
11+
rust-version = "1.74"
1212
include = ["src/**/*", "LICENSE-*"]
1313

1414
[lib]
@@ -20,8 +20,8 @@ serde = ["dep:serde"]
2020

2121
[dependencies]
2222
gix-features = { version = "^0.40.0", path = "../gix-features", features = ["fs-read-dir"] }
23-
gix-path = { version = "^0.10.14", path = "../gix-path" }
2423
gix-utils = { version = "^0.1.14", path = "../gix-utils" }
24+
thiserror = "2.0.0"
2525
serde = { version = "1.0.114", optional = true, default-features = false, features = ["std", "derive"] }
2626

2727
# For `Capabilities` to assure parallel operation works.

gix-fs/src/stack.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,43 @@
1-
use std::path::{Path, PathBuf};
1+
use std::ffi::OsStr;
2+
use std::path::{Component, Path, PathBuf};
23

34
use crate::Stack;
45

6+
///
7+
pub mod to_normal_path_components {
8+
use std::ffi::OsString;
9+
10+
/// The error used in [`ToNormalPathComponents::to_normal_path_components()`](crate::ToNormalPathComponents::to_normal_path_components()).
11+
#[derive(Debug, thiserror::Error)]
12+
#[allow(missing_docs)]
13+
pub enum Error {
14+
#[error("Input path \"{path}\" contains relative or absolute components", path = std::path::Path::new(.0.as_os_str()).display())]
15+
NotANormalComponent(OsString),
16+
}
17+
}
18+
19+
/// Obtain an iterator over `OsStr`-components which are normal, none-relative and not absolute.
20+
pub trait ToNormalPathComponents {
21+
/// Return an iterator over the normal components of a path, without the separator.
22+
// TODO(MSRV): turn this into `impl Iterator` once MSRV is 1.75 or higher
23+
fn to_normal_path_components(
24+
&self,
25+
) -> Box<dyn Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> + '_>;
26+
}
27+
28+
impl ToNormalPathComponents for &Path {
29+
fn to_normal_path_components(
30+
&self,
31+
) -> Box<dyn Iterator<Item = Result<&OsStr, to_normal_path_components::Error>> + '_> {
32+
Box::new(self.components().map(|component| match component {
33+
Component::Normal(os_str) => Ok(os_str),
34+
_ => Err(to_normal_path_components::Error::NotANormalComponent(
35+
self.as_os_str().to_owned(),
36+
)),
37+
}))
38+
}
39+
}
40+
541
/// Access
642
impl Stack {
743
/// Returns the top-level path of the stack.
@@ -64,12 +100,11 @@ impl Stack {
64100
/// or have leading or trailing slashes (or additionally backslashes on Windows).
65101
pub fn make_relative_path_current(
66102
&mut self,
67-
relative: impl gix_path::ToPathComponents,
103+
relative: impl ToNormalPathComponents,
68104
delegate: &mut dyn Delegate,
69105
) -> std::io::Result<()> {
70-
let components: Vec<_> = relative.to_components().collect();
71-
72-
if self.valid_components != 0 && components.is_empty() {
106+
let mut components = relative.to_normal_path_components().peekable();
107+
if self.valid_components != 0 && components.peek().is_none() {
73108
return Err(std::io::Error::new(
74109
std::io::ErrorKind::Other,
75110
"empty inputs are not allowed",
@@ -79,7 +114,6 @@ impl Stack {
79114
delegate.push_directory(self)?;
80115
}
81116

82-
let mut components = components.into_iter().peekable();
83117
let mut existing_components = self.current_relative.components();
84118
let mut matching_components = 0;
85119
while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {
@@ -92,8 +126,8 @@ impl Stack {
92126
break;
93127
}
94128
}
95-
Err(err) => return Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{err}"))),
96-
};
129+
Err(err) => return Err(std::io::Error::other(format!("{err}"))),
130+
}
97131
}
98132

99133
for _ in 0..self.valid_components - matching_components {
@@ -111,12 +145,7 @@ impl Stack {
111145
}
112146

113147
while let Some(comp) = components.next() {
114-
let comp = match comp {
115-
Ok(comp) => comp,
116-
Err(err) => {
117-
return Err(std::io::Error::new(std::io::ErrorKind::Other, format!("{err}")));
118-
}
119-
};
148+
let comp = comp.map_err(std::io::Error::other)?;
120149
let is_last_component = components.peek().is_none();
121150
self.current_is_directory = !is_last_component;
122151
self.current.push(comp);

gix-path/src/convert.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -340,26 +340,3 @@ pub fn relativize_with_prefix<'a>(relative_path: &'a Path, prefix: &Path) -> Cow
340340
Cow::Owned(buf)
341341
}
342342
}
343-
344-
/// The error used in [`ToPathComponents::to_components()`][crate::convert::ToPathComponents::to_components()].
345-
#[derive(Debug, thiserror::Error)]
346-
#[allow(missing_docs)]
347-
pub enum Error<'a> {
348-
#[error("Input path {path:?} contains relative or absolute components", path = .0)]
349-
NotANormalComponent(&'a OsStr),
350-
}
351-
352-
/// Obtain an iterator over `OsStr`-components.
353-
pub trait ToPathComponents {
354-
/// Return an iterator over the components of a path, without the separator.
355-
fn to_components(&self) -> impl Iterator<Item = Result<&OsStr, Error<'_>>>;
356-
}
357-
358-
impl ToPathComponents for &Path {
359-
fn to_components(&self) -> impl Iterator<Item = Result<&OsStr, Error<'_>>> {
360-
self.components().map(|component| match component {
361-
Component::Normal(os_str) => Ok(os_str),
362-
_ => Err(Error::NotANormalComponent(self.as_os_str())),
363-
})
364-
}
365-
}

0 commit comments

Comments
 (0)