azure_storage_blobs/blob/
page_range_list.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
use azure_core::{prelude::Range, xml::read_xml_str};

#[derive(Debug, Deserialize)]
struct Start {
    #[serde(rename = "$value")]
    pub value: u64,
}

#[derive(Debug, Deserialize)]
struct End {
    #[serde(rename = "$value")]
    pub value: u64,
}

#[derive(Debug, Deserialize)]
struct PageRange {
    #[serde(rename = "Start")]
    pub start: Start,
    #[serde(rename = "End")]
    pub end: End,
}

#[derive(Debug, Deserialize)]
struct PageList {
    #[serde(rename = "PageRange", default)]
    pub page_list: Vec<PageRange>,
}

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct PageRangeList {
    pub ranges: Vec<Range>,
}

impl PageRangeList {
    pub fn try_from_xml(xml: &str) -> azure_core::Result<Self> {
        let pl: PageList = read_xml_str(xml)?;

        let mut prl = PageRangeList { ranges: Vec::new() };

        for range in pl.page_list {
            prl.ranges
                .push(Range::new(range.start.value, range.end.value));
        }

        Ok(prl)
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn try_parse() {
        let page_list = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
            <PageList>
              <PageRange>
                <Start>0</Start>
                <End>511</End>
              </PageRange>
              <PageRange>
                <Start>1024</Start>
                <End>1535</End>
              </PageRange>
            </PageList>  ";

        let prl = PageRangeList::try_from_xml(page_list).unwrap();
        assert!(prl.ranges.len() == 2);
        assert!(prl.ranges[0] == Range::new(0, 511));
        assert!(prl.ranges[1] == Range::new(1024, 1535));

        let page_list = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PageList></PageList>";
        let prl = PageRangeList::try_from_xml(page_list).unwrap();
        assert!(prl.ranges.is_empty());

        let page_list = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PageList />";
        let prl = PageRangeList::try_from_xml(page_list).unwrap();
        assert!(prl.ranges.is_empty());
    }
}