1
1
/// The [`xmlity::de::Deserializer`] implementation for the `quick-xml` crate.
2
2
///
3
3
/// This deserializer is based upon the [`quick_xml::NsReader`] with the same limits as the underlying reader, including requiring a `[u8]` backing.
4
- use std:: { borrow:: Cow , ops:: Deref } ;
4
+ use std:: { borrow:: Cow , collections :: HashMap , ops:: Deref , rc :: Rc } ;
5
5
6
6
use quick_xml:: {
7
7
events:: { attributes:: Attribute , BytesCData , BytesDecl , BytesPI , BytesStart , BytesText , Event } ,
@@ -11,7 +11,7 @@ use quick_xml::{
11
11
12
12
use xmlity:: {
13
13
de:: {
14
- self , Error as _, NamespaceContext , Visitor , XmlCData , XmlComment , XmlDeclaration ,
14
+ self , DeserializeContext , Error as _, Visitor , XmlCData , XmlComment , XmlDeclaration ,
15
15
XmlDoctype , XmlProcessingInstruction , XmlText ,
16
16
} ,
17
17
Deserialize , ExpandedName , LocalName , XmlNamespace ,
@@ -202,6 +202,42 @@ impl<'i> Reader<'i> {
202
202
}
203
203
}
204
204
205
+ /// A struct to hold external data that can be used during deserialization.
206
+ #[ derive( Debug ) ]
207
+ pub struct ExternalData {
208
+ data : HashMap < core:: any:: TypeId , Box < dyn core:: any:: Any > > ,
209
+ }
210
+
211
+ impl ExternalData {
212
+ /// Creates a new [`ExternalData`] instance.
213
+ pub fn new ( ) -> Self {
214
+ Self {
215
+ data : HashMap :: new ( ) ,
216
+ }
217
+ }
218
+
219
+ /// Inserts data into the external data map.
220
+ pub fn insert < T : ' static > ( & mut self , data : T ) {
221
+ self . data . insert (
222
+ core:: any:: TypeId :: of :: < T > ( ) ,
223
+ Box :: new ( data) as Box < dyn core:: any:: Any > ,
224
+ ) ;
225
+ }
226
+
227
+ /// Retrieves data from the external data map.
228
+ pub fn get < T : ' static > ( & self ) -> Option < & T > {
229
+ self . data
230
+ . get ( & core:: any:: TypeId :: of :: < T > ( ) )
231
+ . and_then ( |data| data. downcast_ref :: < T > ( ) )
232
+ }
233
+ }
234
+
235
+ impl Default for ExternalData {
236
+ fn default ( ) -> Self {
237
+ Self :: new ( )
238
+ }
239
+ }
240
+
205
241
/// The [`xmlity::Deserializer`] for the `quick-xml` crate.
206
242
///
207
243
/// This currently only supports an underlying reader of type `&[u8]` due to limitations in the `quick-xml` crate.
@@ -210,6 +246,7 @@ pub struct Deserializer<'i> {
210
246
reader : Reader < ' i > ,
211
247
// Limit depth
212
248
limit_depth : i16 ,
249
+ external_data : Option < Rc < ExternalData > > ,
213
250
}
214
251
215
252
impl < ' i > From < NsReader < & ' i [ u8 ] > > for Deserializer < ' i > {
@@ -230,9 +267,16 @@ impl<'i> Deserializer<'i> {
230
267
Self {
231
268
reader : Reader :: new ( reader) ,
232
269
limit_depth : 0 ,
270
+ external_data : None ,
233
271
}
234
272
}
235
273
274
+ /// Set the external data for the deserializer.
275
+ pub fn with_external_data ( mut self , external_data : ExternalData ) -> Self {
276
+ self . external_data = Some ( Rc :: new ( external_data) ) ;
277
+ self
278
+ }
279
+
236
280
fn read_until_end ( & mut self ) -> Result < ( ) , Error > {
237
281
while let Some ( event) = self . next_event ( ) {
238
282
debug_assert ! ( !matches!( event, Event :: Eof ) ) ;
@@ -281,6 +325,7 @@ impl<'i> Deserializer<'i> {
281
325
Self {
282
326
reader : self . reader . clone ( ) ,
283
327
limit_depth,
328
+ external_data : self . external_data . clone ( ) ,
284
329
}
285
330
}
286
331
@@ -315,7 +360,7 @@ impl<'r> ElementAccess<'_, 'r> {
315
360
316
361
const PLACEHOLDER_ELEMENT_NAME : & str = "a" ;
317
362
318
- impl NamespaceContext for & Deserializer < ' _ > {
363
+ impl DeserializeContext for & Deserializer < ' _ > {
319
364
fn default_namespace ( & self ) -> Option < XmlNamespace < ' _ > > {
320
365
let ( _, namespace) = self
321
366
. resolve_qname ( QuickName ( PLACEHOLDER_ELEMENT_NAME . as_bytes ( ) ) , false )
@@ -332,6 +377,13 @@ impl NamespaceContext for &Deserializer<'_> {
332
377
333
378
namespace. map ( XmlNamespace :: into_owned)
334
379
}
380
+
381
+ fn external_data < T > ( & self ) -> Option < & T >
382
+ where
383
+ T : core:: any:: Any ,
384
+ {
385
+ self . external_data . as_ref ( ) . and_then ( |data| data. get :: < T > ( ) )
386
+ }
335
387
}
336
388
337
389
struct AttributeAccess < ' a , ' v > {
@@ -367,7 +419,7 @@ struct TextDeserializer<'a, 'v> {
367
419
}
368
420
369
421
impl < ' de > de:: XmlText < ' de > for TextDeserializer < ' _ , ' de > {
370
- type NamespaceContext < ' a >
422
+ type DeserializeContext < ' a >
371
423
= & ' a Deserializer < ' a >
372
424
where
373
425
Self : ' a ;
@@ -391,7 +443,7 @@ impl<'de> de::XmlText<'de> for TextDeserializer<'_, 'de> {
391
443
std:: str:: from_utf8 ( self . value . as_ref ( ) ) . unwrap ( )
392
444
}
393
445
394
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
446
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
395
447
self . deserializer
396
448
}
397
449
}
@@ -617,7 +669,7 @@ impl<'de> de::AttributesAccess<'de> for ElementAccess<'_, 'de> {
617
669
618
670
impl < ' a , ' de > de:: ElementAccess < ' de > for ElementAccess < ' a , ' de > {
619
671
type ChildrenAccess = SeqAccess < ' a , ' de > ;
620
- type NamespaceContext < ' b >
672
+ type DeserializeContext < ' b >
621
673
= & ' b Deserializer < ' de >
622
674
where
623
675
Self : ' b ;
@@ -646,7 +698,7 @@ impl<'a, 'de> de::ElementAccess<'de> for ElementAccess<'a, 'de> {
646
698
} )
647
699
}
648
700
649
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
701
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
650
702
self . deserializer ( )
651
703
}
652
704
}
@@ -757,7 +809,7 @@ impl<'a, T> DataWithD<'a, T> {
757
809
}
758
810
759
811
impl < ' de > XmlText < ' de > for DataWithD < ' _ , BytesText < ' de > > {
760
- type NamespaceContext < ' a >
812
+ type DeserializeContext < ' a >
761
813
= & ' a Deserializer < ' a >
762
814
where
763
815
Self : ' a ;
@@ -781,13 +833,13 @@ impl<'de> XmlText<'de> for DataWithD<'_, BytesText<'de>> {
781
833
std:: str:: from_utf8 ( self . data . deref ( ) ) . unwrap ( )
782
834
}
783
835
784
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
836
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
785
837
self . deserializer
786
838
}
787
839
}
788
840
789
841
impl < ' de > XmlCData < ' de > for DataWithD < ' _ , BytesCData < ' de > > {
790
- type NamespaceContext < ' a >
842
+ type DeserializeContext < ' a >
791
843
= & ' a Deserializer < ' a >
792
844
where
793
845
Self : ' a ;
@@ -811,13 +863,13 @@ impl<'de> XmlCData<'de> for DataWithD<'_, BytesCData<'de>> {
811
863
std:: str:: from_utf8 ( self . data . deref ( ) ) . unwrap ( )
812
864
}
813
865
814
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
866
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
815
867
self . deserializer
816
868
}
817
869
}
818
870
819
871
impl < ' de > XmlComment < ' de > for DataWithD < ' _ , BytesText < ' de > > {
820
- type NamespaceContext < ' a >
872
+ type DeserializeContext < ' a >
821
873
= & ' a Deserializer < ' a >
822
874
where
823
875
Self : ' a ;
@@ -830,7 +882,7 @@ impl<'de> XmlComment<'de> for DataWithD<'_, BytesText<'de>> {
830
882
self . data . deref ( )
831
883
}
832
884
833
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
885
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
834
886
self . deserializer
835
887
}
836
888
}
@@ -862,7 +914,7 @@ impl<'a> TryFrom<&'a BytesDecl<'a>> for ClearedByteDecl<'a> {
862
914
}
863
915
864
916
impl XmlDeclaration for DataWithD < ' _ , ClearedByteDecl < ' _ > > {
865
- type NamespaceContext < ' a >
917
+ type DeserializeContext < ' a >
866
918
= & ' a Deserializer < ' a >
867
919
where
868
920
Self : ' a ;
@@ -879,13 +931,13 @@ impl XmlDeclaration for DataWithD<'_, ClearedByteDecl<'_>> {
879
931
self . data . standalone . as_deref ( )
880
932
}
881
933
882
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
934
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
883
935
self . deserializer
884
936
}
885
937
}
886
938
887
939
impl XmlProcessingInstruction for DataWithD < ' _ , BytesPI < ' _ > > {
888
- type NamespaceContext < ' a >
940
+ type DeserializeContext < ' a >
889
941
= & ' a Deserializer < ' a >
890
942
where
891
943
Self : ' a ;
@@ -898,13 +950,13 @@ impl XmlProcessingInstruction for DataWithD<'_, BytesPI<'_>> {
898
950
self . data . content ( )
899
951
}
900
952
901
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
953
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
902
954
self . deserializer
903
955
}
904
956
}
905
957
906
958
impl < ' de > XmlDoctype < ' de > for DataWithD < ' _ , BytesText < ' de > > {
907
- type NamespaceContext < ' a >
959
+ type DeserializeContext < ' a >
908
960
= & ' a Deserializer < ' a >
909
961
where
910
962
Self : ' a ;
@@ -917,7 +969,7 @@ impl<'de> XmlDoctype<'de> for DataWithD<'_, BytesText<'de>> {
917
969
self . data . deref ( )
918
970
}
919
971
920
- fn namespace_context ( & self ) -> Self :: NamespaceContext < ' _ > {
972
+ fn context ( & self ) -> Self :: DeserializeContext < ' _ > {
921
973
self . deserializer
922
974
}
923
975
}
0 commit comments