Skip to content

Commit b8ca6af

Browse files
committed
Allow building xsd-parser-types with xml but without quick-xml
1 parent eb356b2 commit b8ca6af

9 files changed

Lines changed: 80 additions & 11 deletions

File tree

xsd-parser-types/src/xml/any_simple_type.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,21 @@ use std::borrow::Cow;
44
use std::fmt::Display;
55
use std::str::from_utf8;
66

7+
use crate::xml::NamespacesShared;
8+
79
#[cfg(feature = "num")]
810
use num::{BigInt, BigRational, BigUint};
911

1012
#[cfg(feature = "quick-xml")]
1113
use quick_xml::events::Event;
1214

13-
use crate::quick_xml::{DeserializeBytes, SerializeBytes};
14-
use crate::xml::NamespacesShared;
15-
1615
#[cfg(feature = "quick-xml")]
1716
use crate::{
1817
misc::{Namespace, NamespacePrefix},
1918
quick_xml::{
20-
ContentDeserializer, DeserializeHelper, Deserializer, DeserializerArtifact,
21-
DeserializerEvent, DeserializerOutput, DeserializerResult, Error, ErrorKind,
22-
SerializeHelper, Serializer, WithDeserializer, WithSerializer,
19+
ContentDeserializer, DeserializeBytes, DeserializeHelper, Deserializer,
20+
DeserializerArtifact, DeserializerEvent, DeserializerOutput, DeserializerResult, Error,
21+
ErrorKind, SerializeBytes, SerializeHelper, Serializer, WithDeserializer, WithSerializer,
2322
},
2423
};
2524

@@ -216,6 +215,7 @@ impl WithDeserializer for AnySimpleType {
216215
type Deserializer = AnySimpleTypeDeserializer;
217216
}
218217

218+
#[cfg(feature = "quick-xml")]
219219
impl SerializeBytes for AnySimpleType {
220220
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> Result<Option<Cow<'_, str>>, Error> {
221221
let _helper = helper;
@@ -224,6 +224,7 @@ impl SerializeBytes for AnySimpleType {
224224
}
225225
}
226226

