pub struct Reader { /* private fields */ }Expand description
An open ASDF file.
Implementations§
Source§impl Reader
impl Reader
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Open and scan a file from disk.
The file is memory-mapped, so block data is not read until it is used.
Sourcepub fn from_bytes(bytes: Vec<u8>) -> Result<Self>
pub fn from_bytes(bytes: Vec<u8>) -> Result<Self>
Scan an in-memory file.
Sourcepub fn tree(&self) -> Result<Option<Document>>
pub fn tree(&self) -> Result<Option<Document>>
Parse the YAML tree.
A file with no tree – legitimate in exploded form – yields None.
Sourcepub fn block_count(&self) -> usize
pub fn block_count(&self) -> usize
The number of binary blocks.
Sourcepub fn block(&self, index: usize) -> Result<&BlockLocation>
pub fn block(&self, index: usize) -> Result<&BlockLocation>
A block’s location and header.
Sourcepub fn block_raw(&self, index: usize) -> Result<&[u8]>
pub fn block_raw(&self, index: usize) -> Result<&[u8]>
A block’s bytes exactly as stored, without decompressing.
For an uncompressed block this is the data itself; for a compressed one it is the compressed form.
Sourcepub fn block_compression(&self, index: usize) -> Result<Compression>
pub fn block_compression(&self, index: usize) -> Result<Compression>
The compression method a block uses.
Sourcepub fn block_data(&self, index: usize) -> Result<Cow<'_, [u8]>>
pub fn block_data(&self, index: usize) -> Result<Cow<'_, [u8]>>
A block’s data, decompressed if necessary.
An uncompressed block borrows straight from the file with no copy.
Sourcepub fn verify_block_checksum(
&self,
index: usize,
) -> Result<(ChecksumStatus, [u8; 16])>
pub fn verify_block_checksum( &self, index: usize, ) -> Result<(ChecksumStatus, [u8; 16])>
Verify a block’s MD5 checksum.
The returned digest is what the data actually hashes to, which is useful for reporting a mismatch.
§The Python asdf compatibility case
For a compressed block, the specification means the checksum to
cover the bytes as stored. Python asdf 5.x and earlier instead
checksum the uncompressed data
(asdf#2015).
libasdf works around this by consulting the file’s asdf_library
metadata, and so do we: see Reader::has_python_checksum_bug. A
compressed block whose stored bytes do not match is therefore retried
against the decompressed bytes when the writer is known to be affected.
Sourcepub fn has_python_checksum_bug(&self) -> bool
pub fn has_python_checksum_bug(&self) -> bool
Whether this file was written by a Python asdf version that checksums compressed blocks incorrectly.
Matches libasdf’s test: the asdf_library name is asdf and its
major version is 5 or below.
Source§impl Reader
impl Reader
Sourcepub fn external_block(&self, uri: &str) -> Result<Vec<u8>>
pub fn external_block(&self, uri: &str) -> Result<Vec<u8>>
Resolve an external array source and read the data it names.
The standard makes source a URI relative to the file’s own, and
exploded form writes one array per file with the data in block 0.
Resolution is deliberately narrow. The URI must be a relative path
with no .. component and no scheme, so a file can only reach others
beneath its own directory: a tree is untrusted input, and following an
arbitrary path out of it would let a crafted file name anything on the
machine. A file read from memory has no directory to resolve against
and so resolves nothing.
Sourcepub fn tree_inlined(&self) -> Result<Option<(Document, Vec<String>)>>
pub fn tree_inlined(&self) -> Result<Option<(Document, Vec<String>)>>
Parse the tree with every block-backed core/ndarray replaced by its
data inline.
This is the transformation the ASDF Standard’s reference corpus asks
for before comparing a file against its expected YAML. An array whose
data lives in another file – exploded form’s external source – is
resolved through Reader::external_block, which needs this file to
have been read from disk.
Returns the transformed tree and the paths of any arrays that could not be inlined.