Skip to content

Commit 21f7800

Browse files
authored
Introduce suitable types for handling base64 and hex strings
This commit introduces suitable types for handling `base64` and `hex` strings: `Base64String`, `Base64Binary`, `HexString` and `HexBinary`. The old implementation used `String` to represent these types, but the problem with that was, that the length constrains applied to these values were not correct, because it returned the length of the string, not of the actual data. The new types fixes that. For convenience we provided two different types for each of the two data formats, one that stores a `Vec<u8>` that contains the decoded data and one that stores the string encoded value as is (in cases where decoding is not needed).
1 parent 5987bb4 commit 21f7800

56 files changed

Lines changed: 3631 additions & 313 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 86 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ homepage = "https://github.com/Bergmann89/xsd-parser"
2424
[workspace.dependencies]
2525
anyhow = "1.0"
2626
base64 = "0.22"
27+
const-hex = "1.17.0"
2728
bit-set = "0.8"
2829
bitflags = "2.7"
2930
bytesize = "2.3"

xsd-parser-types/Cargo.toml

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,34 @@ repository.workspace = true
1414
all-features = true
1515

1616
[features]
17-
default = [ ]
17+
default = []
1818
# Enable support for async `quick-xml` de-/serialization
19-
async = [ "dep:futures", "dep:tokio", "quick-xml/async-tokio" ]
19+
async = ["dep:futures", "dep:tokio", "quick-xml/async-tokio"]
20+
21+
# Enable support for base64 encoding/decoding
22+
base64 = ["dep:base64"]
2023

2124
# Enable support for dynamic xml types
22-
xml = [ "dep:indexmap", "dep:encoding_rs" ]
25+
xml = ["dep:indexmap", "dep:encoding_rs"]
2326

2427
# Enable support for `quick-xml` de-/serialization
25-
quick-xml = [ "xml", "dep:quick-xml", "dep:regex", "dep:thiserror" ]
28+
quick-xml = [
29+
"xml",
30+
"dep:quick-xml",
31+
"dep:regex",
32+
"dep:thiserror",
33+
"dep:const-hex",
34+
"dep:base64",
35+
]
2636

2737
[dependencies]
2838
base64 = { workspace = true, optional = true }
39+
const-hex = { workspace = true, optional = true }
2940
encoding_rs = { workspace = true, optional = true }
3041
futures = { workspace = true, optional = true }
3142
indexmap = { workspace = true, optional = true }
3243
num = { workspace = true, optional = true }
33-
quick-xml = { workspace = true, optional = true, features = [ "encoding" ] }
44+
quick-xml = { workspace = true, optional = true, features = ["encoding"] }
3445
regex = { workspace = true, optional = true }
3546
serde = { workspace = true, optional = true }
3647
thiserror = { workspace = true, optional = true }

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ use num::{BigInt, BigRational, BigUint};
1010
#[cfg(feature = "quick-xml")]
1111
use quick_xml::events::Event;
1212

13+
use crate::quick_xml::{DeserializeBytes, SerializeBytes};
14+
use crate::xml::NamespacesShared;
15+
1316
#[cfg(feature = "quick-xml")]
1417
use crate::{
1518
misc::{Namespace, NamespacePrefix},
@@ -19,10 +22,6 @@ use crate::{
1922
SerializeHelper, Serializer, WithDeserializer, WithSerializer,
2023
},
2124
};
22-
use crate::{
23-
quick_xml::{DeserializeBytes, SerializeBytes},
24-
xml::NamespacesShared,
25-
};
2625

2726
use super::QName;
2827

@@ -45,10 +44,10 @@ pub type Unsigned = BigUint;
4544
pub type Unsigned = usize;
4645

4746
#[cfg(feature = "base64")]
48-
pub type Base64Binary = Vec<u8>;
47+
type Base64Binary = crate::xml::Base64Binary;
4948

5049
#[cfg(not(feature = "base64"))]
51-
pub type Base64Binary = String;
50+
type Base64Binary = crate::xml::Base64String;
5251

5352
/// Type that represents an `xs:anySimpleType` value.
5453
#[derive(Debug, Clone)]
@@ -599,14 +598,15 @@ fn parse_base64_binary(bytes: &str) -> Result<Base64Binary, Error> {
599598

600599
Ok(BASE64_STANDARD
601600
.decode(bytes)
602-
.map_err(|_| ErrorKind::InvalidData(bytes.as_bytes().to_vec().into()))?)
601+
.map_err(|_| ErrorKind::InvalidData(bytes.as_bytes().to_vec().into()))?
602+
.into())
603603
}
604604

605605
#[inline]
606606
#[cfg(not(feature = "base64"))]
607607
#[allow(clippy::unnecessary_wraps)]
608608
fn parse_base64_binary(s: &str) -> Result<Base64Binary, Error> {
609-
Ok(s.to_string())
609+
Ok(s.to_string().into())
610610
}
611611

612612
fn format_vec<X: Display>(items: &[X]) -> String {

0 commit comments

Comments
 (0)