29
29
//!
30
30
//! See [`Writer`] for further information.
31
31
//!
32
+ //! [`Reader::read_event_into`]: crate::reader::Reader::read_event_into
33
+ //! [`Reader`]: crate::reader::Reader
32
34
//! [`Writer`]: crate::writer::Writer
33
35
//! [`Event`]: crate::events::Event
34
36
@@ -44,7 +46,7 @@ use std::str::from_utf8;
44
46
use crate :: errors:: { Error , Result } ;
45
47
use crate :: escape:: { escape, partial_escape, unescape_with} ;
46
48
use crate :: name:: { LocalName , QName } ;
47
- use crate :: reader:: { Decoder , Reader } ;
49
+ use crate :: reader:: Decoder ;
48
50
use crate :: utils:: write_cow_string;
49
51
use attributes:: { Attribute , Attributes } ;
50
52
@@ -84,9 +86,9 @@ impl<'a> BytesStartText<'a> {
84
86
///
85
87
/// This method does not unescapes content, because no escape sequences can
86
88
/// appeared in the BOM or in the text before the first tag.
87
- pub fn decode_with_bom_removal ( & self , decoder : Decoder ) -> Result < String > {
89
+ pub fn decode_with_bom_removal ( & self ) -> Result < String > {
88
90
//TODO: Fix lifetime issue - it should be possible to borrow string
89
- let decoded = decoder. decode_with_bom_removal ( & * self ) ?;
91
+ let decoded = self . content . decoder . decode_with_bom_removal ( & * self ) ?;
90
92
91
93
Ok ( decoded. to_string ( ) )
92
94
}
@@ -758,59 +760,23 @@ impl<'a> BytesText<'a> {
758
760
}
759
761
}
760
762
761
- /// Decodes using UTF-8 then unescapes the content of the event.
762
- ///
763
- /// Searches for '&' into content and try to escape the coded character if possible
764
- /// returns Malformed error with index within element if '&' is not followed by ';'
765
- ///
766
- /// See also [`unescape_with()`](Self::unescape_with)
767
- ///
768
- /// This method is available only if `encoding` feature is **not** enabled.
769
- #[ cfg( any( doc, not( feature = "encoding" ) ) ) ]
770
- pub fn unescape ( & self ) -> Result < Cow < str > > {
771
- self . unescape_with ( |_| None )
772
- }
773
-
774
- /// Decodes using UTF-8 then unescapes the content of the event with custom entities.
775
- ///
776
- /// Searches for '&' into content and try to escape the coded character if possible
777
- /// returns Malformed error with index within element if '&' is not followed by ';'
778
- /// A fallback resolver for additional custom entities can be provided via `resolve_entity`.
779
- ///
780
- /// See also [`unescape()`](Self::unescape)
781
- ///
782
- /// This method is available only if `encoding` feature is **not** enabled.
783
- #[ cfg( any( doc, not( feature = "encoding" ) ) ) ]
784
- pub fn unescape_with < ' entity > (
785
- & self ,
786
- resolve_entity : impl Fn ( & str ) -> Option < & ' entity str > ,
787
- ) -> Result < Cow < str > > {
788
- // from_utf8 should never fail because content is always UTF-8 encoded
789
- Ok ( unescape_with ( from_utf8 ( & self . content ) ?, resolve_entity) ?)
790
- }
791
-
792
763
/// Decodes then unescapes the content of the event.
793
764
///
794
765
/// This will allocate if the value contains any escape sequences or in
795
766
/// non-UTF-8 encoding.
796
- pub fn decode_and_unescape < B > ( & self , reader : & Reader < B > ) -> Result < Cow < str > > {
797
- self . decode_and_unescape_with ( reader , |_| None )
767
+ pub fn unescape ( & self ) -> Result < Cow < str > > {
768
+ self . unescape_with ( |_| None )
798
769
}
799
770
800
771
/// Decodes then unescapes the content of the event with custom entities.
801
772
///
802
773
/// This will allocate if the value contains any escape sequences or in
803
774
/// non-UTF-8 encoding.
804
- ///
805
- /// # Pre-condition
806
- ///
807
- /// The implementation of `resolve_entity` is expected to operate over UTF-8 inputs.
808
- pub fn decode_and_unescape_with < ' entity , B > (
775
+ pub fn unescape_with < ' entity > (
809
776
& self ,
810
- reader : & Reader < B > ,
811
777
resolve_entity : impl Fn ( & str ) -> Option < & ' entity str > ,
812
778
) -> Result < Cow < str > > {
813
- let decoded = reader . decoder ( ) . decode ( & * self ) ?;
779
+ let decoded = self . decoder . decode ( & * self ) ?;
814
780
815
781
match unescape_with ( & decoded, resolve_entity) ? {
816
782
// Because result is borrowed, no replacements was done and we can use original string
@@ -820,15 +786,15 @@ impl<'a> BytesText<'a> {
820
786
}
821
787
822
788
/// Gets content of this text buffer in the specified encoding and optionally
823
- /// unescapes it. Unlike [`Self::decode_and_unescape `] & Co., the lifetime
789
+ /// unescapes it. Unlike [`Self::unescape `] & Co., the lifetime
824
790
/// of the returned `Cow` is bound to the original buffer / input
825
791
#[ cfg( feature = "serialize" ) ]
826
- pub ( crate ) fn decode ( & self , decoder : Decoder , unescape : bool ) -> Result < Cow < ' a , str > > {
792
+ pub ( crate ) fn decode ( & self , unescape : bool ) -> Result < Cow < ' a , str > > {
827
793
//TODO: too many copies, can be optimized
828
794
let text = match & self . content {
829
- Cow :: Borrowed ( bytes) => decoder. decode ( bytes) ?,
795
+ Cow :: Borrowed ( bytes) => self . decoder . decode ( bytes) ?,
830
796
// Convert to owned, because otherwise Cow will be bound with wrong lifetime
831
- Cow :: Owned ( bytes) => decoder. decode ( bytes) ?. into_owned ( ) . into ( ) ,
797
+ Cow :: Owned ( bytes) => self . decoder . decode ( bytes) ?. into_owned ( ) . into ( ) ,
832
798
} ;
833
799
let text = if unescape {
834
800
//FIXME: need to take into account entities defined in the document
@@ -930,8 +896,8 @@ impl<'a> BytesCData<'a> {
930
896
/// | `&` | `&`
931
897
/// | `'` | `'`
932
898
/// | `"` | `"`
933
- pub fn escape ( self , decoder : Decoder ) -> Result < BytesText < ' a > > {
934
- let decoded = self . decode ( decoder ) ?;
899
+ pub fn escape ( self ) -> Result < BytesText < ' a > > {
900
+ let decoded = self . decode ( ) ?;
935
901
Ok ( BytesText :: wrap (
936
902
match escape ( & decoded) {
937
903
// Because result is borrowed, no replacements was done and we can use original content
@@ -955,8 +921,8 @@ impl<'a> BytesCData<'a> {
955
921
/// | `<` | `<`
956
922
/// | `>` | `>`
957
923
/// | `&` | `&`
958
- pub fn partial_escape ( self , decoder : Decoder ) -> Result < BytesText < ' a > > {
959
- let decoded = self . decode ( decoder ) ?;
924
+ pub fn partial_escape ( self ) -> Result < BytesText < ' a > > {
925
+ let decoded = self . decode ( ) ?;
960
926
Ok ( BytesText :: wrap (
961
927
match partial_escape ( & decoded) {
962
928
// Because result is borrowed, no replacements was done and we can use original content
@@ -968,11 +934,11 @@ impl<'a> BytesCData<'a> {
968
934
}
969
935
970
936
/// Gets content of this text buffer in the specified encoding
971
- pub ( crate ) fn decode ( & self , decoder : Decoder ) -> Result < Cow < ' a , str > > {
937
+ pub ( crate ) fn decode ( & self ) -> Result < Cow < ' a , str > > {
972
938
Ok ( match & self . content {
973
- Cow :: Borrowed ( bytes) => decoder. decode ( bytes) ?,
939
+ Cow :: Borrowed ( bytes) => self . decoder . decode ( bytes) ?,
974
940
// Convert to owned, because otherwise Cow will be bound with wrong lifetime
975
- Cow :: Owned ( bytes) => decoder. decode ( bytes) ?. into_owned ( ) . into ( ) ,
941
+ Cow :: Owned ( bytes) => self . decoder . decode ( bytes) ?. into_owned ( ) . into ( ) ,
976
942
} )
977
943
}
978
944
}
@@ -996,6 +962,8 @@ impl<'a> Deref for BytesCData<'a> {
996
962
////////////////////////////////////////////////////////////////////////////////////////////////////
997
963
998
964
/// Event emitted by [`Reader::read_event_into`].
965
+ ///
966
+ /// [`Reader::read_event_into`]: crate::reader::Reader::read_event_into
999
967
#[ derive( Clone , Debug , Eq , PartialEq ) ]
1000
968
pub enum Event < ' a > {
1001
969
/// Text that appeared before the first opening tag or an [XML declaration].
@@ -1044,6 +1012,7 @@ pub enum Event<'a> {
1044
1012
///
1045
1013
/// [XML declaration]: Event::Decl
1046
1014
/// [std]: https://www.w3.org/TR/xml11/#NT-document
1015
+ /// [`Reader`]: crate::reader::Reader
1047
1016
/// [`Writer`]: crate::writer::Writer
1048
1017
StartText ( BytesStartText < ' a > ) ,
1049
1018
/// Start tag (with attributes) `<tag attr="value">`.
0 commit comments