-
Notifications
You must be signed in to change notification settings - Fork 33
Fix bug checking the length of binary types #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cbcf223
Fix bug checking the length of binary types
p32blo b645c44
Revert previous commit and try new approach
p32blo c7b5f22
Implement HexBinary and Base64Binary correctly and add new test
p32blo bfb009c
Fix include errors
p32blo e3b3edf
Rename files as sugested
p32blo bab2180
Fix PR Comments
p32blo 6e5a1c5
Make HexString and Base64String the default
p32blo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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]; | ||
|
|
||
| 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)) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.