227+
#[cfg(feature = "quick-xml")]
227228
impl DeserializeBytes for AnySimpleType {
228229
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
229230
Self::deserialize_str(helper, from_utf8(bytes)?)
@@ -603,7 +604,7 @@ fn parse_base64_binary(bytes: &str) -> Result<Base64Binary, Error> {
603604
}
604605

605606
#[inline]
606-
#[cfg(not(feature = "base64"))]
607+
#[cfg(all(feature = "quick-xml", not(feature = "base64")))]
607608
#[allow(clippy::unnecessary_wraps)]
608609
fn parse_base64_binary(s: &str) -> Result<Base64Binary, Error> {
609610
Ok(s.to_string().into())

xsd-parser-types/src/xml/attributes.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::ops::{Deref, DerefMut};
44

55
use encoding_rs::{Encoding, UTF_8};
66
use indexmap::{map::Entry, IndexMap};
7+
#[cfg(feature = "quick-xml")]
78
use quick_xml::{
89
encoding::decode,
910
escape::{resolve_predefined_entity, unescape_with},
@@ -16,6 +17,21 @@ use crate::misc::{format_utf8_slice, RawByteStr};
1617
#[cfg(feature = "quick-xml")]
1718
use crate::quick_xml::{Error, ErrorKind};
1819

20+
#[cfg(not(feature = "quick-xml"))]
21+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
22+
pub struct QName<'a>(pub &'a [u8]);
23+
24+
#[cfg(not(feature = "quick-xml"))]
25+
#[derive(Debug, Clone, Eq, PartialEq)]
26+
pub struct Attribute<'a> {
27+
/// The key to uniquely define the attribute.
28+
///
29+
/// If [`Attributes::with_checks`] is turned off, the key might not be unique.
30+
pub key: QName<'a>,
31+
/// The raw value of the attribute.
32+
pub value: Cow<'a, [u8]>,
33+
}
34+
1935
/// Represents a list of unstructured XML attributes.
2036
#[derive(Default, Debug, Clone, Eq, PartialEq)]
2137
pub struct Attributes<'a>(pub IndexMap<Key<'a>, Value<'a>>);
@@ -33,6 +49,7 @@ impl<'a> Attributes<'a> {
3349
///
3450
/// Raises a [`DuplicateAttribute`](ErrorKind::DuplicateAttribute) error if
3551
/// the passed attribute is already part of the list.
52+
#[cfg(feature = "quick-xml")]
3653
pub fn push(&mut self, attrib: Attribute<'_>) -> Result<(), Error> {
3754
let key = Key(Cow::Owned(attrib.key.0.to_owned()));
3855

@@ -154,6 +171,7 @@ impl Borrow<[u8]> for Key<'_> {
154171
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
155172
pub struct Value<'a>(pub Cow<'a, [u8]>);
156173

174+
#[cfg(feature = "quick-xml")]
157175
impl Value<'_> {
158176
/// Return the unescaped value using UTF-8 encoding.
159177
///

xsd-parser-types/src/xml/mixed.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ use crate::quick_xml::{
1111
WithSerializer,
1212
};
1313

14-
use super::text::{Text, TextDeserializer};
14+
use super::text::Text;
15+
#[cfg(feature = "quick-xml")]
16+
use super::text::TextDeserializer;
1517

1618
/// Used to represent xml elements with mixed content
1719
#[derive(Default, Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]

xsd-parser-types/src/xml/mod.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@ pub use self::attributes::{
2323
AnyAttributes, Attributes, Key as AttributeKey, Value as AttributeValue,
2424
};
2525
pub use self::element::{AnyElement, AnyElements, Element, Elements};
26-
pub use self::mixed::{Mixed, MixedDeserializer, MixedSerializer};
26+
pub use self::mixed::Mixed;
27+
#[cfg(feature = "quick-xml")]
28+
pub use self::mixed::{MixedDeserializer, MixedSerializer};
2729
pub use self::namespace_scope::NamespaceScope;
2830
pub use self::namespaces::{
2931
Key as NamespaceKey, Namespaces, NamespacesShared, Value as NamespaceValue,
3032
};
31-
pub use self::nillable::{Nillable, NillableDeserializer, NillableSerializer};
33+
pub use self::nillable::Nillable;
34+
#[cfg(feature = "quick-xml")]
35+
pub use self::nillable::{NillableDeserializer, NillableSerializer};
3236
pub use self::qname::QName;
33-
pub use self::text::{Text, TextDeserializer, TextSerializer};
37+
pub use self::text::Text;
38+
#[cfg(feature = "quick-xml")]
39+
pub use self::text::{TextDeserializer, TextSerializer};
3440
pub use self::value::Value;
3541

3642
#[cfg(feature = "quick-xml")]

xsd-parser-types/src/xml/namespace_scope.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ impl<T> NamespaceScope<T> {
9393
}
9494
}
9595

96+
#[cfg(feature = "quick-xml")]
9697
impl<T> SerializeBytes for NamespaceScope<T>
9798
where
9899
T: SerializeBytes,
@@ -102,6 +103,7 @@ where
102103
}
103104
}
104105

106+
#[cfg(feature = "quick-xml")]
105107
impl<T> DeserializeBytes for NamespaceScope<T>
106108
where
107109
T: DeserializeBytes,

xsd-parser-types/src/xml/namespaces.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::hash::{Hash, Hasher};
55
use std::ops::{Deref, DerefMut};
66
use std::sync::Arc;
77

8+
#[cfg(feature = "quick-xml")]
89
use quick_xml::name::{Namespace, PrefixDeclaration};
910

1011
use crate::misc::format_utf8_slice;
@@ -76,6 +77,7 @@ where
7677
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
7778
pub struct Key<'a>(pub Cow<'a, [u8]>);
7879

80+
#[cfg(feature = "quick-xml")]
7981
impl Key<'_> {
8082
/// Get the key value as [`PrefixDeclaration`].
8183
#[must_use]
@@ -140,6 +142,7 @@ impl Borrow<[u8]> for Key<'_> {
140142
#[derive(Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
141143
pub struct Value<'a>(pub Cow<'a, [u8]>);
142144

145+
#[cfg(feature = "quick-xml")]
143146
impl Value<'_> {
144147
/// Get the namespace as [`Namespace`].
145148
#[inline]

xsd-parser-types/src/xml/qname.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ use std::borrow::Cow;
22
use std::fmt::{Debug, Display, Formatter, Result as FmtResult};
33
use std::hash::{Hash, Hasher};
44

5+
#[cfg(feature = "quick-xml")]
56
use quick_xml::name::{QName as QuickXmlQName, ResolveResult};
67

