1- use azure:: core:: errors:: BlockListParseError ;
2- use azure:: storage:: blob:: BlobBlockType ;
1+ use azure:: storage:: blob:: { BlobBlockType , BlockWithSizeList } ;
32use std:: borrow:: Borrow ;
4- use std:: convert:: TryFrom ;
53
64#[ derive( Default , Debug , Clone , PartialEq ) ]
75pub struct BlockList < T >
119 pub blocks : Vec < BlobBlockType < T > > ,
1210}
1311
14- impl < ' a > TryFrom < & ' a str > for BlockList < & ' a str > {
15- type Error = BlockListParseError ;
16-
17- fn try_from ( xml : & ' a str ) -> Result < Self , Self :: Error > {
18- // this is terrible XML parsing but will do temporarily.
19- // at least we are not copying strings around.
20- // we assume here the XML is composed by
21- // single byte chars. It should (base64 encoding should
22- // comply) but if we get unpleasant errors
23- // this can be a place to start looking
24- trace ! ( "BlockList::try_from called with xml == \" {}\" " , xml) ;
25-
26- let mut bl = BlockList { blocks : Vec :: new ( ) } ;
27-
28- let begin = xml[ ..] . find ( "<BlockList>" ) ? + "<BlockList>" . len ( ) ;
29- let end = xml[ begin..] . find ( "</BlockList>" ) ? + begin;
30-
31- debug ! ( "begin == {}, end == {}" , begin, end) ;
32-
33- let mut cur = begin;
34-
35- while cur < end {
36- debug ! ( "cur == {}" , cur) ;
37-
38- let tagbegin = xml[ cur..] . find ( '<' ) ? + cur + 1 ;
39- let tagend = xml[ cur..] . find ( '>' ) ? + cur;
40-
41- debug ! ( "tagbegin == {}, tagend == {}" , tagbegin, tagend) ;
42- let node_type = & xml[ tagbegin..tagend] ;
43-
44- debug ! ( "{}" , node_type) ;
45- if node_type == "/BlockList" {
46- break ;
47- }
48-
49- cur = tagend + 1 ;
50-
51- let close_tag = format ! ( "</{}>" , node_type) ;
52- let close_pos = xml[ cur..] . find ( & close_tag) ? + cur;
53-
54- let id = & xml[ cur..close_pos] ;
55- debug ! ( "id == {}" , id) ;
56-
57- cur = close_pos + close_tag. len ( ) + 1 ;
58-
59- bl. blocks . push ( match node_type {
60- "Committed" => BlobBlockType :: Committed ( id) ,
61- "Uncommitted" => BlobBlockType :: Uncommitted ( id) ,
62- "Latest" => BlobBlockType :: Latest ( id) ,
63- _ => {
64- return Err ( BlockListParseError :: InvalidBlockType {
65- name : node_type. to_owned ( ) ,
66- } )
67- }
68- } ) ;
69- }
70-
71- Ok ( bl)
72- }
73- }
74-
7512impl < ' a > BlockList < & ' a str > {
7613 pub fn to_owned ( & self ) -> BlockList < String > {
7714 let mut bl: BlockList < String > = BlockList {
@@ -90,6 +27,19 @@ impl<'a> BlockList<&'a str> {
9027 }
9128}
9229
30+ impl < T > From < BlockWithSizeList < T > > for BlockList < T >
31+ where
32+ T : Borrow < str > + Default ,
33+ {
34+ fn from ( b : BlockWithSizeList < T > ) -> BlockList < T > {
35+ let mut bl = BlockList :: default ( ) ;
36+ for block in b. blocks {
37+ bl. blocks . push ( block. block_list_type ) ;
38+ }
39+ bl
40+ }
41+ }
42+
9343impl < T > BlockList < T >
9444where
9545 T : Borrow < str > ,
@@ -123,42 +73,16 @@ mod test {
12373 use super :: * ;
12474
12575 #[ test]
126- fn try_parse ( ) {
127- let range = "<?xml version=\" 1.0\" encoding=\" utf-8\" ?>
128- <BlockList>
129- <Committed>numero1</Committed>
130- <Uncommitted>numero2</Uncommitted>
131- <Uncommitted>numero3</Uncommitted>
132- <Latest>numero4</Latest>
133- </BlockList>" ;
134-
135- let bl = BlockList :: try_from ( range) . unwrap ( ) ;
136- assert ! ( bl. blocks. len( ) == 4 ) ;
137- assert ! ( bl. blocks[ 0 ] == BlobBlockType :: Committed ( "numero1" ) ) ;
138- assert ! ( bl. blocks[ 1 ] == BlobBlockType :: Uncommitted ( "numero2" ) ) ;
139- assert ! ( bl. blocks[ 2 ] == BlobBlockType :: Uncommitted ( "numero3" ) ) ;
140- assert ! ( bl. blocks[ 3 ] == BlobBlockType :: Latest ( "numero4" ) ) ;
141- }
142-
143- #[ test]
144- fn to_xml_and_then_parse ( ) {
76+ fn to_xml ( ) {
14577 let mut blocks = BlockList { blocks : Vec :: new ( ) } ;
14678 blocks. blocks . push ( BlobBlockType :: Committed ( "numero1" ) ) ;
14779 blocks. blocks . push ( BlobBlockType :: Uncommitted ( "numero2" ) ) ;
14880 blocks. blocks . push ( BlobBlockType :: Uncommitted ( "numero3" ) ) ;
14981 blocks. blocks . push ( BlobBlockType :: Latest ( "numero4" ) ) ;
15082
151- let retu: & str = & blocks. to_xml ( ) ;
152-
153- let bl2 = BlockList :: try_from ( retu) . unwrap ( ) ;
154- assert ! ( bl2. blocks. len( ) == 4 ) ;
155- assert ! ( blocks == bl2) ;
83+ let _retu: & str = & blocks. to_xml ( ) ;
15684
157- let bl_owned = bl2. to_owned ( ) ;
158- assert ! ( bl_owned. blocks[ 0 ] == BlobBlockType :: Committed ( String :: from( "numero1" ) ) ) ;
159- assert ! ( bl_owned. blocks[ 1 ] == BlobBlockType :: Uncommitted ( String :: from( "numero2" ) ) ) ;
160- assert ! ( bl_owned. blocks[ 2 ] == BlobBlockType :: Uncommitted ( String :: from( "numero3" ) ) ) ;
161- assert ! ( bl_owned. blocks[ 3 ] == BlobBlockType :: Latest ( String :: from( "numero4" ) ) ) ;
85+ // to assert with handcrafted XML
16286 }
16387
16488}
0 commit comments