mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 15:55:14 +00:00
psd: add Descriptor::parse_prefix for mid-stream reads (#1887)
from_bytes still requires the slice to be fully consumed. parse_prefix returns the descriptor and the number of bytes used, matching VersionedDescriptor, so apps can read a descriptor that is followed by more data (layer effects, fill and adjustment blocks). Part of #778 and effectcraft#786.
This commit is contained in:
1 parent
43fbfaaa3b
commit
9fc471fc5c
1 file changed
+21
@@ -300,6 +300,15 @@ impl Descriptor {
|
||||
Ok(d)
|
||||
}
|
||||
|
||||
/// Parses a descriptor at the start of `data`, returning it and the number of bytes consumed
|
||||
/// (trailing data is allowed). Mid-stream reads of layer effects, fill and adjustment
|
||||
/// descriptors use this; [`from_bytes`] still requires the slice to be fully consumed.
|
||||
pub fn parse_prefix(data: &[u8]) -> Result<(Self, usize)> {
|
||||
let mut r = Reader::new(data);
|
||||
let d = Self::read(&mut r)?;
|
||||
Ok((d, r.pos()))
|
||||
}
|
||||
|
||||
/// Serializes the descriptor.
|
||||
pub fn to_bytes(&self) -> Vec<u8> {
|
||||
let mut v = Vec::new();
|
||||
@@ -705,6 +714,18 @@ mod tests {
|
||||
assert_eq!(VersionedDescriptor::from_bytes(&b).unwrap(), v);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_prefix() {
|
||||
let d = sample();
|
||||
let mut b = d.to_bytes();
|
||||
let n = b.len();
|
||||
b.extend_from_slice(&[1, 2, 3]);
|
||||
let (p, used) = Descriptor::parse_prefix(&b).unwrap();
|
||||
assert_eq!(p, d);
|
||||
assert_eq!(used, n);
|
||||
assert!(Descriptor::from_bytes(&b).is_err(), "from_bytes still refuses trailing bytes");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn versioned_parse_prefix() {
|
||||
let v = VersionedDescriptor::new(sample());
|
||||
|
||||
Reference in new issue
Block a user