mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-07 05:16:33 +00:00
repo_path: extract helper that converts Path to RepoPath literally
This commit is contained in:
parent
13c93d5270
commit
a4f6e0de0b
1 changed files with 18 additions and 8 deletions
|
@ -83,6 +83,22 @@ impl RepoPath {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Converts repo-relative `Path` to `RepoPath`.
|
||||||
|
///
|
||||||
|
/// The input path should not contain `.` or `..`.
|
||||||
|
pub fn from_relative_path(relative_path: impl AsRef<Path>) -> Option<Self> {
|
||||||
|
let relative_path = relative_path.as_ref();
|
||||||
|
let components = relative_path
|
||||||
|
.components()
|
||||||
|
.map(|c| match c {
|
||||||
|
Component::Normal(a) => Some(RepoPathComponent::from(a.to_str().unwrap())),
|
||||||
|
// TODO: better to return Err instead of None?
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect::<Option<_>>()?;
|
||||||
|
Some(RepoPath::from_components(components))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_components(components: Vec<RepoPathComponent>) -> Self {
|
pub fn from_components(components: Vec<RepoPathComponent>) -> Self {
|
||||||
RepoPath { components }
|
RepoPath { components }
|
||||||
}
|
}
|
||||||
|
@ -103,14 +119,8 @@ impl RepoPath {
|
||||||
if repo_relative_path == Path::new(".") {
|
if repo_relative_path == Path::new(".") {
|
||||||
return Ok(RepoPath::root());
|
return Ok(RepoPath::root());
|
||||||
}
|
}
|
||||||
let components = repo_relative_path
|
Self::from_relative_path(repo_relative_path)
|
||||||
.components()
|
.ok_or_else(|| FsPathParseError::InputNotInRepo(input.to_owned()))
|
||||||
.map(|c| match c {
|
|
||||||
Component::Normal(a) => Ok(RepoPathComponent::from(a.to_str().unwrap())),
|
|
||||||
_ => Err(FsPathParseError::InputNotInRepo(input.to_owned())),
|
|
||||||
})
|
|
||||||
.try_collect()?;
|
|
||||||
Ok(RepoPath::from_components(components))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The full string form used internally, not for presenting to users (where
|
/// The full string form used internally, not for presenting to users (where
|
||||||
|
|
Loading…
Reference in a new issue