1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use std::cmp::Ordering;
use std::fmt::Display;
use crate::intermediate_public_item::IntermediatePublicItem;
use crate::render::RenderingContext;
use crate::tokens::tokens_to_string;
use crate::tokens::Token;
pub(crate) type PublicItemPath = Vec<String>;
#[derive(Clone, Eq, PartialEq, Hash)]
pub struct PublicItem {
pub(crate) sortable_path: PublicItemPath,
pub(crate) tokens: Vec<Token>,
}
impl PublicItem {
pub(crate) fn from_intermediate_public_item(
context: &RenderingContext,
public_item: &IntermediatePublicItem<'_>,
) -> PublicItem {
PublicItem {
sortable_path: public_item.sortable_path(),
tokens: public_item.render_token_stream(context),
}
}
pub fn tokens(&self) -> impl Iterator<Item = &Token> {
self.tokens.iter()
}
}
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))
}
}
impl PartialOrd for PublicItem {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
fn different_or_none<T: Ord>(a: &T, b: &T) -> Option<Ordering> {
match a.cmp(b) {
Ordering::Equal => None,
c => Some(c),
}
}
impl Ord for PublicItem {
fn 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())
}
}