pub struct PartFs { /* private fields */ }Expand description
Thin adapter over oxml_opc::OpcPackage. Port-equivalent of PartFS.
Implementations§
Source§impl PartFs
impl PartFs
Sourcepub fn open(bytes: &[u8]) -> Result<PartFs, OpcError>
pub fn open(bytes: &[u8]) -> Result<PartFs, OpcError>
Open a .docx/OPC package from raw bytes: read its parts,
relationships and [Content_Types].xml, normalize their part
names to the no-leading-slash form, fold any explicit <…></…>
closes of empty elements (<Relationship …></Relationship> and
its [Content_Types] siblings) into self-closing ones, decode
XML entities that the underlying parser left raw, and keep one
copy of any identical repeated relationship.
The package is read under crate::admission::InputLimits::compare
(#645): an input past the compressed-size or entry-count budget is
refused before the archive is indexed, and a part past the per-part
or package budget while it inflates, with the admission INPUT_LIMIT
wording. Callers that admit under their own budget open through
open_within so their limits keep governing.
§Errors
OpcError for an unreadable zip, a package past the input budget
(an crate::admission::AdmissionError with code INPUT_LIMIT as
the std::io::Error source), an OPC metadata element exceeding
256 attributes, two relationships under the same Id that differ in
any attribute, or two entries whose decoded part names are equal
byte-for-byte or under ASCII case
(OpcError::DuplicatePartName, naming the second entry’s
spelling; text surfaces print the admission DUPLICATE_PART
detail through refusal_message).
Sourcepub fn repaired_duplicate_ids(&self) -> &[(String, String)]
pub fn repaired_duplicate_ids(&self) -> &[(String, String)]
(part, Id) of each relationship the source repeated identically,
kept once on open ("": the package’s own relationships). Word
refuses such a package; validate reports these as DUPLICATE_RID.
Sourcepub fn part_bytes(&self, name: &str) -> Option<&[u8]>
pub fn part_bytes(&self, name: &str) -> Option<&[u8]>
PartFS.partBytes(name) — raw bytes of a part. The lookup resolves
under OPC identity: word/document.xml finds a part stored as
word/Document.xml (#600).
Sourcepub fn part_string(&self, name: &str) -> Option<String>
pub fn part_string(&self, name: &str) -> Option<String>
Read a part as a UTF-8 string.
Sourcepub fn set_part(&mut self, name: &str, data: Vec<u8>)
pub fn set_part(&mut self, name: &str, data: Vec<u8>)
PartFS.setPart(name, data) — replace or add a part.
A …/_rels/<part>.rels name replaces that part’s relationships: the
package keeps relationships parsed, and to_zip writes them next to the
raw parts, so a raw .rels copy would be a second zip entry of the same
name (the whole package fails) and invisible to read_rels_for.
Unparseable relationship XML is kept raw, as before.
PartFS.setPart(name, data) — replace or add a part. A name that
is a case alias of a stored part replaces it and keeps the stored
spelling (there is one part per identity, #600).
A …/_rels/<part>.rels name (matched under ASCII case) replaces
that part’s relationships: the package keeps relationships parsed,
and to_zip writes them next to the raw parts, so a raw .rels
copy would be a second zip entry of the same name (the whole
package fails) and invisible to read_rels_for. Unparseable
relationship XML is kept raw, as before; to_zip then refuses the
collision rather than write a duplicate name. A
[Content_Types].xml name likewise replaces the parsed content
types when it parses.
Sourcepub fn remove_part(&mut self, name: &str)
pub fn remove_part(&mut self, name: &str)
Remove a part (no-op when absent), resolved under OPC identity:
an alias removes the stored part, a .rels name removes the
owner’s relationships, and the [Content_Types].xml identity
clears the content types. Content-type overrides and rels pointing
at the part are left to the caller.
Sourcepub fn parts(&self) -> Vec<String>
pub fn parts(&self) -> Vec<String>
Enumerate all part names (docxodus style, no leading slash), sorted.
Sourcepub fn to_zip(&self) -> Result<Vec<u8>, OpcError>
pub fn to_zip(&self) -> Result<Vec<u8>, OpcError>
Serialize the package back to zip bytes.
Uses deflate compression level 1 (deflate_quick) instead of the
zip crate default level 6. Level 1 skips longest_match (the
largest WASM self-time frame, 26% of the deflate cluster per the W5
profile) while producing content-identical decompressed bytes — Word
opens any deflate level.
Every entry is dated 1980-01-01 00:00, as Office dates its own, and names Unix as its “made by” system whatever the host (the zip crate would pick DOS on Windows), so the same input writes the same bytes on every platform.
Sourcepub fn resolve_rel_target(&self, source_part: &str, rel_target: &str) -> String
pub fn resolve_rel_target(&self, source_part: &str, rel_target: &str) -> String
resolveRelTarget(sourcePart, relTarget) — the part a rel target names,
relative to its source part, as a canonical part name: no leading slash
(the form parts() returns) and ./.. segments resolved. An echoed
absolute target (“/word/footer1.xml”) was later re-prefixed into a
“//word/…” relationship, and “word/../customXml/item1.xml” named no part.
Sourcepub fn content_type_for(&self, name: &str) -> Option<String>
pub fn content_type_for(&self, name: &str) -> Option<String>
contentTypeFor(part).
Sourcepub fn add_content_type_override(&mut self, part_name: &str, content_type: &str)
pub fn add_content_type_override(&mut self, part_name: &str, content_type: &str)
Add an Override entry to [Content_Types].xml.
Sourcepub fn add_content_type_default(&mut self, ext: &str, content_type: &str)
pub fn add_content_type_default(&mut self, ext: &str, content_type: &str)
Add a Default extension mapping to [Content_Types].xml.
Sourcepub fn adopt_missing_content_types(&mut self, sources: &[&PartFs])
pub fn adopt_missing_content_types(&mut self, sources: &[&PartFs])
Give every part that has no content type the one its source declares: the source’s entry for the same part name, else the source’s Default for the extension (case-insensitive, as OPC matches extensions). A part no source types is left alone rather than guessed.
Sourcepub fn remove_content_type_override(&mut self, part_name: &str)
pub fn remove_content_type_override(&mut self, part_name: &str)
Remove an Override entry from [Content_Types].xml (no-op when
absent). The name resolves under OPC identity, so an alias removes
the stored spelling (#600).
Sourcepub fn remove_relationships_by_type(
&mut self,
source_part: &str,
rel_type: &str,
)
pub fn remove_relationships_by_type( &mut self, source_part: &str, rel_type: &str, )
Remove every relationship of source_part with the given type.
No-op when the source part has no relationships part yet — must not
invent an empty .rels entry just to remove from it (PR #81 review).
Sourcepub fn remove_package_relationships_by_type(&mut self, rel_type: &str)
pub fn remove_package_relationships_by_type(&mut self, rel_type: &str)
Remove every package-level relationship (_rels/.rels) of the given
type. The parts they target are left to the caller.
Remove the relationship rel_id of source_part and, unless another
internal relationship still targets it, the part it names with that
part’s own relationships and content-type override, then the parts
(a header’s images) nothing else targets any more. No-op for an
unknown or external relationship.
Sourcepub fn read_rels_for(&self, part_name: &str) -> Option<&Relationships>
pub fn read_rels_for(&self, part_name: &str) -> Option<&Relationships>
readRelsFor(part) — the relationships of a part, if any.
Sourcepub fn add_document_relationship(
&mut self,
source_part: &str,
rel_type: &str,
target: &str,
) -> String
pub fn add_document_relationship( &mut self, source_part: &str, rel_type: &str, target: &str, ) -> String
addDocumentRelationship(...) — add a relationship to a part, returning
the new relationship id.
Sourcepub fn package_relationships(&self) -> &Relationships
pub fn package_relationships(&self) -> &Relationships
The package’s own relationships (_rels/.rels).
Sourcepub fn add_package_relationship(
&mut self,
rel_type: &str,
target: &str,
) -> String
pub fn add_package_relationship( &mut self, rel_type: &str, target: &str, ) -> String
Add a package-level relationship, returning its id.
Sourcepub fn add_document_relationship_external(
&mut self,
source_part: &str,
rel_type: &str,
target: &str,
) -> String
pub fn add_document_relationship_external( &mut self, source_part: &str, rel_type: &str, target: &str, ) -> String
Add a relationship with TargetMode="External" (absolute-URI targets
are ILLEGAL for the default Internal mode — strict packaging layers
like Word’s reject the package without this).
Sourcepub fn set_rel_target_mode_external(&mut self, source_part: &str, rel_id: &str)
pub fn set_rel_target_mode_external(&mut self, source_part: &str, rel_id: &str)
Mark an existing relationship of source_part as External (test aid).
Sourcepub fn main_document_part(&self) -> Option<String>
pub fn main_document_part(&self) -> Option<String>
The main document part name (docxodus style).