1
- use azure:: core:: errors:: BlockListParseError ;
2
- use azure:: storage:: blob:: BlobBlockType ;
1
+ use azure:: storage:: blob:: { BlobBlockType , BlockWithSizeList } ;
3
2
use std:: borrow:: Borrow ;
4
- use std:: convert:: TryFrom ;
5
3
6
4
#[ derive( Default , Debug , Clone , PartialEq ) ]
7
5
pub struct BlockList < T >
11
9
pub blocks : Vec < BlobBlockType < T > > ,
12
10
}
13
11
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
-
75
12
impl < ' a > BlockList < & ' a str > {
76
13
pub fn to_owned ( & self ) -> BlockList < String > {
77
14
let mut bl: BlockList < String > = BlockList {
@@ -90,6 +27,19 @@ impl<'a> BlockList<&'a str> {
90
27
}
91
28
}
92
29
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
+
93
43
impl < T > BlockList < T >
94
44
where
95
45
T : Borrow < str > ,
@@ -123,42 +73,16 @@ mod test {
123
73
use super :: * ;
124
74
125
75
#[ 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 ( ) {
145
77
let mut blocks = BlockList { blocks : Vec :: new ( ) } ;
146
78
blocks. blocks . push ( BlobBlockType :: Committed ( "numero1" ) ) ;
147
79
blocks. blocks . push ( BlobBlockType :: Uncommitted ( "numero2" ) ) ;
148
80
blocks. blocks . push ( BlobBlockType :: Uncommitted ( "numero3" ) ) ;
149
81
blocks. blocks . push ( BlobBlockType :: Latest ( "numero4" ) ) ;
150
82
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 ( ) ;
156
84
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
162
86
}
163
87
164
88
}
0 commit comments