Debian Codegraph @

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

raw
1 use crate::errors::{ErrorKind, Result};
2 use crate::fsshttpb::packaging::OneStorePackaging;
3 use crate::onenote::notebook::Notebook;
4 use crate::onenote::section::{Section, SectionEntry, SectionGroup};
5 use crate::onestore::parse_store;
6 use crate::reader::Reader;
7 use std::ffi::OsStr;
8 use std::fs::File;
9 use std::io::{BufReader, Read, Cursor};
10 use std::path::Path;
11 use std::str::FromStr;
12
13 pub(crate) mod content;
14 pub(crate) mod embedded_file;
15 pub(crate) mod iframe;
16 pub(crate) mod image;
17 pub(crate) mod ink;
18 pub(crate) mod list;
19 pub(crate) mod note_tag;
20 pub(crate) mod notebook;
21 pub(crate) mod outline;
22 pub(crate) mod page;
23 pub(crate) mod page_content;
24 pub(crate) mod page_series;
25 pub(crate) mod rich_text;
26 pub(crate) mod section;
27 pub(crate) mod table;
28
29 /// The OneNote file parser.
30 pub struct Parser;
31
32 impl Parser {
33 /// Create a new OneNote file parser.
34 pub fn new() -> Parser {
35 Parser {}
36 }
37
38 /// Parse a OneNote notebook.
39 ///
40 /// The `path` argument must point to a `.onetoc2` file. This will parse the
41 /// table of contents of the notebook as well as all contained
42 /// sections from the folder that the table of contents file is in.
43 pub fn parse_notebook(&mut self, path: &Path) -> Result<Notebook> {
44 let file = File::open(path)?;
45 let data = Parser::read(file)?;
46 let packaging = OneStorePackaging::parse(&mut Reader::new(data.as_slice()))?;
47 let store = parse_store(&packaging)?;
48
49 if store.schema_guid() != guid!({E4DBFD38-E5C7-408B-A8A1-0E7B421E1F5F}) {
50 return Err(ErrorKind::NotATocFile {
51 file: path.to_string_lossy().to_string(),
52 }
53 .into());
54 }
55
56 let base_dir = path.parent().expect("no base dir found");
57 let sections = notebook::parse_toc(store.data_root())?
58 .iter()
59 .map(|name| {
60 let mut file = base_dir.to_path_buf();
61 file.push(name);
62
63 file
64 })
65 .filter(|p| p.exists())
66 .filter(|p| !p.ends_with("OneNote_RecycleBin"))
67 .map(|path| {
68 if path.is_file() {
69 self.parse_section(&path).map(SectionEntry::Section)
70 } else {
71 self.parse_section_group(&path)
72 .map(SectionEntry::SectionGroup)
73 }
74 })
75 .collect::<Result<_>>()?;
76
77 Ok(Notebook { entries: sections })
78 }
79
80 /// Parse a OneNote section buffer.
81 ///
82 /// The `data` argument must contain a OneNote section.
83 pub fn parse_section_buffer(&mut self, data: &[u8], file_name: &Path) -> Result<Section> {
84 let packaging = OneStorePackaging::parse(&mut Reader::new(data))?;
85 let store = parse_store(&packaging)?;
86
87 if store.schema_guid() != guid!({1F937CB4-B26F-445F-B9F8-17E20160E461}) {
88 return Err(ErrorKind::NotASectionFile {
89 file: file_name.to_string_lossy().into_owned(),
90 }
91 .into());
92 }
93
94 section::parse_section(
95 store,
96 file_name.to_string_lossy().into_owned(),
97 )
98 }
99
100 /// Parse a OneNote section file.
101 ///
102 /// The `path` argument must point to a `.one` file that contains a
103 /// OneNote section.
104 pub fn parse_section(&mut self, path: &Path) -> Result<Section> {
105 let file = File::open(path)?;
106 let data = Parser::read(file)?;
107 let packaging = OneStorePackaging::parse(&mut Reader::new(data.as_slice()))?;
108 let store = parse_store(&packaging)?;
109
110 if store.schema_guid() != guid!({1F937CB4-B26F-445F-B9F8-17E20160E461}) {
111 return Err(ErrorKind::NotASectionFile {
112 file: path.to_string_lossy().to_string(),
113 }
114 .into());
115 }
116
117 section::parse_section(
118 store,
119 path.file_name()
120 .expect("file without file name")
121 .to_string_lossy()
122 .to_string(),
123 )
124 }
125
126 fn parse_section_group(&mut self, path: &Path) -> Result<SectionGroup> {
127 let display_name = path
128 .file_name()
129 .expect("file without file name")
130 .to_string_lossy()
131 .to_string();
132
133 for entry in path.read_dir()? {
134 let entry = entry?;
135 let is_toc = entry
136 .path()
137 .extension()
138 .map(|ext| ext == OsStr::new("onetoc2"))
139 .unwrap_or_default();
140
141 if is_toc {
142 return self
143 .parse_notebook(&entry.path())
144 .map(|group| SectionGroup {
145 display_name,
146 entries: group.entries,
147 });
148 }
149 }
150
151 Err(ErrorKind::TocFileMissing {
152 dir: path.as_os_str().to_string_lossy().into_owned(),
153 }
154 .into())
155 }
156
157 fn read(file: File) -> Result<Vec<u8>> {
158 let size = file.metadata()?.len();
159 let mut data = Vec::with_capacity(size as usize);
160
161 let mut buf = BufReader::new(file);
162 buf.read_to_end(&mut data)?;
163
164 Ok(data)
165 }
166 }
167
168 impl Default for Parser {
169 fn default() -> Self {
170 Self::new()
171 }
172 }