Skip to main content

Header

Struct Header 

Source
pub struct Header { /* private fields */ }

Implementations§

Source§

impl Header

Source

pub fn new() -> Result<Self>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 4)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/11_aa_custom_stream_callbacks.rs (line 108)
93fn main() -> Result<(), Box<dyn std::error::Error>> {
94    let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
95    let mut stream = ByteStream::custom(SharedByteCallbacks {
96        inner: byte_state,
97    })?;
98    stream.write_all(b"hello custom stream")?;
99    stream.seek(0, 0)?;
100    let mut buffer = vec![0_u8; 19];
101    stream.read(&mut buffer)?;
102    assert_eq!(&buffer, b"hello custom stream");
103
104    let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
105    let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
106        inner: archive_state.clone(),
107    })?;
108    let mut header = Header::new()?;
109    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
110    header.append_field_string(FieldKey::PAT, "custom.txt")?;
111    header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
112    header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
113    writer.write_header(&header)?;
114    writer.write_blob(FieldKey::DAT, &buffer)?;
115
116    let mut reader = ArchiveStream::custom(SharedArchiveCallbacks { inner: archive_state })?;
117    let decoded_header = reader.read_header()?.expect("header");
118    assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
119    let mut decoded = vec![0_u8; buffer.len()];
120    reader.read_blob(FieldKey::DAT, &mut decoded)?;
121    assert_eq!(decoded, buffer);
122
123    println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
124    Ok(())
125}
examples/04_aa_archive_stream_roundtrip.rs (line 15)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn from_encoded_data(data: &[u8]) -> Result<Self>

Examples found in repository?
examples/11_aa_custom_stream_callbacks.rs (line 82)
78    fn read_header(&mut self) -> compression::Result<Option<Header>> {
79        let Some(header) = self.inner.borrow_mut().headers.pop_front() else {
80            return Ok(None);
81        };
82        Ok(Some(Header::from_encoded_data(&header)?))
83    }
More examples
Hide additional examples
examples/08_aa_header_roundtrip.rs (line 18)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
Source

pub fn from_path( key_set: &FieldKeySet, dir: &str, path: &str, flags: ArchiveFlags, ) -> Result<Self>

Source

pub fn assign(&mut self, other: &Self) -> Result<()>

Source

pub fn field_count(&self) -> u32

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 32)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
Source

pub fn is_empty(&self) -> bool

Source

pub fn key_index(&self, key: FieldKey) -> Result<Option<u32>>

Source

pub fn field_type(&self, index: u32) -> Result<FieldType>

Source

pub fn field_key(&self, index: u32) -> Result<FieldKey>

Source

pub fn payload_size(&self) -> u64

Source

pub fn remove_field(&mut self, index: u32) -> Result<()>

Source

pub fn clear(&mut self) -> Result<()>

Source

pub fn set_field_flag(&mut self, index: u32, key: FieldKey) -> Result<()>

Source

pub fn set_field_uint( &mut self, index: u32, key: FieldKey, value: u64, ) -> Result<()>

Source

pub fn set_field_string( &mut self, index: u32, key: FieldKey, value: &str, ) -> Result<()>

Source

pub fn set_field_hash( &mut self, index: u32, key: FieldKey, function: HashFunction, value: &[u8], ) -> Result<()>

Source

pub fn set_field_timespec( &mut self, index: u32, key: FieldKey, value: Timespec, ) -> Result<()>

Source

pub fn set_field_blob( &mut self, index: u32, key: FieldKey, size: u64, ) -> Result<()>

Source

pub fn append_field_flag(&mut self, key: FieldKey) -> Result<()>

Source

pub fn append_field_uint(&mut self, key: FieldKey, value: u64) -> Result<()>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 5)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/11_aa_custom_stream_callbacks.rs (line 109)
93fn main() -> Result<(), Box<dyn std::error::Error>> {
94    let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
95    let mut stream = ByteStream::custom(SharedByteCallbacks {
96        inner: byte_state,
97    })?;
98    stream.write_all(b"hello custom stream")?;
99    stream.seek(0, 0)?;
100    let mut buffer = vec![0_u8; 19];
101    stream.read(&mut buffer)?;
102    assert_eq!(&buffer, b"hello custom stream");
103
104    let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
105    let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
106        inner: archive_state.clone(),
107    })?;
108    let mut header = Header::new()?;
109    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
110    header.append_field_string(FieldKey::PAT, "custom.txt")?;
111    header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
112    header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
113    writer.write_header(&header)?;
114    writer.write_blob(FieldKey::DAT, &buffer)?;
115
116    let mut reader = ArchiveStream::custom(SharedArchiveCallbacks { inner: archive_state })?;
117    let decoded_header = reader.read_header()?.expect("header");
118    assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
119    let mut decoded = vec![0_u8; buffer.len()];
120    reader.read_blob(FieldKey::DAT, &mut decoded)?;
121    assert_eq!(decoded, buffer);
122
123    println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
124    Ok(())
125}
examples/04_aa_archive_stream_roundtrip.rs (line 18)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn append_field_string(&mut self, key: FieldKey, value: &str) -> Result<()>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 6)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/11_aa_custom_stream_callbacks.rs (line 110)
93fn main() -> Result<(), Box<dyn std::error::Error>> {
94    let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
95    let mut stream = ByteStream::custom(SharedByteCallbacks {
96        inner: byte_state,
97    })?;
98    stream.write_all(b"hello custom stream")?;
99    stream.seek(0, 0)?;
100    let mut buffer = vec![0_u8; 19];
101    stream.read(&mut buffer)?;
102    assert_eq!(&buffer, b"hello custom stream");
103
104    let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
105    let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
106        inner: archive_state.clone(),
107    })?;
108    let mut header = Header::new()?;
109    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
110    header.append_field_string(FieldKey::PAT, "custom.txt")?;
111    header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
112    header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
113    writer.write_header(&header)?;
114    writer.write_blob(FieldKey::DAT, &buffer)?;
115
116    let mut reader = ArchiveStream::custom(SharedArchiveCallbacks { inner: archive_state })?;
117    let decoded_header = reader.read_header()?.expect("header");
118    assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
119    let mut decoded = vec![0_u8; buffer.len()];
120    reader.read_blob(FieldKey::DAT, &mut decoded)?;
121    assert_eq!(decoded, buffer);
122
123    println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
124    Ok(())
125}
examples/04_aa_archive_stream_roundtrip.rs (line 19)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn append_field_hash( &mut self, key: FieldKey, function: HashFunction, value: &[u8], ) -> Result<()>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 7)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
Source

