azure_storage_blobs/blob/
block_list.rs

1use crate::blob::{BlobBlockType, BlockWithSizeList};
2use azure_core::base64;
3
4#[derive(Default, Debug, Clone, PartialEq, Eq)]
5pub struct BlockList {
6    pub blocks: Vec<BlobBlockType>,
7}
8
9impl From<BlockWithSizeList> for BlockList {
10    fn from(b: BlockWithSizeList) -> BlockList {
11        let mut bl = BlockList::default();
12        for block in b.blocks {
13            bl.blocks.push(block.block_list_type);
14        }
15        bl
16    }
17}
18
19impl BlockList {
20    pub fn to_xml(&self) -> String {
21        let mut s = String::new();
22        s.push_str("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<BlockList>\n");
23        for bl in &self.blocks {
24            let node = match bl {
25                BlobBlockType::Committed(content) => {
26                    format!(
27                        "\t<Committed>{}</Committed>\n",
28                        base64::encode(content.as_ref())
29                    )
30                }
31                BlobBlockType::Uncommitted(content) => format!(
32                    "\t<Uncommitted>{}</Uncommitted>\n",
33                    base64::encode(content.as_ref())
34                ),
35                BlobBlockType::Latest(content) => {
36                    format!("\t<Latest>{}</Latest>\n", base64::encode(content.as_ref()))
37                }
38            };
39
40            s.push_str(&node);
41        }
42
43        s.push_str("</BlockList>");
44        s
45    }
46}
47
48#[cfg(test)]
49mod test {
50    use super::*;
51    use bytes::Bytes;
52
53    #[test]
54    fn to_xml() {
55        let mut blocks = BlockList { blocks: Vec::new() };
56        blocks
57            .blocks
58            .push(BlobBlockType::new_committed(Bytes::from_static(b"numero1")));
59        blocks
60            .blocks
61            .push(BlobBlockType::new_uncommitted("numero2"));
62        blocks
63            .blocks
64            .push(BlobBlockType::new_uncommitted("numero3"));
65        blocks.blocks.push(BlobBlockType::new_latest("numero4"));
66
67        let _retu: &str = &blocks.to_xml();
68
69        // to assert with handcrafted XML
70    }
71}