78
use crate::misc::{format_utf8_slice, Namespace};
9+
#[cfg(feature = "quick-xml")]
810
use crate::quick_xml::{WithDeserializerFromBytes, WithSerializeToBytes};
911

1012
#[cfg(feature = "quick-xml")]
@@ -152,6 +154,7 @@ impl SerializeBytes for QName {
152154
}
153155
}
154156

157+
#[cfg(feature = "quick-xml")]
155158
impl WithSerializeToBytes for QName {}
156159

157160
#[cfg(feature = "quick-xml")]
@@ -161,6 +164,7 @@ impl DeserializeBytes for QName {
161164
}
162165
}
163166

167+
#[cfg(feature = "quick-xml")]
164168
impl WithDeserializerFromBytes for QName {}
165169

166170
#[allow(clippy::missing_fields_in_debug)]

xsd-parser-types/src/xml/text.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ impl DerefMut for Text {
6565
}
6666
}
6767

68+
#[cfg(feature = "quick-xml")]
6869
impl WithSerializer for Text {
6970
type Serializer<'x> = TextSerializer<'x>;
7071

@@ -80,11 +81,13 @@ impl WithSerializer for Text {
8081
}
8182
}
8283

84+
#[cfg(feature = "quick-xml")]
8385
impl WithDeserializer for Text {
8486
type Deserializer = TextDeserializer;
8587
}
8688

8789
/// Implemented the [`Serializer`](crate::quick_xml::Serializer) trait for [`Text`].
90+
#[cfg(feature = "quick-xml")]
8891
#[derive(Debug)]
8992
pub enum TextSerializer<'ser> {
9093
/// Emit events for the contained text value.
@@ -97,6 +100,7 @@ pub enum TextSerializer<'ser> {
97100
Done,
98101
}
99102

103+
#[cfg(feature = "quick-xml")]
100104
impl<'ser> Serializer<'ser> for TextSerializer<'ser> {
101105
fn next(&mut self, helper: &mut SerializeHelper) -> Option<Result<Event<'ser>, Error>> {
102106
let _helper = helper;
@@ -111,6 +115,7 @@ impl<'ser> Serializer<'ser> for TextSerializer<'ser> {
111115
}
112116

113117
/// Implemented the [`Deserializer`] trait for [`Text`].
118+
#[cfg(feature = "quick-xml")]
114119
#[derive(Debug)]
115120
pub enum TextDeserializer {
116121
/// Init the deserializer
@@ -123,6 +128,7 @@ pub enum TextDeserializer {
123128
},
124129
}
125130

131+
#[cfg(feature = "quick-xml")]
126132
impl<'de> Deserializer<'de, Text> for TextDeserializer {
127133
fn init(helper: &mut DeserializeHelper, event: Event<'de>) -> DeserializerResult<'de, Text> {
128134
Self::Init.next(helper, event)

xsd-parser-types/src/xml/value.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
use std::fmt::{Debug, Formatter, Result as FmtResult};
22

3+
#[cfg(feature = "quick-xml")]
34
use quick_xml::events::{BytesCData, BytesText};
45

56
use crate::misc::format_utf8_slice;
67

78
use super::Element;
89

10+
#[cfg(not(feature = "quick-xml"))]
11+
#[derive(Debug, Clone, PartialEq, Eq)]
12+
pub struct BytesText<'a>(std::borrow::Cow<'a, [u8]>);
13+
14+
#[cfg(not(feature = "quick-xml"))]
15+
impl<'a> std::ops::Deref for BytesText<'a> {
16+
type Target = [u8];
17+
18+
fn deref(&self) -> &[u8] {
19+
&self.0
20+
}
21+
}
22+
23+
#[cfg(not(feature = "quick-xml"))]
24+
#[derive(Debug, Clone, PartialEq, Eq)]
25+
pub struct BytesCData<'a>(std::borrow::Cow<'a, [u8]>);
26+
27+
#[cfg(not(feature = "quick-xml"))]
28+
impl<'a> std::ops::Deref for BytesCData<'a> {
29+
type Target = [u8];
30+
31+
fn deref(&self) -> &[u8] {
32+
&self.0
33+
}
34+
}
35+
936
/// Represents unstructured XML data.
1037
///
1138
/// This is mainly used to store the data contained by an [`Element`].

0 commit comments

Comments
 (0)