pub fn append_field_timespec( &mut self, key: FieldKey, value: Timespec, ) -> Result<()>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (lines 8-14)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
Source

pub fn append_field_blob(&mut self, key: FieldKey, size: u64) -> Result<()>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 15)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/11_aa_custom_stream_callbacks.rs (line 112)
93fn main() -> Result<(), Box<dyn std::error::Error>> {
94    let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
95    let mut stream = ByteStream::custom(SharedByteCallbacks {
96        inner: byte_state,
97    })?;
98    stream.write_all(b"hello custom stream")?;
99    stream.seek(0, 0)?;
100    let mut buffer = vec![0_u8; 19];
101    stream.read(&mut buffer)?;
102    assert_eq!(&buffer, b"hello custom stream");
103
104    let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
105    let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
106        inner: archive_state.clone(),
107    })?;
108    let mut header = Header::new()?;
109    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
110    header.append_field_string(FieldKey::PAT, "custom.txt")?;
111    header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
112    header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
113    writer.write_header(&header)?;
114    writer.write_blob(FieldKey::DAT, &buffer)?;
115
116    let mut reader = ArchiveStream::custom(SharedArchiveCallbacks { inner: archive_state })?;
117    let decoded_header = reader.read_header()?.expect("header");
118    assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
119    let mut decoded = vec![0_u8; buffer.len()];
120    reader.read_blob(FieldKey::DAT, &mut decoded)?;
121    assert_eq!(decoded, buffer);
122
123    println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
124    Ok(())
125}
examples/04_aa_archive_stream_roundtrip.rs (line 21)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn field_uint(&self, index: u32) -> Result<u64>

Source

pub fn field_string(&self, index: u32) -> Result<String>

Source

pub fn field_hash(&self, index: u32) -> Result<HashValue>

Source

pub fn field_timespec(&self, index: u32) -> Result<Timespec>

Source

pub fn field_blob(&self, index: u32) -> Result<BlobDescription>

Source

pub fn field_value(&self, index: u32) -> Result<HeaderFieldValue>

Source

pub fn uint_with_key(&self, key: FieldKey) -> Result<Option<u64>>

Source

pub fn string_with_key(&self, key: FieldKey) -> Result<Option<String>>

Source

pub fn hash_with_key(&self, key: FieldKey) -> Result<Option<HashValue>>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 22)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
Source

pub fn timespec_with_key(&self, key: FieldKey) -> Result<Option<Timespec>>

Source

pub fn blob_with_key(&self, key: FieldKey) -> Result<Option<BlobDescription>>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 26)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/04_aa_archive_stream_roundtrip.rs (line 37)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn value_with_key(&self, key: FieldKey) -> Result<Option<HeaderFieldValue>>

Source

pub fn entry_type(&self) -> Result<Option<EntryType>>

Source

pub fn path(&self) -> Result<Option<String>>

