azure_storage_blobs/blob/
block_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
use crate::blob::{BlobBlockType, BlockWithSizeList};
use azure_core::base64;

#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct BlockList {
    pub blocks: Vec<BlobBlockType>,
}

impl From<BlockWithSizeList> for BlockList {
    fn from(b: BlockWithSizeList) -> BlockList {
        let mut bl = BlockList::default();
        for block in b.blocks {
            bl.blocks.push(block.block_list_type);
        }
        bl
    }
}

impl BlockList {
    pub fn to_xml(&self) -> String {
        let mut s = String::new();
        s.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<BlockList>\n");
        for bl in &self.blocks {
            let node = match bl {
                BlobBlockType::Committed(content) => {
                    format!(
                        "\t<Committed>{}</Committed>\n",
                        base64::encode(content.as_ref())
                    )
                }
                BlobBlockType::Uncommitted(content) => format!(
                    "\t<Uncommitted>{}</Uncommitted>\n",
                    base64::encode(content.as_ref())
                ),
                BlobBlockType::Latest(content) => {
                    format!("\t<Latest>{}</Latest>\n", base64::encode(content.as_ref()))
                }
            };

            s.push_str(&node);
        }

        s.push_str("</BlockList>");
        s
    }
}

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

    #[test]
    fn to_xml() {
        let mut blocks = BlockList { blocks: Vec::new() };
        blocks
            .blocks
            .push(BlobBlockType::new_committed(Bytes::from_static(b"numero1")));
        blocks
            .blocks
            .push(BlobBlockType::new_uncommitted("numero2"));
        blocks
            .blocks
            .push(BlobBlockType::new_uncommitted("numero3"));
        blocks.blocks.push(BlobBlockType::new_latest("numero4"));

        let _retu: &str = &blocks.to_xml();

        // to assert with handcrafted XML
    }
}