pub struct Package { /* private fields */ }Expand description
An OpenDocument package: a zip archive whose first entry declares the media
type of everything else in it.
Entries are kept in the order they were read, and every entry is kept whether or not anything here understands it — a digital signature, a thumbnail, an embedded object, a part from a future version of the format.
Implementations§
Source§impl Package
impl Package
Sourcepub fn read(bytes: &[u8]) -> Result<Self, Error>
pub fn read(bytes: &[u8]) -> Result<Self, Error>
Read a package from its bytes.
§Errors
The bytes are not a zip archive, or an entry cannot be decompressed.
Sourcepub fn media_type(&self) -> Option<&str>
pub fn media_type(&self) -> Option<&str>
The media type the package declares.
None for a package that declares none, which is legal: the
specification makes the mimetype entry optional, and a package written
without one is identified by its file extension alone.
Sourcepub fn optional_xml(&self, name: &'static str) -> Result<Option<Element>, Error>
pub fn optional_xml(&self, name: &'static str) -> Result<Option<Element>, Error>
Parse an entry as XML, or give back an empty document if it is absent.
styles.xml, meta.xml and settings.xml are each optional in a
package that has nothing to put in them, and a document with no metadata
is not a document that should fail to open.
§Errors
The entry is present and is not well-formed XML.
Sourcepub fn set_part(&mut self, name: &str, data: Vec<u8>)
pub fn set_part(&mut self, name: &str, data: Vec<u8>)
Replace an entry’s bytes, adding it at the end if it was not there.
Sourcepub fn set_xml(&mut self, name: &str, root: &Element)
pub fn set_xml(&mut self, name: &str, root: &Element)
Replace an entry with a serialized XML tree.
Sourcepub fn write(&self) -> Result<Vec<u8>, Error>
pub fn write(&self) -> Result<Vec<u8>, Error>
Write the package back out.
The mimetype entry goes first and uncompressed whatever order it was
read in, because that is the one ordering rule the format has and a
package that breaks it is identified by its extension alone.
§Errors
The underlying writer failed, which for a buffer in memory means the allocation did.
Sourcepub fn expect_media_type(
&self,
wanted: &'static [&'static str],
) -> Result<(), Error>
pub fn expect_media_type( &self, wanted: &'static [&'static str], ) -> Result<(), Error>
Check the package declares one of the media types this reader opens.
A format may have more than one: a Writer/Web document is a text document and declares a media type of its own.
§Errors
The package declares a media type that is none of them. A package that declares none is accepted: the caller asked for a format, and the file extension is the only other thing that ever said.