pub struct Header { /* private fields */ }Implementations§
Source§impl Header
impl Header
Sourcepub fn new() -> Result<Self>
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
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}Sourcepub fn from_encoded_data(data: &[u8]) -> Result<Self>
pub fn from_encoded_data(data: &[u8]) -> Result<Self>
Examples found in repository?
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}pub fn from_path( key_set: &FieldKeySet, dir: &str, path: &str, flags: ArchiveFlags, ) -> Result<Self>
pub fn assign(&mut self, other: &Self) -> Result<()>
Sourcepub fn field_count(&self) -> u32
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}pub fn is_empty(&self) -> bool
pub fn key_index(&self, key: FieldKey) -> Result<Option<u32>>
pub fn field_type(&self, index: u32) -> Result<FieldType>
pub fn field_key(&self, index: u32) -> Result<FieldKey>
pub fn payload_size(&self) -> u64
pub fn remove_field(&mut self, index: u32) -> Result<()>
pub fn clear(&mut self) -> Result<()>
pub fn set_field_flag(&mut self, index: u32, key: FieldKey) -> Result<()>
pub fn set_field_uint( &mut self, index: u32, key: FieldKey, value: u64, ) -> Result<()>
pub fn set_field_string( &mut self, index: u32, key: FieldKey, value: &str, ) -> Result<()>
pub fn set_field_hash( &mut self, index: u32, key: FieldKey, function: HashFunction, value: &[u8], ) -> Result<()>
pub fn set_field_timespec( &mut self, index: u32, key: FieldKey, value: Timespec, ) -> Result<()>
pub fn set_field_blob( &mut self, index: u32, key: FieldKey, size: u64, ) -> Result<()>
pub fn append_field_flag(&mut self, key: FieldKey) -> Result<()>
Sourcepub fn append_field_uint(&mut self, key: FieldKey, value: u64) -> Result<()>
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
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}Sourcepub fn append_field_string(&mut self, key: FieldKey, value: &str) -> Result<()>
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
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}Sourcepub fn append_field_hash(
&mut self,
key: FieldKey,
function: HashFunction,
value: &[u8],
) -> Result<()>
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}Sourcepub fn append_field_timespec(
&mut self,
key: FieldKey,
value: Timespec,
) -> Result<()>
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}Sourcepub fn append_field_blob(&mut self, key: FieldKey, size: u64) -> Result<()>
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
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}pub fn field_uint(&self, index: u32) -> Result<u64>
pub fn field_string(&self, index: u32) -> Result<String>
pub fn field_hash(&self, index: u32) -> Result<HashValue>
pub fn field_timespec(&self, index: u32) -> Result<Timespec>
pub fn field_blob(&self, index: u32) -> Result<BlobDescription>
pub fn field_value(&self, index: u32) -> Result<HeaderFieldValue>
pub fn uint_with_key(&self, key: FieldKey) -> Result<Option<u64>>
pub fn string_with_key(&self, key: FieldKey) -> Result<Option<String>>
Sourcepub fn hash_with_key(&self, key: FieldKey) -> Result<Option<HashValue>>
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}pub fn timespec_with_key(&self, key: FieldKey) -> Result<Option<Timespec>>
Sourcepub fn blob_with_key(&self, key: FieldKey) -> Result<Option<BlobDescription>>
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
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}pub fn value_with_key(&self, key: FieldKey) -> Result<Option<HeaderFieldValue>>
pub fn entry_type(&self) -> Result<Option<EntryType>>
Sourcepub fn path(&self) -> Result<Option<String>>
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
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}pub fn encoded_size(&self) -> usize
Sourcepub fn encoded_data(&self) -> Result<Vec<u8>>
pub fn encoded_data(&self) -> Result<Vec<u8>>
Examples found in repository?
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§
Auto Trait Implementations§
impl Freeze for Header
impl RefUnwindSafe for Header
impl !Send for Header
impl !Sync for Header
impl Unpin for Header
impl UnsafeUnpin for Header
impl UnwindSafe for Header
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more