From 0c518b47e65f5568e14a2e12a6798b85212f8a83 Mon Sep 17 00:00:00 2001 From: John Date: Mon, 22 Apr 2024 21:04:30 -0500 Subject: [PATCH] cl-ast: Give Path some inherent methods --- compiler/cl-ast/src/ast.rs | 2 +- compiler/cl-ast/src/ast_impl.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/compiler/cl-ast/src/ast.rs b/compiler/cl-ast/src/ast.rs index 1a8b271..4ddf897 100644 --- a/compiler/cl-ast/src/ast.rs +++ b/compiler/cl-ast/src/ast.rs @@ -284,7 +284,7 @@ pub struct TyFn { } /// 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 absolute: bool, pub parts: Vec, diff --git a/compiler/cl-ast/src/ast_impl.rs b/compiler/cl-ast/src/ast_impl.rs index 1c8b89e..1c47709 100644 --- a/compiler/cl-ast/src/ast_impl.rs +++ b/compiler/cl-ast/src/ast_impl.rs @@ -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 { + 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) + } + } +}