Debian Codegraph @

.cargo/vendor/onenote_parser/src/onenote/section.rs

raw
1 use crate::errors::{ErrorKind, Result};
2 use crate::one::property::color::Color;
3 use crate::one::property_set::{section_metadata_node, section_node};
4 use crate::onenote::page_series::{parse_page_series, PageSeries};
5 use crate::onestore::object_space::ObjectSpace;
6 use crate::onestore::OneStore;
7
8 /// An entry in a section list.
9 #[allow(missing_docs)]
10 #[derive(Clone, Debug)]
11 pub enum SectionEntry {
12 Section(Section),
13 SectionGroup(SectionGroup),
14 }
15
16 /// A OneNote section.
17 ///
18 /// See [\[MS-ONE\] 1.3.1] and [\[MS-ONE\] 2.2.17].
19 ///
20 /// [\[MS-ONE\] 1.3.1]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/1603b29c-1c9f-4e85-b9b9-59684122374a
21 /// [\[MS-ONE\] 2.2.17]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-one/6913913f-b7d1-4b29-ab09-231ea3835ac2
22 #[derive(Clone, Debug)]
23 pub struct Section {
24 display_name: String,
25 page_series: Vec<PageSeries>,
26 color: Option<Color>,
27 }
28
29 impl Section {
30 /// The section name.
31 pub fn display_name(&self) -> &str {
32 &self.display_name
33 }
34
35 /// The page series contained within the section.
36 pub fn page_series(&self) -> &[PageSeries] {
37 &self.page_series
38 }
39
40 /// The color of the section.
41 pub fn color(&self) -> Option<Color> {
42 self.color
43 }
44 }
45
46 /// A group of sections.
47 #[derive(Clone, Debug)]
48 pub struct SectionGroup {
49 pub(crate) display_name: String,
50 pub(crate) entries: Vec<SectionEntry>,
51 }
52
53 impl SectionGroup {
54 /// The group name.
55 pub fn display_name(&self) -> &str {
56 &self.display_name
57 }
58
59 /// The sections contained within the group.
60 pub fn entries(&self) -> &[SectionEntry] {
61 &self.entries
62 }
63 }
64
65 pub(crate) fn parse_section(store: OneStore, filename: String) -> Result<Section> {
66 let metadata = parse_metadata(store.data_root())?;
67 let content = parse_content(store.data_root())?;
68
69 let display_name = metadata
70 .display_name
71 .unwrap_or(filename)
72 .trim_end_matches(".one")
73 .to_string();
74
75 let page_series = content
76 .page_series
77 .into_iter()
78 .map(|page_series_id| parse_page_series(page_series_id, &store))
79 .collect::<Result<_>>()?;
80
81 Ok(Section {
82 display_name,
83 page_series,
84 color: metadata.color,
85 })
86 }
87
88 fn parse_content(space: &ObjectSpace) -> Result<section_node::Data> {
89 let content_root_id = space
90 .content_root()
91 .ok_or_else(|| ErrorKind::MalformedOneNoteData("section has no content root".into()))?;
92 let content_object = space.get_object(content_root_id).ok_or_else(|| {
93 ErrorKind::MalformedOneNoteData("section content object is missing".into())
94 })?;
95
96 section_node::parse(content_object)
97 }
98
99 fn parse_metadata(space: &ObjectSpace) -> Result<section_metadata_node::Data> {
100 let metadata_root_id = space
101 .metadata_root()
102 .ok_or_else(|| ErrorKind::MalformedOneNoteData("section has no metadata root".into()))?;
103 let metadata_object = space.get_object(metadata_root_id).ok_or_else(|| {
104 ErrorKind::MalformedOneNoteData("section metadata object is missing".into())
105 })?;
106
107 section_metadata_node::parse(metadata_object)
108 }