File tree 1 file changed +59
-0
lines changed
1 file changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ use std:: convert:: Infallible ;
2
+
3
+ use quick_xml:: de:: { Deserializer , EntityResolver } ;
4
+ use quick_xml:: events:: BytesText ;
5
+ use serde:: Deserialize ;
6
+
7
+ struct TestResolver ;
8
+ impl EntityResolver for TestResolver {
9
+ type Error = Infallible ;
10
+
11
+ fn capture ( & mut self , _doctype : BytesText ) -> Result < ( ) , Self :: Error > {
12
+ Ok ( ( ) )
13
+ }
14
+ fn resolve ( & self , entity : & str ) -> Option < & str > {
15
+ match dbg ! ( entity) {
16
+ "text" => Some ( " <![CDATA[second text]]> " ) ,
17
+ _ => Some (
18
+ "
19
+ <child1 attribute = '<attribute value>'>&text;</child1>
20
+ <child2/>
21
+ " ,
22
+ ) ,
23
+ }
24
+ }
25
+ }
26
+
27
+ #[ derive( Debug , PartialEq , Deserialize ) ]
28
+ struct Root {
29
+ child1 : Child1 ,
30
+ child2 : ( ) ,
31
+ }
32
+
33
+ #[ derive( Debug , PartialEq , Deserialize ) ]
34
+ struct Child1 {
35
+ #[ serde( rename = "@attribute" ) ]
36
+ attribute : String ,
37
+
38
+ #[ serde( rename = "$text" ) ]
39
+ text : String ,
40
+ }
41
+
42
+ #[ test]
43
+ fn entities ( ) {
44
+ let mut de = Deserializer :: from_str_with_resolver ( "<root>&entity;</root>" , TestResolver ) ;
45
+
46
+ let data = Root :: deserialize ( & mut de) . unwrap ( ) ;
47
+
48
+ assert ! ( de. is_empty( ) , "the whole XML document should be consumed" ) ;
49
+ assert_eq ! (
50
+ data,
51
+ Root {
52
+ child1: Child1 {
53
+ attribute: "<attribute value>" . to_string( ) ,
54
+ text: " second text " . to_string( ) ,
55
+ } ,
56
+ child2: ( ) ,
57
+ }
58
+ ) ;
59
+ }
You can’t perform that action at this time.
0 commit comments