use crate::{AncestorPath, FromPathError, Utf8PathBuf};
use alloc::vec::Vec;
use camino::{FromPathBufError, Utf8Components};
use core::str::FromStr;
pub type WorkspacePath = RootedPath;
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RootedPath {
pub up: Option<AncestorPath>,
pub down: Utf8PathBuf,
}
impl FromStr for RootedPath {
type Err = std::io::Error;
fn from_str(input: &str) -> core::result::Result<Self, Self::Err> {
use alloc::format;
use std::io::{Error, ErrorKind};
let up = AncestorPath::from_str(input).map_err(FromPathError::into_io_error)?;
let cwd = Utf8PathBuf::try_from(std::env::current_dir()?)
.map_err(FromPathBufError::into_io_error)?;
let mut parts = cwd.components().rev().take(up.depth()).collect::<Vec<_>>();
parts.reverse();
if parts.len() != up.depth() {
return Err(Error::new(
ErrorKind::Other,
format!("path depth mismatch: {} != {}", parts.len(), up.depth()),
));
}
let down = Utf8PathBuf::from_iter(parts);
Ok(Self { up: Some(up), down })
}
}