Skip to content

Commit 5b7c771

Browse files
committed
refactor
1 parent 40e8e51 commit 5b7c771

File tree

2 files changed

+21
-18
lines changed

2 files changed

+21
-18
lines changed

gix-fs/src/stack.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,11 @@ impl Stack {
6464
/// or have leading or trailing slashes (or additionally backslashes on Windows).
6565
pub fn make_relative_path_current(
6666
&mut self,
67-
relative: impl gix_path::ToPathComponents,
67+
relative: impl gix_path::ToNormalPathComponents,
6868
delegate: &mut dyn Delegate,
6969
) -> std::io::Result<()> {
70-
let components: Vec<_> = relative.to_components().collect();
71-
72-
if self.valid_components != 0 && components.is_empty() {
70+
let mut components = relative.to_normal_path_components().peekable();
71+
if self.valid_components != 0 && components.peek().is_none() {
7372
return Err(std::io::Error::new(
7473
std::io::ErrorKind::Other,
7574
"empty inputs are not allowed",
@@ -79,7 +78,6 @@ impl Stack {
7978
delegate.push_directory(self)?;
8079
}
8180

82-
let mut components = components.into_iter().peekable();
8381
let mut existing_components = self.current_relative.components();
8482
let mut matching_components = 0;
8583
while let (Some(existing_comp), Some(new_comp)) = (existing_components.next(), components.peek()) {

gix-path/src/convert.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -341,25 +341,30 @@ pub fn relativize_with_prefix<'a>(relative_path: &'a Path, prefix: &Path) -> Cow
341341
}
342342
}
343343

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),
344+
///
345+
pub mod to_normal_path_components {
346+
use std::ffi::OsStr;
347+
348+
/// The error used in [`ToNormalPathComponents::to_normal_path_components()`](crate::ToNormalPathComponents::to_normal_path_components()).
349+
#[derive(Debug, thiserror::Error)]
350+
#[allow(missing_docs)]
351+
pub enum Error<'a> {
352+
#[error("Input path {path:?} contains relative or absolute components", path = .0)]
353+
NotANormalComponent(&'a OsStr),
354+
}
350355
}
351356

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<'_>>>;
357+
/// Obtain an iterator over `OsStr`-components which are normal, none-relative and not absolute.
358+
pub trait ToNormalPathComponents {
359+
/// Return an iterator over the normal components of a path, without the separator.
360+
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error<'_>>>;
356361
}
357362

358-
impl ToPathComponents for &Path {
359-
fn to_components(&self) -> impl Iterator<Item = Result<&OsStr, Error<'_>>> {
363+
impl ToNormalPathComponents for &Path {
364+
fn to_normal_path_components(&self) -> impl Iterator<Item = Result<&OsStr, to_normal_path_components::Error<'_>>> {
360365
self.components().map(|component| match component {
361366
Component::Normal(os_str) => Ok(os_str),
362-
_ => Err(Error::NotANormalComponent(self.as_os_str())),
367+
_ => Err(to_normal_path_components::Error::NotANormalComponent(self.as_os_str())),
363368
})
364369
}
365370
}

0 commit comments

Comments
 (0)