Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 86 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ homepage = "https://github.com/Bergmann89/xsd-parser"
[workspace.dependencies]
anyhow = "1.0"
base64 = "0.22"
const-hex = "1.17.0"
bit-set = "0.8"
bitflags = "2.7"
bytesize = "2.3"
Expand Down
21 changes: 16 additions & 5 deletions xsd-parser-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,34 @@ repository.workspace = true
all-features = true

[features]
default = [ ]
default = []
# Enable support for async `quick-xml` de-/serialization
async = [ "dep:futures", "dep:tokio", "quick-xml/async-tokio" ]
async = ["dep:futures", "dep:tokio", "quick-xml/async-tokio"]

# Enable support for base64 encoding/decoding
base64 = ["dep:base64"]

# Enable support for dynamic xml types
xml = [ "dep:indexmap", "dep:encoding_rs" ]
xml = ["dep:indexmap", "dep:encoding_rs"]

# Enable support for `quick-xml` de-/serialization
quick-xml = [ "xml", "dep:quick-xml", "dep:regex", "dep:thiserror" ]
quick-xml = [
"xml",
"dep:quick-xml",
"dep:regex",
"dep:thiserror",
"dep:const-hex",
"dep:base64",
]

[dependencies]
base64 = { workspace = true, optional = true }
const-hex = { workspace = true, optional = true }
encoding_rs = { workspace = true, optional = true }
futures = { workspace = true, optional = true }
indexmap = { workspace = true, optional = true }
num = { workspace = true, optional = true }
quick-xml = { workspace = true, optional = true, features = [ "encoding" ] }
quick-xml = { workspace = true, optional = true, features = ["encoding"] }
regex = { workspace = true, optional = true }
serde = { workspace = true, optional = true }
thiserror = { workspace = true, optional = true }
Expand Down
129 changes: 129 additions & 0 deletions xsd-parser-types/src/xml/base64binary.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use std::borrow::Cow;
use std::ops::Deref;

#[cfg(feature = "quick-xml")]
use crate::quick_xml::{
DeserializeBytes, DeserializeHelper, Error, SerializeBytes, SerializeHelper,
};

#[cfg(any(feature = "quick-xml", feature = "serde"))]
use base64::{engine::general_purpose, Engine as _};

/// Wrapper for base64Binary encoded as a String.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Base64String(pub String);

impl Base64String {
/// Returns the byte length of the decoded data, not the length of the string.
#[must_use]
pub fn len(&self) -> usize {
let bytes = self.0.trim_end_matches('=');
bytes.len() * 3 / 4
}

/// Check emptyness
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}

/// Get the inner string as a &str.
#[must_use]
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}

impl From<String> for Base64String {
fn from(value: String) -> Self {
Self(value)
}
}

impl From<Base64String> for String {
fn from(value: Base64String) -> Self {
value.0
}
}

impl Deref for Base64String {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(feature = "quick-xml")]
impl SerializeBytes for Base64String {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> Result<Option<Cow<'_, str>>, Error> {
self.0.serialize_bytes(helper)
}
}

#[cfg(feature = "quick-xml")]
impl DeserializeBytes for Base64String {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
let inner = String::deserialize_bytes(helper, bytes)?;
Ok(Self(inner))
}
}

/// Wrapper for base64Binary as decoded bytes.
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
pub struct Base64Binary(pub Vec<u8>);

impl Deref for Base64Binary {
type Target = [u8];
Comment thread
p32blo marked this conversation as resolved.
Outdated

fn deref(&self) -> &Self::Target {
&self.0
}
}

#[cfg(feature = "quick-xml")]
impl SerializeBytes for Base64Binary {
fn serialize_bytes(
&self,
_helper: &mut SerializeHelper,
) -> Result<Option<Cow<'_, str>>, Error> {
let base64_string = general_purpose::STANDARD.encode(&self.0);
Ok(Some(Cow::Owned(base64_string)))
}
}

#[cfg(feature = "quick-xml")]
impl DeserializeBytes for Base64Binary {
fn deserialize_bytes(_helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
let inner = general_purpose::STANDARD
.decode(bytes)
.map_err(Error::custom)?;
Ok(Self(inner))
}
}

#[cfg(feature = "serde")]
impl serde::Serialize for Base64Binary {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let base64_string = general_purpose::STANDARD.encode(&self.0);
serializer.serialize_str(&base64_string)
}
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Base64Binary {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let base64_string = String::deserialize(deserializer)?;
let bytes = general_purpose::STANDARD
.decode(base64_string.as_bytes())
.map_err(|e| serde::de::Error::custom(format!("Invalid base64 string: {}", e)))?;
Ok(Self(bytes))
}
}
Loading
Loading