@@ -7,7 +7,6 @@ use quick_xml::name::QName;
77use quick_xml:: reader:: Reader ;
88use std:: borrow:: Cow ;
99use std:: collections:: HashMap ;
10- use std:: convert:: Infallible ;
1110use std:: str;
1211
1312const XML : & str = r#"
@@ -47,8 +46,8 @@ impl Translation {
4746 for attr_result in element. attributes ( ) {
4847 let a = attr_result?;
4948 match a. key . as_ref ( ) {
50- b "Language" => lang = a . decode_and_unescape_value ( reader ) ? ,
51- b "Tag" => tag = a . decode_and_unescape_value ( reader ) ? ,
49+ "Language" => lang = Cow :: Owned ( a . unescape_value ( ) ? . to_string ( ) ) ,
50+ "Tag" => tag = Cow :: Owned ( a . unescape_value ( ) ? . to_string ( ) ) ,
5251 _ => ( ) ,
5352 }
5453 }
@@ -57,7 +56,7 @@ impl Translation {
5756
5857 if let Event :: Start ( ref e) = event {
5958 let name = e. name ( ) ;
60- if name == QName ( b "Text") {
59+ if name == QName ( "Text" ) {
6160 // note: `read_text` does not support content as CDATA
6261 let text_content = reader. read_text ( e. name ( ) ) ?;
6362 Ok ( Translation {
@@ -67,8 +66,7 @@ impl Translation {
6766 } )
6867 } else {
6968 dbg ! ( "Expected Event::Start for Text, got: {:?}" , & event) ;
70- let name_string = reader. decoder ( ) . decode ( name. as_ref ( ) ) ?;
71- Err ( quick_xml:: Error :: UnexpectedToken ( name_string. into ( ) ) )
69+ Err ( quick_xml:: Error :: UnexpectedToken ( name. as_ref ( ) . to_owned ( ) ) )
7270 }
7371 } else {
7472 let event_string = format ! ( "{:?}" , event) ;
@@ -99,24 +97,16 @@ fn main() -> Result<(), quick_xml::Error> {
9997
10098 match event {
10199 Event :: Start ( element) => match element. name ( ) . as_ref ( ) {
102- b "DefaultSettings" => {
100+ "DefaultSettings" => {
103101 // Note: real app would handle errors with good defaults or halt program with nice message
104102 // This illustrates decoding an attribute's key and value with error handling
105103 settings = element
106104 . attributes ( )
107105 . map ( |attr_result| {
108106 match attr_result {
109107 Ok ( a) => {
110- let key = reader. decoder ( ) . decode ( a. key . local_name ( ) . as_ref ( ) )
111- . or_else ( |err| {
112- dbg ! ( "unable to read key in DefaultSettings attribute {:?}, utf8 error {:?}" , & a, err) ;
113- Ok :: < Cow < ' _ , str > , Infallible > ( std:: borrow:: Cow :: from ( "" ) )
114- } )
115- . unwrap ( ) . to_string ( ) ;
116- let value = a. decode_and_unescape_value ( & reader) . or_else ( |err| {
117- dbg ! ( "unable to read key in DefaultSettings attribute {:?}, utf8 error {:?}" , & a, err) ;
118- Ok :: < Cow < ' _ , str > , Infallible > ( std:: borrow:: Cow :: from ( "" ) )
119- } ) . unwrap ( ) . to_string ( ) ;
108+ let key = a. key . local_name ( ) . as_ref ( ) . to_string ( ) ;
109+ let value = a. unescape_value ( ) . expect ( "failure to unescape" ) . to_string ( ) ;
120110 ( key, value)
121111 } ,
122112 Err ( err) => {
@@ -130,7 +120,7 @@ fn main() -> Result<(), quick_xml::Error> {
130120 assert_eq ! ( settings[ "Greeting" ] , "HELLO" ) ;
131121 reader. read_to_end ( element. name ( ) ) ?;
132122 }
133- b "Translation" => {
123+ "Translation" => {
134124 translations. push ( Translation :: new_from_element ( & mut reader, element) ?) ;
135125 }
136126 _ => ( ) ,
0 commit comments