Make ModPath's representation private

This commit is contained in:
Jonas Schievink 2021-02-04 20:49:24 +01:00
parent 36191543a6
commit 5d99ba1d9a
18 changed files with 70 additions and 51 deletions

View file

@ -20,7 +20,7 @@ use crate::{
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ModPath {
pub kind: PathKind,
pub segments: Vec<Name>,
segments: Vec<Name>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
@ -53,6 +53,11 @@ impl ModPath {
ModPath { kind, segments }
}
/// Creates a `ModPath` from a `PathKind`, with no extra path segments.
pub const fn from_kind(kind: PathKind) -> ModPath {
ModPath { kind, segments: Vec::new() }
}
/// Calls `cb` with all paths, represented by this use item.
pub(crate) fn expand_use_item(
item_src: InFile<ast::Use>,
@ -64,6 +69,18 @@ impl ModPath {
}
}
pub fn segments(&self) -> &[Name] {
&self.segments
}
pub fn push_segment(&mut self, segment: Name) {
self.segments.push(segment);
}
pub fn pop_segment(&mut self) -> Option<Name> {
self.segments.pop()
}
/// Returns the number of segments in the path (counting special segments like `$crate` and
/// `super`).
pub fn len(&self) -> usize {
@ -78,7 +95,7 @@ impl ModPath {
}
pub fn is_ident(&self) -> bool {
self.kind == PathKind::Plain && self.segments.len() == 1
self.as_ident().is_some()
}
pub fn is_self(&self) -> bool {
@ -87,10 +104,14 @@ impl ModPath {
/// If this path is a single identifier, like `foo`, return its name.
pub fn as_ident(&self) -> Option<&Name> {
if !self.is_ident() {
if self.kind != PathKind::Plain {
return None;
}
self.segments.first()
match &*self.segments {
[name] => Some(name),
_ => None,
}
}
}