Examples found in repository?
examples/08_aa_header_roundtrip.rs (line 20)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}
More examples
Hide additional examples
examples/11_aa_custom_stream_callbacks.rs (line 118)
93fn main() -> Result<(), Box<dyn std::error::Error>> {
94    let byte_state = Rc::new(RefCell::new(MemoryByteState::default()));
95    let mut stream = ByteStream::custom(SharedByteCallbacks {
96        inner: byte_state,
97    })?;
98    stream.write_all(b"hello custom stream")?;
99    stream.seek(0, 0)?;
100    let mut buffer = vec![0_u8; 19];
101    stream.read(&mut buffer)?;
102    assert_eq!(&buffer, b"hello custom stream");
103
104    let archive_state = Rc::new(RefCell::new(MemoryArchiveState::default()));
105    let mut writer = ArchiveStream::custom(SharedArchiveCallbacks {
106        inner: archive_state.clone(),
107    })?;
108    let mut header = Header::new()?;
109    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
110    header.append_field_string(FieldKey::PAT, "custom.txt")?;
111    header.append_field_uint(FieldKey::SIZ, buffer.len() as u64)?;
112    header.append_field_blob(FieldKey::DAT, buffer.len() as u64)?;
113    writer.write_header(&header)?;
114    writer.write_blob(FieldKey::DAT, &buffer)?;
115
116    let mut reader = ArchiveStream::custom(SharedArchiveCallbacks { inner: archive_state })?;
117    let decoded_header = reader.read_header()?.expect("header");
118    assert_eq!(decoded_header.path()?.as_deref(), Some("custom.txt"));
119    let mut decoded = vec![0_u8; buffer.len()];
120    reader.read_blob(FieldKey::DAT, &mut decoded)?;
121    assert_eq!(decoded, buffer);
122
123    println!("✅ Custom AppleArchive byte/archive stream callbacks OK");
124    Ok(())
125}
examples/04_aa_archive_stream_roundtrip.rs (line 36)
9fn main() -> Result<(), Box<dyn std::error::Error>> {
10    let data = b"hello from apple archive".to_vec();
11    let artifact_dir = artifact_dir("aa-archive-stream");
12    let archive_path = artifact_dir.join("sample.aar");
13    let archive_path = path_string(&archive_path);
14
15    let mut header = Header::new()?;
16    let regular_file = u64::from(b'F');
17    let data_len = u64::try_from(data.len())?;
18    header.append_field_uint(FieldKey::TYP, regular_file)?;
19    header.append_field_string(FieldKey::PAT, "greeting.txt")?;
20    header.append_field_uint(FieldKey::SIZ, data_len)?;
21    header.append_field_blob(FieldKey::DAT, data_len)?;
22
23    let byte_stream = ByteStream::open_with_path(
24        &archive_path,
25        OPEN_WRITE_ONLY | OPEN_CREATE | OPEN_TRUNCATE,
26        DEFAULT_FILE_MODE,
27    )?;
28    let mut archive = ArchiveStream::encode_output(byte_stream, ArchiveFlags::empty(), 0)?;
29    archive.write_header(&header)?;
30    archive.write_blob(FieldKey::DAT, &data)?;
31    archive.close()?;
32
33    let byte_stream = ByteStream::open_with_path(&archive_path, OPEN_READ_ONLY, 0)?;
34    let mut archive = ArchiveStream::decode_input(byte_stream, ArchiveFlags::empty(), 0)?;
35    let header = archive.read_header()?.expect("archive entry");
36    assert_eq!(header.path()?.as_deref(), Some("greeting.txt"));
37    let blob = header.blob_with_key(FieldKey::DAT)?.expect("blob field");
38    let mut decoded = vec![0_u8; usize::try_from(blob.size)?];
39    archive.read_blob(FieldKey::DAT, &mut decoded)?;
40    assert_eq!(decoded, data);
41    assert!(archive.read_header()?.is_none());
42    archive.close()?;
43
44    println!("archive path={archive_path}");
45    println!("✅ AppleArchive round-trip OK");
46    Ok(())
47}
Source

pub fn encoded_size(&self) -> usize

Source

pub fn encoded_data(&self) -> Result<Vec<u8>>

Examples found in repository?
examples/11_aa_custom_stream_callbacks.rs (line 69)
68    fn write_header(&mut self, header: &Header) -> compression::Result<()> {
69        self.inner.borrow_mut().headers.push_back(header.encoded_data()?);
70        Ok(())
71    }
More examples
Hide additional examples
examples/08_aa_header_roundtrip.rs (line 17)
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let mut header = Header::new()?;
5    header.append_field_uint(FieldKey::TYP, u64::from(b'F'))?;
6    header.append_field_string(FieldKey::PAT, "notes.txt")?;
7    header.append_field_hash(FieldKey::SH2, HashFunction::Sha256, &[7_u8; 32])?;
8    header.append_field_timespec(
9        FieldKey::MTM,
10        Timespec {
11            seconds: 1_234,
12            nanoseconds: 56,
13        },
14    )?;
15    header.append_field_blob(FieldKey::DAT, 512)?;
16
17    let encoded = header.encoded_data()?;
18    let decoded = Header::from_encoded_data(&encoded)?;
19
20    assert_eq!(decoded.path()?.as_deref(), Some("notes.txt"));
21    assert_eq!(
22        decoded.hash_with_key(FieldKey::SH2)?.expect("hash").bytes,
23        vec![7_u8; 32]
24    );
25    assert_eq!(
26        decoded.blob_with_key(FieldKey::DAT)?.expect("blob"),
27        BlobDescription {
28            size: 512,
29            offset: 0
30        }
31    );
32    let field_count = decoded.field_count();
33    println!("fields={field_count}");
34    println!("✅ AppleArchive header encode/decode OK");
35    Ok(())
36}

Trait Implementations§

Source§

impl Clone for Header

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Header

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Header

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

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<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.