cl-ast: Give Path some inherent methods

This commit is contained in:
John 2024-04-22 21:04:30 -05:00
parent 169f61144b
commit 0c518b47e6
2 changed files with 32 additions and 1 deletions

View File

@ -284,7 +284,7 @@ pub struct TyFn {
} }
/// A path to an [Item] in the [Module] tree /// A path to an [Item] in the [Module] tree
#[derive(Clone, Debug, PartialEq, Eq, Hash)] #[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
pub struct Path { pub struct Path {
pub absolute: bool, pub absolute: bool,
pub parts: Vec<PathPart>, pub parts: Vec<PathPart>,

View File

@ -774,3 +774,34 @@ mod convert {
} }
} }
} }
mod path {
//! Utils for [Path]
use crate::{ast::Path, Identifier, PathPart};
impl Path {
/// Appends a [PathPart] to this [Path]
pub fn push(&mut self, part: PathPart) {
self.parts.push(part);
}
/// Removes a [PathPart] from this [Path]
pub fn pop(&mut self) -> Option<PathPart> {
self.parts.pop()
}
/// Concatenates `self::other`. If `other` is an absolute [Path],
/// this replaces `self` with `other`
pub fn concat(&mut self, other: &Self) -> &mut Self {
if other.absolute {
*self = other.clone();
} else {
self.parts.extend(other.parts.iter().cloned())
}
self
}
}
impl PathPart {
pub fn from_ident(ident: Identifier) -> Self {
Self::Ident(ident)
}
}
}