|
1 | | -#![allow(clippy::use_self)] |
| 1 | +use serde::{de::DeserializeOwned, Serialize}; |
| 2 | +use std::io::Read; |
2 | 3 |
|
3 | | -use proc_macro2::{Ident, Span, TokenStream}; |
4 | | -use quote::{quote, ToTokens, TokenStreamExt}; |
5 | | -use strum_macros::Display; |
6 | | - |
7 | | -#[derive(Copy, Clone, Debug, Display, Eq, PartialEq)] |
8 | | -pub enum SerdeFormat { |
9 | | - #[cfg(any(serde_default, feature = "__serde_bincode"))] |
10 | | - Bincode, |
11 | | - #[cfg(feature = "__serde_cbor")] |
12 | | - Cbor, |
13 | | - #[cfg(feature = "__serde_cbor4ii")] |
14 | | - Cbor4ii, |
15 | | -} |
| 4 | +#[cfg(any(serde_default, feature = "__serde_bincode"))] |
| 5 | +const BYTE_LIMIT: u64 = 1024 * 1024 * 1024; |
16 | 6 |
|
17 | 7 | #[allow(clippy::vec_init_then_push)] |
18 | 8 | #[must_use] |
19 | | -pub fn serde_format() -> SerdeFormat { |
| 9 | +pub fn as_feature() -> &'static str { |
20 | 10 | let mut formats = vec![]; |
| 11 | + |
21 | 12 | #[cfg(any(serde_default, feature = "__serde_bincode"))] |
22 | | - formats.push(SerdeFormat::Bincode); |
| 13 | + formats.push("serde_bincode"); |
| 14 | + |
23 | 15 | #[cfg(feature = "__serde_cbor")] |
24 | | - formats.push(SerdeFormat::Cbor); |
| 16 | + formats.push("serde_cbor"); |
| 17 | + |
25 | 18 | #[cfg(feature = "__serde_cbor4ii")] |
26 | | - formats.push(SerdeFormat::Cbor4ii); |
| 19 | + formats.push("serde_cbor4ii"); |
| 20 | + |
27 | 21 | assert!( |
28 | 22 | formats.len() <= 1, |
29 | 23 | "{}", |
30 | 24 | "Multiple serde formats selected: {formats:?}" |
31 | 25 | ); |
| 26 | + |
32 | 27 | formats.pop().expect("No serde format selected") |
33 | 28 | } |
34 | 29 |
|
35 | | -impl SerdeFormat { |
36 | | - #[must_use] |
37 | | - pub fn as_feature(self) -> &'static str { |
38 | | - match self { |
39 | | - #[cfg(any(serde_default, feature = "__serde_bincode"))] |
40 | | - Self::Bincode => "serde_bincode", |
41 | | - #[cfg(feature = "__serde_cbor")] |
42 | | - Self::Cbor => "serde_cbor", |
43 | | - #[cfg(feature = "__serde_cbor4ii")] |
44 | | - Self::Cbor4ii => "serde_cbor4ii", |
45 | | - } |
46 | | - } |
| 30 | +#[must_use] |
| 31 | +pub fn serializes_variant_names() -> bool { |
| 32 | + #[cfg(any(serde_default, feature = "__serde_bincode"))] |
| 33 | + return false; |
| 34 | + |
| 35 | + #[cfg(feature = "__serde_cbor")] |
| 36 | + return true; |
| 37 | + |
| 38 | + #[cfg(feature = "__serde_cbor4ii")] |
| 39 | + return true; |
| 40 | +} |
| 41 | + |
| 42 | +pub fn serialize<T: Serialize>(args: &T) -> Vec<u8> { |
| 43 | + #[cfg(any(serde_default, feature = "__serde_bincode"))] |
| 44 | + return { |
| 45 | + use bincode::Options; |
| 46 | + // smoelius: From |
| 47 | + // https://github.com/bincode-org/bincode/blob/c44b5e364e7084cdbabf9f94b63a3c7f32b8fb68/src/lib.rs#L102-L103 : |
| 48 | + // /// **Warning:** the default configuration used by [`bincode::serialize`] is not |
| 49 | + // /// the same as that used by the `DefaultOptions` struct. ... |
| 50 | + // The point is that `bincode::serialize(..)` and `bincode::options().serialize(..)` use |
| 51 | + // different encodings, even though the latter uses "default" options. |
| 52 | + bincode::options() |
| 53 | + .with_limit(BYTE_LIMIT) |
| 54 | + .serialize(args) |
| 55 | + .unwrap() |
| 56 | + }; |
| 57 | + |
| 58 | + #[cfg(feature = "__serde_cbor")] |
| 59 | + return serde_cbor::to_vec(args).unwrap(); |
| 60 | + |
| 61 | + #[cfg(feature = "__serde_cbor4ii")] |
| 62 | + return { |
| 63 | + let mut data = Vec::new(); |
| 64 | + cbor4ii::serde::to_writer(&mut data, args).unwrap(); |
| 65 | + data |
| 66 | + }; |
47 | 67 | } |
48 | 68 |
|
49 | | -impl ToTokens for SerdeFormat { |
50 | | - fn to_tokens(&self, tokens: &mut TokenStream) { |
51 | | - let ident = Ident::new(&self.to_string(), Span::call_site()); |
52 | | - tokens.append_all(quote! { |
53 | | - test_fuzz::SerdeFormat::#ident |
54 | | - }); |
55 | | - } |
| 69 | +pub fn deserialize<T: DeserializeOwned, R: Read>(reader: R) -> Option<T> { |
| 70 | + #[cfg(any(serde_default, feature = "__serde_bincode"))] |
| 71 | + return { |
| 72 | + use bincode::Options; |
| 73 | + bincode::options() |
| 74 | + .with_limit(BYTE_LIMIT) |
| 75 | + .deserialize_from(reader) |
| 76 | + .ok() |
| 77 | + }; |
| 78 | + |
| 79 | + #[cfg(feature = "__serde_cbor")] |
| 80 | + return serde_cbor::from_reader(reader).ok(); |
| 81 | + |
| 82 | + #[cfg(feature = "__serde_cbor4ii")] |
| 83 | + return { |
| 84 | + let reader = std::io::BufReader::new(reader); |
| 85 | + cbor4ii::serde::from_reader(reader).ok() |
| 86 | + }; |
56 | 87 | } |
0 commit comments