apple_dmg/
xml.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6use {
7    crate::blkx::BlkxTable,
8    anyhow::Result,
9    serde::{Deserialize, Serialize},
10};
11
12#[derive(Clone, Debug, Default, Deserialize, Serialize)]
13#[serde(deny_unknown_fields)]
14pub struct Plist {
15    #[serde(rename = "resource-fork")]
16    pub resource_fork: ResourceFork,
17}
18
19impl Plist {
20    pub fn partitions(&self) -> &[Partition] {
21        &self.resource_fork.blkx
22    }
23
24    pub fn add_partition(&mut self, partition: Partition) {
25        self.resource_fork.blkx.push(partition);
26    }
27}
28
29#[derive(Clone, Debug, Default, Deserialize, Serialize)]
30#[serde(deny_unknown_fields)]
31pub struct ResourceFork {
32    pub blkx: Vec<Partition>,
33    #[serde(default)]
34    pub plst: Vec<Partition>,
35}
36
37#[derive(Clone, Debug, Deserialize, Serialize)]
38#[serde(deny_unknown_fields)]
39pub struct Partition {
40    #[serde(rename = "Attributes")]
41    pub attributes: String,
42    #[serde(rename = "CFName")]
43    #[serde(default)]
44    pub cfname: String,
45    #[serde(rename = "Data")]
46    #[serde(with = "serde_bytes")]
47    pub data: Vec<u8>,
48    #[serde(rename = "ID")]
49    pub id: String,
50    #[serde(rename = "Name")]
51    pub name: String,
52}
53
54impl Partition {
55    pub fn new(id: i32, name: String, table: BlkxTable) -> Self {
56        let mut data = vec![];
57        table.write_to(&mut data).unwrap();
58        Self {
59            attributes: "0x0050".to_string(),
60            cfname: name.clone(),
61            data,
62            id: id.to_string(),
63            name,
64        }
65    }
66
67    pub fn table(&self) -> Result<BlkxTable> {
68        BlkxTable::read_from(&mut &self.data[..])
69    }
70}