| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 3 |
use crate::one::property_set::toc_container; |
| 4 |
use crate::onenote::section::SectionEntry; |
| 5 |
use crate::onestore::object_space::ObjectSpace; |
| 6 |
use itertools::Itertools; |
| 7 |
|
| 8 |
/// A OneNote notebook. |
| 9 |
#[derive(Clone, Debug)] |
| 10 |
pub struct Notebook { |
| 11 |
pub(crate) entries: Vec<SectionEntry>, |
| 12 |
} |
| 13 |
|
| 14 |
impl Notebook { |
| 15 |
/// The section entries of this notebook. |
| 16 |
pub fn entries(&self) -> &[SectionEntry] { |
| 17 |
&self.entries |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
pub(crate) fn parse_toc(space: &ObjectSpace) -> Result<Vec<String>> { |
| 22 |
let content_id = space |
| 23 |
.content_root() |
| 24 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("notebook has no content root".into()))?; |
| 25 |
|
| 26 |
let toc = parse_toc_entry(content_id, space)? |
| 27 |
.into_iter() |
| 28 |
.sorted_by_key(|(ordering_id, _)| *ordering_id) |
| 29 |
.dedup_by(|(_, a), (_, b)| a == b) |
| 30 |
.map(|(_, name)| name) |
| 31 |
.collect(); |
| 32 |
|
| 33 |
Ok(toc) |
| 34 |
} |
| 35 |
|
| 36 |
fn parse_toc_entry(content_id: ExGuid, space: &ObjectSpace) -> Result<Vec<(u32, String)>> { |
| 37 |
let content = space.get_object(content_id).ok_or_else(|| { |
| 38 |
ErrorKind::MalformedOneNoteData("notebook content root is missing".into()) |
| 39 |
})?; |
| 40 |
|
| 41 |
let toc = toc_container::parse(content)?; |
| 42 |
|
| 43 |
if let Some(name) = toc.filename { |
| 44 |
let ordering_id = toc |
| 45 |
.ordering_id |
| 46 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("section has no order id".into()))?; |
| 47 |
|
| 48 |
Ok(vec![(ordering_id, name)]) |
| 49 |
} else { |
| 50 |
let children = toc |
| 51 |
.children |
| 52 |
.into_iter() |
| 53 |
.map(|content_id| parse_toc_entry(content_id, space)) |
| 54 |
.collect::<Result<Vec<_>>>()? |
| 55 |
.into_iter() |
| 56 |
.flatten() |
| 57 |
.collect(); |
| 58 |
|
| 59 |
Ok(children) |
| 60 |
} |
| 61 |
} |