docx_rs/reader/
document_rels.rs

1use std::collections::BTreeMap;
2use std::collections::BTreeSet;
3use std::io::Cursor;
4use std::path::*;
5
6use document_rels::rels::find_rels_filename;
7use document_rels::rels::read_rels_xml;
8
9use super::errors::*;
10use super::*;
11
12pub type RId = String;
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct ReadDocumentRels {
16    rels: BTreeMap<String, BTreeSet<(RId, PathBuf, Option<String>)>>,
17}
18
19impl ReadDocumentRels {
20    pub fn find_target_path(&self, target: &str) -> Option<Vec<(RId, PathBuf, Option<String>)>> {
21        self.rels
22            .get(target)
23            .map(|s| s.clone().into_iter().collect())
24    }
25}
26
27pub fn read_document_rels(
28    archive: &mut zip::read::ZipArchive<Cursor<&[u8]>>,
29    main_path: impl AsRef<Path>,
30) -> Result<ReadDocumentRels, ReaderError> {
31    let dir = &main_path
32        .as_ref()
33        .parent()
34        .ok_or(ReaderError::DocumentRelsNotFoundError)?;
35    let p = find_rels_filename(&main_path)?;
36    let p = p.to_str().ok_or(ReaderError::DocumentRelsNotFoundError)?;
37    let data = read_zip(archive, p)?;
38    let rels = read_rels_xml(&data[..], dir)?;
39    Ok(ReadDocumentRels { rels })
40}