use rustdoc_types::Id;
use std::cmp::Ordering;
use std::fmt::Display;
use std::hash::Hash;
use crate::intermediate_public_item::IntermediatePublicItem;
use crate::render::RenderingContext;
use crate::tokens::Token;
use crate::tokens::tokens_to_string;
pub(crate) type PublicItemPath = Vec<String>;
#[derive(Clone)]
pub struct PublicItem {
pub(crate) sortable_path: PublicItemPath,
pub(crate) tokens: Vec<Token>,
pub(crate) parent_id: Option<Id>,
pub(crate) id: Id,
}
impl PublicItem {
pub(crate) fn from_intermediate_public_item(
context: &RenderingContext,
public_item: &IntermediatePublicItem<'_>,
) -> PublicItem {
PublicItem {
sortable_path: public_item.sortable_path(context),
tokens: public_item.render_token_stream(context),
parent_id: public_item.parent_id(),
id: public_item.id(),
}
}
#[must_use]
pub fn parent_id(&self) -> Option<Id> {
self.parent_id
}
#[must_use]
pub fn id(&self) -> Id {
self.id
}
pub fn tokens(&self) -> impl Iterator<Item = &Token> {
self.tokens.iter()
}
#[must_use]
pub fn grouping_cmp(&self, other: &Self) -> std::cmp::Ordering {
if let Some(ordering) = different_or_none(&self.sortable_path, &other.sortable_path) {
return ordering;
}
self.to_string().cmp(&other.to_string())
}
}
impl PartialEq for PublicItem {
fn eq(&self, other: &Self) -> bool {
self.tokens == other.tokens
}
}
impl Eq for PublicItem {}
impl Hash for PublicItem {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.tokens.hash(state);
}
}
impl std::fmt::Debug for PublicItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Display::fmt(self, f)
}
}
impl Display for PublicItem {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", tokens_to_string(&self.tokens))
}
}
fn different_or_none<T: Ord>(a: &T, b: &T) -> Option<Ordering> {
match a.cmp(b) {
Ordering::Equal => None,
c => Some(c),
}
}