Skip to content

Commit

Permalink
Change ItemPath internal representation
Browse files Browse the repository at this point in the history
  • Loading branch information
Sasha Pourcelot committed Jun 14, 2021
1 parent 972386c commit 8511043
Showing 1 changed file with 13 additions and 32 deletions.
45 changes: 13 additions & 32 deletions src/public_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use std::{
cmp::Ordering,
collections::HashMap,
fmt::{Display, Formatter, Result as FmtResult},
iter,
};

use syn::{visit::Visit, Ident};
Expand Down Expand Up @@ -70,33 +69,30 @@ impl PublicApi {
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq)]
pub(crate) struct ItemPath {
path: Vec<Ident>,
last: Ident,
}

impl ItemPath {
fn new(path: Vec<Ident>, last: Ident) -> ItemPath {
ItemPath { last, path }
}

fn path_idents(&self) -> impl Iterator<Item = &Ident> {
self.path.iter().chain(iter::once(&self.last))
}

fn len(&self) -> usize {
self.path.len() + 1
fn new(mut path: Vec<Ident>, last: Ident) -> ItemPath {
path.push(last);
ItemPath { path }
}
}

impl Display for ItemPath {
fn fmt(&self, f: &mut Formatter) -> FmtResult {
self.path
.iter()
.try_for_each(|segment| write!(f, "{}::", segment))?;
if let Some(first) = self.path.first() {
write!(f, "{}", first)?;

self.path
.iter()
.skip(1)
.try_for_each(|segment| write!(f, "::{}", segment))?;
}

write!(f, "{}", self.last)
Ok(())
}
}

Expand All @@ -106,21 +102,6 @@ impl PartialOrd for ItemPath {
}
}

impl Ord for ItemPath {
fn cmp(&self, other: &Self) -> Ordering {
let idents = self.path_idents().zip(other.path_idents());

for (seg_a, seg_b) in idents {
let order = seg_a.cmp(seg_b);
if order != Ordering::Equal {
return order;
}
}

self.len().cmp(&other.len())
}
}

#[cfg(test)]
impl Parse for ItemPath {
fn parse(input: ParseStream) -> ParseResult<ItemPath> {
Expand Down

0 comments on commit 8511043

Please sign in to comment.