Debian Codegraph @

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

raw
1 use crate::errors::{ErrorKind, Result};
2 use crate::fsshttpb::data::exguid::ExGuid;
3 use crate::one::property_set::PropertySetId;
4 use crate::onenote::embedded_file::{parse_embedded_file, EmbeddedFile};
5 use crate::onenote::image::{parse_image, Image};
6 use crate::onenote::ink::{parse_ink, Ink};
7 use crate::onenote::rich_text::{parse_rich_text, RichText};
8 use crate::onenote::table::{parse_table, Table};
9 use crate::onestore::object_space::ObjectSpace;
10
11 /// The content of an outline.
12 #[derive(Clone, Debug)]
13 pub enum Content {
14 /// A rich-text block.
15 RichText(RichText),
16
17 /// A table.
18 Table(Table),
19
20 /// An embedded image.
21 Image(Image),
22
23 /// An embedded file.
24 EmbeddedFile(EmbeddedFile),
25
26 /// An ink drawing
27 Ink(Ink),
28
29 /// Content of unknown type.
30 Unknown,
31 }
32
33 impl Content {
34 /// Return the rich-text data if it's a rich-text content block.
35 pub fn rich_text(&self) -> Option<&RichText> {
36 if let Content::RichText(rich_text) = self {
37 Some(rich_text)
38 } else {
39 None
40 }
41 }
42
43 /// Return the table data if it's a table content block.
44 pub fn table(&self) -> Option<&Table> {
45 if let Content::Table(table) = self {
46 Some(table)
47 } else {
48 None
49 }
50 }
51
52 /// Return the image data if it's a image content block.
53 pub fn image(&self) -> Option<&Image> {
54 if let Content::Image(image) = self {
55 Some(image)
56 } else {
57 None
58 }
59 }
60
61 /// Return the embedded file data if it's a embedded file content block.
62 pub fn embedded_file(&self) -> Option<&EmbeddedFile> {
63 if let Content::EmbeddedFile(file) = self {
64 Some(file)
65 } else {
66 None
67 }
68 }
69
70 /// Return the ink data if it's an ink content.
71 pub fn ink(&self) -> Option<&Ink> {
72 if let Content::Ink(ink) = self {
73 Some(ink)
74 } else {
75 None
76 }
77 }
78 }
79
80 pub(crate) fn parse_content(content_id: ExGuid, space: &ObjectSpace) -> Result<Content> {
81 let content_type = space
82 .get_object(content_id)
83 .ok_or_else(|| ErrorKind::MalformedOneNoteData("page content is missing".into()))?
84 .id();
85 let id = PropertySetId::from_jcid(content_type).ok_or_else(|| {
86 ErrorKind::MalformedOneNoteData(
87 format!("invalid property set id: 0x{:X}", content_type.0).into(),
88 )
89 })?;
90
91 let content = match id {
92 PropertySetId::ImageNode => Content::Image(parse_image(content_id, space)?),
93 PropertySetId::EmbeddedFileNode => {
94 Content::EmbeddedFile(parse_embedded_file(content_id, space)?)
95 }
96 PropertySetId::RichTextNode => Content::RichText(parse_rich_text(content_id, space)?),
97 PropertySetId::TableNode => Content::Table(parse_table(content_id, space)?),
98 PropertySetId::InkContainer => Content::Ink(parse_ink(content_id, space)?),
99 _ => Content::Unknown,
100 };
101
102 Ok(content)
103 }