Skip to main content

08_aa_header_roundtrip/
08_aa_header_roundtrip.rs

1use compression::{BlobDescription, FieldKey, HashFunction, Header, Timespec};
2
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}