diff --git a/lib/src/repo_path.rs b/lib/src/repo_path.rs index d3ae6adda..bd0d8cf34 100644 --- a/lib/src/repo_path.rs +++ b/lib/src/repo_path.rs @@ -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) -> Option { + 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::>()?; + Some(RepoPath::from_components(components)) + } + pub fn from_components(components: Vec) -> Self { RepoPath { components } } @@ -103,14 +119,8 @@ impl RepoPath { if repo_relative_path == Path::new(".") { return Ok(RepoPath::root()); } - let components = repo_relative_path - .components() - .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)) + Self::from_relative_path(repo_relative_path) + .ok_or_else(|| FsPathParseError::InputNotInRepo(input.to_owned())) } /// The full string form used internally, not for presenting to users (where