-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
108 additions
and
155 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,56 +1,87 @@ | ||
#![allow(clippy::use_self)] | ||
use serde::{de::DeserializeOwned, Serialize}; | ||
use std::io::Read; | ||
|
||
use proc_macro2::{Ident, Span, TokenStream}; | ||
use quote::{quote, ToTokens, TokenStreamExt}; | ||
use strum_macros::Display; | ||
|
||
#[derive(Copy, Clone, Debug, Display, Eq, PartialEq)] | ||
pub enum SerdeFormat { | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
Bincode, | ||
#[cfg(feature = "__serde_cbor")] | ||
Cbor, | ||
#[cfg(feature = "__serde_cbor4ii")] | ||
Cbor4ii, | ||
} | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
const BYTE_LIMIT: u64 = 1024 * 1024 * 1024; | ||
|
||
#[allow(clippy::vec_init_then_push)] | ||
#[must_use] | ||
pub fn serde_format() -> SerdeFormat { | ||
pub fn as_feature() -> &'static str { | ||
let mut formats = vec![]; | ||
|
||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
formats.push(SerdeFormat::Bincode); | ||
formats.push("serde_bincode"); | ||
|
||
#[cfg(feature = "__serde_cbor")] | ||
formats.push(SerdeFormat::Cbor); | ||
formats.push("serde_cbor"); | ||
|
||
#[cfg(feature = "__serde_cbor4ii")] | ||
formats.push(SerdeFormat::Cbor4ii); | ||
formats.push("serde_cbor4ii"); | ||
|
||
assert!( | ||
formats.len() <= 1, | ||
"{}", | ||
"Multiple serde formats selected: {formats:?}" | ||
); | ||
|
||
formats.pop().expect("No serde format selected") | ||
} | ||
|
||
impl SerdeFormat { | ||
#[must_use] | ||
pub fn as_feature(self) -> &'static str { | ||
match self { | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
Self::Bincode => "serde_bincode", | ||
#[cfg(feature = "__serde_cbor")] | ||
Self::Cbor => "serde_cbor", | ||
#[cfg(feature = "__serde_cbor4ii")] | ||
Self::Cbor4ii => "serde_cbor4ii", | ||
} | ||
} | ||
#[must_use] | ||
pub fn serializes_variant_names() -> bool { | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
return false; | ||
|
||
#[cfg(feature = "__serde_cbor")] | ||
return true; | ||
|
||
#[cfg(feature = "__serde_cbor4ii")] | ||
return true; | ||
} | ||
|
||
pub fn serialize<T: Serialize>(args: &T) -> Vec<u8> { | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
return { | ||
use bincode::Options; | ||
// smoelius: From | ||
// https://github.com/bincode-org/bincode/blob/c44b5e364e7084cdbabf9f94b63a3c7f32b8fb68/src/lib.rs#L102-L103 : | ||
// /// **Warning:** the default configuration used by [`bincode::serialize`] is not | ||
// /// the same as that used by the `DefaultOptions` struct. ... | ||
// The point is that `bincode::serialize(..)` and `bincode::options().serialize(..)` use | ||
// different encodings, even though the latter uses "default" options. | ||
bincode::options() | ||
.with_limit(BYTE_LIMIT) | ||
.serialize(args) | ||
.unwrap() | ||
}; | ||
|
||
#[cfg(feature = "__serde_cbor")] | ||
return serde_cbor::to_vec(args).unwrap(); | ||
|
||
#[cfg(feature = "__serde_cbor4ii")] | ||
return { | ||
let mut data = Vec::new(); | ||
cbor4ii::serde::to_writer(&mut data, args).unwrap(); | ||
data | ||
}; | ||
} | ||
|
||
impl ToTokens for SerdeFormat { | ||
fn to_tokens(&self, tokens: &mut TokenStream) { | ||
let ident = Ident::new(&self.to_string(), Span::call_site()); | ||
tokens.append_all(quote! { | ||
test_fuzz::SerdeFormat::#ident | ||
}); | ||
} | ||
pub fn deserialize<T: DeserializeOwned, R: Read>(reader: R) -> Option<T> { | ||
#[cfg(any(serde_default, feature = "__serde_bincode"))] | ||
return { | ||
use bincode::Options; | ||
bincode::options() | ||
.with_limit(BYTE_LIMIT) | ||
.deserialize_from(reader) | ||
.ok() | ||
}; | ||
|
||
#[cfg(feature = "__serde_cbor")] | ||
return serde_cbor::from_reader(reader).ok(); | ||
|
||
#[cfg(feature = "__serde_cbor4ii")] | ||
return { | ||
let reader = std::io::BufReader::new(reader); | ||
cbor4ii::serde::from_reader(reader).ok() | ||
}; | ||
} |
This file contains 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 was deleted.
Oops, something went wrong.
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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
Oops, something went wrong.