11use quick_xml:: events:: { BytesStart , Event } ;
22use quick_xml:: name:: { QName , ResolveResult } ;
3- use quick_xml:: { Reader , Result } ;
4- use std:: borrow:: Cow ;
3+ use quick_xml:: { Decoder , Reader , Result } ;
54use std:: str:: from_utf8;
65
76#[ test]
@@ -381,7 +380,7 @@ fn test_bytes(input: &[u8], output: &[u8], is_short: bool) {
381380 loop {
382381 buf. clear ( ) ;
383382 let event = reader. read_namespaced_event ( & mut buf, & mut ns_buffer) ;
384- let line = xmlrs_display ( event, & reader) ;
383+ let line = xmlrs_display ( event, reader. decoder ( ) ) ;
385384 if let Some ( ( n, spec) ) = spec_lines. next ( ) {
386385 if spec. trim ( ) == "EndDocument" {
387386 break ;
@@ -420,8 +419,8 @@ fn test_bytes(input: &[u8], output: &[u8], is_short: bool) {
420419 }
421420}
422421
423- fn namespace_name ( n : ResolveResult , name : QName , reader : & Reader < & [ u8 ] > ) -> String {
424- let name = decode ( name. as_ref ( ) , reader ) ;
422+ fn namespace_name ( n : ResolveResult , name : QName , decoder : Decoder ) -> String {
423+ let name = decoder . decode ( name. as_ref ( ) ) . unwrap ( ) ;
425424 match n {
426425 // Produces string '{namespace}prefixed_name'
427426 ResolveResult :: Bound ( n) => format ! ( "{{{}}}{}" , from_utf8( n. as_ref( ) ) . unwrap( ) , name) ,
@@ -448,44 +447,33 @@ fn make_attrs(e: &BytesStart) -> ::std::result::Result<String, String> {
448447 Ok ( atts. join ( ", " ) )
449448}
450449
451- // FIXME: The public API differs based on the "encoding" feature
452- fn decode < ' a > ( text : & ' a [ u8 ] , reader : & Reader < & [ u8 ] > ) -> Cow < ' a , str > {
453- #[ cfg( feature = "encoding" ) ]
454- let decoded = reader. decoder ( ) . decode ( text) ;
455-
456- #[ cfg( not( feature = "encoding" ) ) ]
457- let decoded = Cow :: Borrowed ( reader. decoder ( ) . decode ( text) . unwrap ( ) ) ;
458-
459- decoded
460- }
461-
462- fn xmlrs_display ( opt_event : Result < ( ResolveResult , Event ) > , reader : & Reader < & [ u8 ] > ) -> String {
450+ fn xmlrs_display ( opt_event : Result < ( ResolveResult , Event ) > , decoder : Decoder ) -> String {
463451 match opt_event {
464452 Ok ( ( _, Event :: StartText ( _) ) ) => "StartText" . to_string ( ) ,
465453 Ok ( ( n, Event :: Start ( ref e) ) ) => {
466- let name = namespace_name ( n, e. name ( ) , reader ) ;
454+ let name = namespace_name ( n, e. name ( ) , decoder ) ;
467455 match make_attrs ( e) {
468456 Ok ( ref attrs) if attrs. is_empty ( ) => format ! ( "StartElement({})" , & name) ,
469457 Ok ( ref attrs) => format ! ( "StartElement({} [{}])" , & name, & attrs) ,
470458 Err ( e) => format ! ( "StartElement({}, attr-error: {})" , & name, & e) ,
471459 }
472460 }
473461 Ok ( ( n, Event :: Empty ( ref e) ) ) => {
474- let name = namespace_name ( n, e. name ( ) , reader ) ;
462+ let name = namespace_name ( n, e. name ( ) , decoder ) ;
475463 match make_attrs ( e) {
476464 Ok ( ref attrs) if attrs. is_empty ( ) => format ! ( "EmptyElement({})" , & name) ,
477465 Ok ( ref attrs) => format ! ( "EmptyElement({} [{}])" , & name, & attrs) ,
478466 Err ( e) => format ! ( "EmptyElement({}, attr-error: {})" , & name, & e) ,
479467 }
480468 }
481469 Ok ( ( n, Event :: End ( ref e) ) ) => {
482- let name = namespace_name ( n, e. name ( ) , reader ) ;
470+ let name = namespace_name ( n, e. name ( ) , decoder ) ;
483471 format ! ( "EndElement({})" , name)
484472 }
485473 Ok ( ( _, Event :: Comment ( ref e) ) ) => format ! ( "Comment({})" , from_utf8( e) . unwrap( ) ) ,
486474 Ok ( ( _, Event :: CData ( ref e) ) ) => format ! ( "CData({})" , from_utf8( e) . unwrap( ) ) ,
487475 Ok ( ( _, Event :: Text ( ref e) ) ) => match e. unescaped ( ) {
488- Ok ( c) => format ! ( "Characters({})" , decode( c. as_ref( ) , reader ) . as_ref ( ) ) ,
476+ Ok ( c) => format ! ( "Characters({})" , decoder . decode( c. as_ref( ) ) . unwrap ( ) ) ,
489477 Err ( ref err) => format ! ( "FailedUnescape({:?}; {})" , e. escaped( ) , err) ,
490478 } ,
491479 Ok ( ( _, Event :: Decl ( ref e) ) ) => {
0 commit comments