Skip to content

Commit

Permalink
Make serde and ark-serialize optional with feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
jbearer committed Nov 16, 2022
1 parent 848e2b4 commit 670033b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ jobs:
with:
token: ${{ github.token }}

name: Clippy without default features
uses: actions-rs/clippy-check@v1
with:
token: ${{ github.token }}
args: --no-default-features

- name: Audit
uses: actions-rs/audit-check@v1
with:
Expand Down
10 changes: 6 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ name = "tagged-base64"
required-features = ["build-cli"]

[features]
default = []
default = ["ark-serialize", "serde"]
ark-serialize = ["dep:ark-serialize"]
serde = ["dep:serde", "tagged-base64-macros/serde"]
wasm-debug = ["dep:console_error_panic_hook"]
build-cli = ["dep:clap"]

[dependencies]
ark-serialize = { version = "0.3.0", default-features = false, features = ["derive"] }
ark-serialize = { version = "0.3.0", optional = true, default-features = false, features = ["derive"] }
base64 = "0.13.0"
crc-any = { version = "2.4.1", default-features = false }
serde = { version = "1.0", features = ["derive"] }
serde = { version = "1.0", optional = true, features = ["derive"] }
snafu = { version = "0.7", features = ["backtraces"] }
tagged-base64-macros = { path = "macros" }
tagged-base64-macros = { path = "macros", default-features = false }

# Command line argument processing
clap = { version = "4.0", optional = true, features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ edition = "2018"
[lib]
proc-macro = true

[features]
default-features = ["serde"]
serde = []

[dependencies]
ark-std = { version = "0.3.0", default-features = false }
syn = { version = "1.0", features = ["extra-traits"] }
Expand Down
10 changes: 9 additions & 1 deletion macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,21 @@ pub fn tagged(args: TokenStream, input: TokenStream) -> TokenStream {
x
),
};
let output = quote! {

#[cfg(feature = "serde")]
let struct_def = quote! {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(try_from = "tagged_base64::TaggedBase64", into = "tagged_base64::TaggedBase64")]
// Override the inferred bound for Serialize/Deserialize impls. If we're converting to and
// from CanonicalBytes as an intermediate, the impls should work for any generic parameters.
#[serde(bound = "")]
#input
};
#[cfg(not(feature = "serde"))]
let struct_def = &input;

let output = quote! {
#struct_def

impl #impl_generics tagged_base64::Tagged for #name #ty_generics #where_clause {
fn tag() -> ark_std::string::String {
Expand Down
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
//! well as display and input in a user interface.
#![allow(clippy::unused_unit)]
#[cfg(feature = "ark-serialize")]
use ark_serialize::*;
use core::fmt;
#[cfg(target_arch = "wasm32")]
use core::fmt::Display;
use core::str::FromStr;
use crc_any::CRC;
#[cfg(feature = "serde")]
use serde::{
de::{Deserialize, Deserializer, Error as DeError},
ser::{Error as SerError, Serialize, Serializer},
Expand All @@ -68,13 +70,18 @@ pub const TB64_CONFIG: base64::Config = base64::URL_SAFE_NO_PAD;
/// A structure holding a string tag, vector of bytes, and a checksum
/// covering the tag and the bytes.
#[cfg_attr(target_arch = "wasm32", wasm_bindgen)]
#[derive(Clone, Debug, Eq, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(
feature = "ark-serialize",
derive(CanonicalSerialize, CanonicalDeserialize)
)]
pub struct TaggedBase64 {
tag: String,
value: Vec<u8>,
checksum: u8,
}

#[cfg(feature = "serde")]
impl Serialize for TaggedBase64 {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -93,6 +100,7 @@ impl Serialize for TaggedBase64 {
}
}

#[cfg(feature = "serde")]
impl<'a> Deserialize<'a> for TaggedBase64 {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down

0 comments on commit 670033b

Please sign in to comment.