Skip to main content

PartFs

Struct PartFs 

Source
pub struct PartFs { /* private fields */ }
Expand description

Thin adapter over oxml_opc::OpcPackage. Port-equivalent of PartFS.

Implementations§

Source§

impl PartFs

Source

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).

Source

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.

Source

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).

Source

pub fn part_string(&self, name: &str) -> Option<String>

Read a part as a UTF-8 string.

Source

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.

Source

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.

Source

pub fn parts(&self) -> Vec<String>

Enumerate all part names (docxodus style, no leading slash), sorted.

Source

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.

Source

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.

Source

pub fn content_type_for(&self, name: &str) -> Option<String>

contentTypeFor(part).

Source

pub fn add_content_type_override(&mut self, part_name: &str, content_type: &str)

Add an Override entry to [Content_Types].xml.

Source

pub fn add_content_type_default(&mut self, ext: &str, content_type: &str)

Add a Default extension mapping to [Content_Types].xml.

Source

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.

Source

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).

Source

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).

Source

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.

Source

pub fn read_rels_for(&self, part_name: &str) -> Option<&Relationships>

readRelsFor(part) — the relationships of a part, if any.

Source

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.

Source

pub fn package_relationships(&self) -> &Relationships

The package’s own relationships (_rels/.rels).

Source

pub fn add_package_relationship( &mut self, rel_type: &str, target: &str, ) -> String

Add a package-level relationship, returning its id.

Source

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).

Source

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).

Source

pub fn main_document_part(&self) -> Option<String>

The main document part name (docxodus style).

Auto Trait Implementations§

§

impl Forget for PartFs

§

impl Freeze for PartFs

§

impl Move for PartFs

§

impl RefUnwindSafe for PartFs

§

impl Send for PartFs

§

impl Sync for PartFs

§

impl Unpin for PartFs

§

impl UnsafeUnpin for PartFs

§

impl UnwindSafe for PartFs

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.