Skip to main content

ephemeris_documents/
entries.rs

1//! Object entries in render order.
2
3use std::slice;
4
5use crate::annotation::Annotated;
6
7/// Object entries, ordered by explicit `order` first and then by key.
8#[derive(Debug, Clone)]
9pub(crate) struct Entries {
10    items: Vec<(String, Annotated)>,
11    dotted: bool,
12}
13
14impl Entries {
15    pub(crate) fn sorted(mut entries: Vec<(Option<i64>, String, Annotated)>) -> Self {
16        entries.sort_unstable_by(|left, right| {
17            (left.0.is_none(), left.0, &left.1).cmp(&(right.0.is_none(), right.0, &right.1))
18        });
19        Self {
20            items: entries
21                .into_iter()
22                .map(|(_, key, value)| (key, value))
23                .collect(),
24            dotted: false,
25        }
26    }
27
28    pub(crate) const fn set_dotted(&mut self, dotted: bool) {
29        self.dotted = dotted;
30    }
31
32    pub(crate) const fn is_dotted(&self) -> bool {
33        self.dotted
34    }
35
36    /// Whether any entry carries a comment, at any depth.
37    pub(crate) fn carries_comment(&self) -> bool {
38        self.into_iter().any(|(_, entry)| entry.carries_comment())
39    }
40
41    pub(crate) const fn is_empty(&self) -> bool {
42        self.items.is_empty()
43    }
44}
45
46impl<'a> IntoIterator for &'a Entries {
47    type Item = &'a (String, Annotated);
48    type IntoIter = slice::Iter<'a, (String, Annotated)>;
49
50    fn into_iter(self) -> Self::IntoIter {
51        self.items.iter()
52    }
53}