Add new Path type with phantom data form tag (Any, Relative, Absolute, or Canonical).

This commit is contained in:
Ian Manske 2024-06-01 12:01:18 -04:00
parent 5ac3ad61c4
commit dd569b1237
3 changed files with 1804 additions and 0 deletions

View File

@ -0,0 +1,86 @@
mod private {
pub trait Sealed {}
}
pub trait PathForm: private::Sealed {}
pub trait MaybeRelative: PathForm {}
pub trait MaybeAbsolute: PathForm {}
pub trait IsAbsolute: PathForm {}
pub trait PathCast<Form: PathForm>: PathForm {}
impl<Form: PathForm> PathCast<Form> for Form {}
pub trait PathJoin: PathForm {
type Output: PathForm;
}
pub trait PathPush: PathForm {}
pub trait PathSet: PathForm {}
/// A path whose form is unknown. It could be a relative, absolute, or canonical path.
///
///
pub struct Any;
impl private::Sealed for Any {}
impl PathForm for Any {}
impl MaybeRelative for Any {}
impl MaybeAbsolute for Any {}
impl PathJoin for Any {
type Output = Self;
}
impl PathPush for Any {}
impl PathSet for Any {}
/// A strictly relative path.
///
/// The path is not guaranteed to be normalized. It may contain unresolved symlinks,
/// trailing slashes, dot components (`..` or `.`), and duplicate path separators.
pub struct Relative;
impl private::Sealed for Relative {}
impl PathForm for Relative {}
impl PathCast<Any> for Relative {}
impl MaybeRelative for Relative {}
impl PathJoin for Relative {
type Output = Any;
}
impl PathSet for Relative {}
/// An absolute path.
///
/// The path is not guaranteed to be normalized. It may contain unresolved symlinks,
/// trailing slashes, dot components (`..` or `.`), and duplicate path separators.
pub struct Absolute;
impl private::Sealed for Absolute {}
impl PathForm for Absolute {}
impl PathCast<Any> for Absolute {}
impl MaybeAbsolute for Absolute {}
impl IsAbsolute for Absolute {}
impl PathJoin for Absolute {
type Output = Self;
}
impl PathPush for Absolute {}
impl PathSet for Absolute {}
// A canonical path.
//
// An absolute path with all intermediate components normalized and symbolic links resolved.
pub struct Canonical;
impl private::Sealed for Canonical {}
impl PathForm for Canonical {}
impl PathCast<Any> for Canonical {}
impl PathCast<Absolute> for Canonical {}
impl MaybeAbsolute for Canonical {}
impl IsAbsolute for Canonical {}
impl PathJoin for Canonical {
type Output = Absolute;
}

View File

@ -2,12 +2,15 @@ mod assert_path_eq;
mod components;
pub mod dots;
pub mod expansions;
pub mod form;
mod helpers;
mod path;
mod tilde;
mod trailing_slash;
pub use components::components;
pub use expansions::{canonicalize_with, expand_path_with, expand_to_real_path, locate_in_dirs};
pub use helpers::{config_dir, config_dir_old, home_dir};
pub use path::*;
pub use tilde::expand_tilde;
pub use trailing_slash::{has_trailing_slash, strip_trailing_slash};

1715
crates/nu-path/src/path.rs Normal file

File diff suppressed because it is too large Load Diff