Skip to main content

RepoPathNormalizer

Struct RepoPathNormalizer 

Source
pub struct RepoPathNormalizer<'a> { /* private fields */ }
Expand description

Converts user-supplied paths, relative to one working directory, into normalized repository-relative paths (as normalize_repo_relative does for a single path).

The working directory’s position in the work tree is resolved once, so each relative path is normalized lexically on its /-separated components with a single allocation for the result. Absolute paths and paths that climb above the work tree root take the general route, which also recognizes alternate spellings of the work tree (such as macOS /private/tmp for /tmp).

§Examples

use std::path::Path;
use grit_lib::config::environment::RepositoryOptions;
use grit_lib::repo::Repository;
use grit_lib::worktree::ignore::RepoPathNormalizer;

let repo = Repository::discover(None, &RepositoryOptions::empty())?;
let cwd = repo.work_tree().unwrap_or(Path::new(".")).join("src");
let normalizer = RepoPathNormalizer::new(&repo, &cwd)?;
assert_eq!(normalizer.normalize("./lib/../main.rs")?, "src/main.rs");
assert_eq!(normalizer.normalize("../docs/")?, "docs/");

Implementations§

Source§

impl<'a> RepoPathNormalizer<'a>

Source

pub fn new(repo: &'a Repository, cwd: &'a Path) -> Result<Self>

Prepare to normalize paths given relative to cwd.

§Parameters
  • repo - repository whose work tree paths are made relative to.
  • cwd - directory relative input paths start from (usually the process’s current directory); it may lie outside the work tree.
§Errors

Returns crate::worktree::WorktreeError::Path when repo has no work tree.

Source

pub fn normalize(&self, path: &str) -> Result<String>

Normalize one user-supplied path.

. and .. components are resolved lexically, a trailing / (a directory query) is kept, the work tree root itself becomes "", and paths starting with : are returned unchanged.

§Parameters
  • path - path as the user typed it, relative to the normalizer’s directory or absolute.
§Returns

The repository-relative path with / separators.

§Errors

Returns crate::worktree::WorktreeError::Path if the path resolves outside the work tree.

Trait Implementations§

Source§

impl<'a> Debug for RepoPathNormalizer<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.