| 1 |
use crate::errors::{ErrorKind, Result}; |
| 2 |
use crate::fsshttpb::data::cell_id::CellId; |
| 3 |
use crate::fsshttpb::data::exguid::ExGuid; |
| 4 |
use crate::fsshttpb::data_element::object_group::ObjectGroupData; |
| 5 |
use crate::fsshttpb::data_element::storage_index::{StorageIndex, StorageIndexCellMapping}; |
| 6 |
use crate::fsshttpb::packaging::OneStorePackaging; |
| 7 |
use crate::onestore::object::Object; |
| 8 |
use crate::onestore::revision::Revision; |
| 9 |
use crate::onestore::revision_role::RevisionRole; |
| 10 |
use std::collections::HashMap; |
| 11 |
|
| 12 |
pub(crate) type GroupData<'a> = HashMap<(ExGuid, u64), &'a ObjectGroupData>; |
| 13 |
|
| 14 |
/// A OneNote object space. |
| 15 |
/// |
| 16 |
/// Typically this is a section's metadata or a page and its content. |
| 17 |
/// |
| 18 |
/// See [\[MS-ONESTOR\] 2.1.4] |
| 19 |
/// |
| 20 |
/// [\[MS-ONESTOR\] 2.1.4]: https://docs.microsoft.com/en-us/openspecs/office_file_formats/ms-onestore/1329433f-02a5-4e83-ab41-80d57ade38d9 |
| 21 |
#[derive(Debug)] |
| 22 |
pub(crate) struct ObjectSpace<'a> { |
| 23 |
id: ExGuid, |
| 24 |
context: ExGuid, |
| 25 |
roots: HashMap<RevisionRole, ExGuid>, |
| 26 |
objects: HashMap<ExGuid, Object<'a>>, |
| 27 |
} |
| 28 |
|
| 29 |
impl<'a, 'b> ObjectSpace<'a> { |
| 30 |
pub(crate) fn get_object(&self, id: ExGuid) -> Option<&Object> { |
| 31 |
self.objects.get(&id) |
| 32 |
} |
| 33 |
|
| 34 |
pub(crate) fn content_root(&self) -> Option<ExGuid> { |
| 35 |
self.roots.get(&RevisionRole::DefaultContent).copied() |
| 36 |
} |
| 37 |
|
| 38 |
pub(crate) fn metadata_root(&self) -> Option<ExGuid> { |
| 39 |
self.roots.get(&RevisionRole::Metadata).copied() |
| 40 |
} |
| 41 |
|
| 42 |
pub(crate) fn parse( |
| 43 |
mapping: &'a StorageIndexCellMapping, |
| 44 |
storage_index: &'a StorageIndex, |
| 45 |
packaging: &'a OneStorePackaging, |
| 46 |
revision_cache: &'b mut HashMap<CellId, Revision<'a>>, |
| 47 |
) -> Result<(CellId, ObjectSpace<'a>)> { |
| 48 |
let cell_id = mapping.cell_id; |
| 49 |
|
| 50 |
let context_id = cell_id.0; |
| 51 |
let object_space_id = cell_id.1; |
| 52 |
|
| 53 |
let cell_manifest_id = ObjectSpace::find_cell_manifest_id(mapping.id, packaging) |
| 54 |
.ok_or_else(|| ErrorKind::MalformedOneStoreData("cell manifest id not found".into()))?; |
| 55 |
let revision_manifest_id = storage_index |
| 56 |
.find_revision_mapping_id(cell_manifest_id) |
| 57 |
.ok_or_else(|| { |
| 58 |
ErrorKind::MalformedOneStoreData("no revision manifest id found".into()) |
| 59 |
})?; |
| 60 |
|
| 61 |
let mut objects = HashMap::new(); |
| 62 |
let mut roots = HashMap::new(); |
| 63 |
|
| 64 |
let mut rev_id = Some(revision_manifest_id); |
| 65 |
|
| 66 |
while let Some(revision_manifest_id) = rev_id { |
| 67 |
let base_rev_id = Revision::parse( |
| 68 |
revision_manifest_id, |
| 69 |
context_id, |
| 70 |
object_space_id, |
| 71 |
storage_index, |
| 72 |
packaging, |
| 73 |
revision_cache, |
| 74 |
&mut objects, |
| 75 |
&mut roots, |
| 76 |
)?; |
| 77 |
|
| 78 |
rev_id = base_rev_id; |
| 79 |
} |
| 80 |
|
| 81 |
let space = ObjectSpace { |
| 82 |
id: object_space_id, |
| 83 |
context: context_id, |
| 84 |
roots, |
| 85 |
objects, |
| 86 |
}; |
| 87 |
|
| 88 |
Ok((cell_id, space)) |
| 89 |
} |
| 90 |
|
| 91 |
fn find_cell_manifest_id( |
| 92 |
cell_manifest_id: ExGuid, |
| 93 |
packaging: &'a OneStorePackaging, |
| 94 |
) -> Option<ExGuid> { |
| 95 |
packaging |
| 96 |
.data_element_package |
| 97 |
.cell_manifests |
| 98 |
.get(&cell_manifest_id) |
| 99 |
.copied() |
| 100 |
} |
| 101 |
} |