| 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::outline::{parse_outline, Outline}; |
| 8 |
use crate::onestore::object_space::ObjectSpace; |
| 9 |
|
| 10 |
/// The contents of a page. |
| 11 |
#[allow(missing_docs)] |
| 12 |
#[derive(Clone, Debug)] |
| 13 |
pub enum PageContent { |
| 14 |
Outline(Outline), |
| 15 |
Image(Image), |
| 16 |
EmbeddedFile(EmbeddedFile), |
| 17 |
Ink(Ink), |
| 18 |
Unknown, |
| 19 |
} |
| 20 |
|
| 21 |
impl PageContent { |
| 22 |
/// Return the outline data if it's an outline content. |
| 23 |
pub fn outline(&self) -> Option<&Outline> { |
| 24 |
if let PageContent::Outline(outline) = self { |
| 25 |
Some(outline) |
| 26 |
} else { |
| 27 |
None |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
/// Return the image data if it's an image content. |
| 32 |
pub fn image(&self) -> Option<&Image> { |
| 33 |
if let PageContent::Image(image) = self { |
| 34 |
Some(image) |
| 35 |
} else { |
| 36 |
None |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
/// Return the embedded file data if it's an embedded file content. |
| 41 |
pub fn embedded_file(&self) -> Option<&EmbeddedFile> { |
| 42 |
if let PageContent::EmbeddedFile(embedded_file) = self { |
| 43 |
Some(embedded_file) |
| 44 |
} else { |
| 45 |
None |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
/// Return the ink data if it's an ink content. |
| 50 |
pub fn ink(&self) -> Option<&Ink> { |
| 51 |
if let PageContent::Ink(ink) = self { |
| 52 |
Some(ink) |
| 53 |
} else { |
| 54 |
None |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
pub(crate) fn parse_page_content(content_id: ExGuid, space: &ObjectSpace) -> Result<PageContent> { |
| 60 |
let content_type = space |
| 61 |
.get_object(content_id) |
| 62 |
.ok_or_else(|| ErrorKind::MalformedOneNoteData("page content is missing".into()))? |
| 63 |
.id(); |
| 64 |
let id = PropertySetId::from_jcid(content_type).ok_or_else(|| { |
| 65 |
ErrorKind::MalformedOneNoteData( |
| 66 |
format!("invalid property set id: {:?}", content_type).into(), |
| 67 |
) |
| 68 |
})?; |
| 69 |
|
| 70 |
let content = match id { |
| 71 |
PropertySetId::ImageNode => PageContent::Image(parse_image(content_id, space)?), |
| 72 |
PropertySetId::EmbeddedFileNode => { |
| 73 |
PageContent::EmbeddedFile(parse_embedded_file(content_id, space)?) |
| 74 |
} |
| 75 |
PropertySetId::OutlineNode => PageContent::Outline(parse_outline(content_id, space)?), |
| 76 |
PropertySetId::InkContainer => PageContent::Ink(parse_ink(content_id, space)?), |
| 77 |
_ => PageContent::Unknown, |
| 78 |
}; |
| 79 |
|
| 80 |
Ok(content) |
| 81 |
} |