Skip to content

Commit

Permalink
put macros and traits of each type behind individual feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
knickish committed Aug 25, 2023
1 parent 8504a42 commit 04fee76
Show file tree
Hide file tree
Showing 12 changed files with 102 additions and 16 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,33 @@ jobs:
command: build
args: --all-targets --target=${{ matrix.config.target }}

features:
name: Test Individual Features
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
feature: [
"binary",
"json",
"ron",
"toml",
"no_std"
]

steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
toolchain: stable
target: x86_64-unknown-linux-gnu
override: true

- uses: actions-rs/cargo@v1
with:
command: test
args: --no-default-features --features ${{ matrix.feature }}

test:
name: Test
runs-on: ${{ matrix.config.os }}
Expand Down
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ edition = "2018"
repository = "https://github.com/not-fl3/nanoserde"

[features]
default = []
default = ["json", "binary", "ron", "toml"]
json = ["nanoserde-derive/json"]
binary = ["nanoserde-derive/binary"]
ron = ["nanoserde-derive/ron"]
toml = []
no_std = ["dep:hashbrown"]

[dependencies]
Expand Down
4 changes: 4 additions & 0 deletions derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ proc-macro = true

[features]
default = []
json = []
binary = []
ron = []
toml = []
no_std = ["dep:hashbrown"]

[dependencies]
Expand Down
16 changes: 14 additions & 2 deletions derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
#![cfg_attr(features = "no_std", no_std)]
#![cfg(any(feature = "binary", feature = "json", feature = "ron"))]

extern crate alloc;
extern crate proc_macro;

#[macro_use]
mod shared;

#[cfg(feature = "binary")]
mod serde_bin;
#[cfg(feature = "binary")]
use crate::serde_bin::*;

#[cfg(feature = "ron")]
mod serde_ron;
#[cfg(feature = "ron")]
use crate::serde_ron::*;

#[cfg(feature = "json")]
mod serde_json;
#[cfg(feature = "json")]
use crate::serde_json::*;

mod parse;

use crate::serde_json::*;

#[cfg(feature = "binary")]
#[proc_macro_derive(SerBin, attributes(nserde))]
pub fn derive_ser_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand All @@ -37,6 +44,7 @@ pub fn derive_ser_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream
ts
}

#[cfg(feature = "binary")]
#[proc_macro_derive(DeBin, attributes(nserde))]
pub fn derive_de_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand All @@ -57,6 +65,7 @@ pub fn derive_de_bin(input: proc_macro::TokenStream) -> proc_macro::TokenStream
ts
}

#[cfg(feature = "ron")]
#[proc_macro_derive(SerRon, attributes(nserde))]
pub fn derive_ser_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand All @@ -76,6 +85,7 @@ pub fn derive_ser_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream
ts
}

#[cfg(feature = "ron")]
#[proc_macro_derive(DeRon, attributes(nserde))]
pub fn derive_de_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand All @@ -95,6 +105,7 @@ pub fn derive_de_ron(input: proc_macro::TokenStream) -> proc_macro::TokenStream
ts
}

#[cfg(feature = "json")]
#[proc_macro_derive(SerJson, attributes(nserde))]
pub fn derive_ser_json(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand All @@ -114,6 +125,7 @@ pub fn derive_ser_json(input: proc_macro::TokenStream) -> proc_macro::TokenStrea
ts
}

#[cfg(feature = "json")]
#[proc_macro_derive(DeJson, attributes(nserde))]
pub fn derive_de_json(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse::parse_data(input);
Expand Down
3 changes: 3 additions & 0 deletions derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,10 +196,12 @@ impl Generic {
}
}

#[cfg(any(feature = "binary", feature = "json"))]
pub fn ident_only(&self) -> String {
format!("{}{}", self.lifetime_prefix(), self.full())
}

#[cfg(any(feature = "binary", feature = "json"))]
pub fn full_with_const(&self, extra_bounds: &[&str], bounds: bool) -> String {
let bounds = match (bounds, &self) {
(true, Generic::Lifetime { .. }) => self.get_bounds().join(" + "),
Expand Down Expand Up @@ -414,6 +416,7 @@ impl Category {
}

impl Type {
#[cfg(any(feature = "ron", feature = "json"))]
pub fn base(&self) -> String {
let mut base = match &self.ref_type {
Some(inner) => match inner {
Expand Down
8 changes: 8 additions & 0 deletions derive/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::string::String;

#[cfg(any(feature = "binary", feature = "json"))]
use crate::parse::{Enum, Struct};

macro_rules! l {
Expand All @@ -22,6 +23,7 @@ pub fn attrs_proxy(attributes: &[crate::parse::Attribute]) -> Option<String> {
})
}

#[cfg(any(feature = "ron", feature = "json"))]
pub fn attrs_rename(attributes: &[crate::parse::Attribute]) -> Option<String> {
attributes.iter().find_map(|attr| {
if attr.tokens.len() == 2 && attr.tokens[0] == "rename" {
Expand All @@ -32,6 +34,7 @@ pub fn attrs_rename(attributes: &[crate::parse::Attribute]) -> Option<String> {
})
}

#[cfg(any(feature = "ron", feature = "json"))]
pub fn attrs_default(attributes: &[crate::parse::Attribute]) -> Option<Option<String>> {
attributes.iter().find_map(|attr| {
if attr.tokens.len() == 1 && attr.tokens[0] == "default" {
Expand All @@ -44,6 +47,7 @@ pub fn attrs_default(attributes: &[crate::parse::Attribute]) -> Option<Option<St
})
}

#[cfg(any(feature = "ron", feature = "json"))]
pub fn attrs_default_with(attributes: &[crate::parse::Attribute]) -> Option<String> {
attributes.iter().find_map(|attr| {
if attr.tokens.len() == 2 && attr.tokens[0] == "default_with" {
Expand All @@ -54,18 +58,21 @@ pub fn attrs_default_with(attributes: &[crate::parse::Attribute]) -> Option<Stri
})
}

#[cfg(feature = "json")]
pub fn attrs_transparent(attributes: &[crate::parse::Attribute]) -> bool {
attributes
.iter()
.any(|attr| attr.tokens.len() == 1 && attr.tokens[0] == "transparent")
}

#[cfg(feature = "json")]
pub fn attrs_skip(attributes: &[crate::parse::Attribute]) -> bool {
attributes
.iter()
.any(|attr| attr.tokens.len() == 1 && attr.tokens[0] == "skip")
}

#[cfg(any(feature = "binary", feature = "json"))]
pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (String, String) {
let generics: &Vec<_> = &struct_.generics;

Expand All @@ -90,6 +97,7 @@ pub(crate) fn struct_bounds_strings(struct_: &Struct, bound_name: &str) -> (Stri
return (generic_w_bounds, generic_no_bounds);
}

#[cfg(any(feature = "binary", feature = "json"))]
pub(crate) fn enum_bounds_strings(enum_: &Enum, bound_name: &str) -> (String, String) {
let generics: &Vec<_> = &enum_.generics;

Expand Down
10 changes: 8 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
//!
//! Data serialization library with zero dependencies. No more syn/proc_macro2/quote in the build tree!
//!
//! `nanoserde` also almost do not use any generics, so build size is not going to be bloated with monomorphizated code.
//!
//! The main difference with "serde" and the reason why "nanoserde" is possible: there is no intermediate data model
//! For each serialisation datatype there is a special macro.
//!
Expand All @@ -26,14 +24,22 @@ extern crate alloc;

pub use nanoserde_derive::*;

#[cfg(feature = "binary")]
mod serde_bin;
#[cfg(feature = "binary")]
pub use crate::serde_bin::*;

#[cfg(feature = "ron")]
mod serde_ron;
#[cfg(feature = "ron")]
pub use crate::serde_ron::*;

#[cfg(feature = "json")]
mod serde_json;
#[cfg(feature = "json")]
pub use crate::serde_json::*;

#[cfg(feature = "toml")]
mod toml;
#[cfg(feature = "toml")]
pub use crate::toml::*;
1 change: 1 addition & 0 deletions tests/bin.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "binary")]
use std::collections::{BTreeSet, HashMap, HashSet, LinkedList};

use nanoserde::{DeBin, SerBin};
Expand Down
1 change: 1 addition & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "json")]
use nanoserde::{DeJson, SerJson};

use std::collections::{BTreeSet, HashMap, HashSet, LinkedList};
Expand Down
1 change: 1 addition & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "ron")]
use nanoserde::{DeRon, SerRon};

use std::collections::{BTreeSet, HashMap, HashSet, LinkedList};
Expand Down
40 changes: 29 additions & 11 deletions tests/ser_de.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use nanoserde::{DeBin, DeJson, DeRon, SerBin, SerJson, SerRon};
#![cfg(any(feature = "binary", feature = "json", feature = "ron"))]
#[cfg(feature = "binary")]
use nanoserde::{DeBin, SerBin};
#[cfg(feature = "json")]
use nanoserde::{DeJson, SerJson};
#[cfg(feature = "ron")]
use nanoserde::{DeRon, SerRon};

use std::collections::HashMap;

#[test]
fn ser_de() {
#[derive(DeBin, SerBin, DeJson, SerJson, DeRon, SerRon, PartialEq, Debug)]
#[derive(PartialEq, Debug)]
#[cfg_attr(feature = "binary", derive(DeBin, SerBin))]
#[cfg_attr(feature = "json", derive(DeJson, SerJson))]
#[cfg_attr(feature = "ron", derive(DeRon, SerRon))]
pub struct Test {
pub a: i32,
pub b: f32,
Expand All @@ -28,15 +37,24 @@ fn ser_de() {
g: (),
};

let bytes = SerBin::serialize_bin(&test);
let test_deserialized = DeBin::deserialize_bin(&bytes).unwrap();
assert_eq!(test, test_deserialized);
#[cfg(feature = "binary")]
{
let bytes = SerBin::serialize_bin(&test);
let test_deserialized = DeBin::deserialize_bin(&bytes).unwrap();
assert_eq!(test, test_deserialized);
}

let bytes = SerJson::serialize_json(&test);
let test_deserialized = DeJson::deserialize_json(&bytes).unwrap();
assert_eq!(test, test_deserialized);
#[cfg(feature = "json")]
{
let bytes = SerJson::serialize_json(&test);
let test_deserialized = DeJson::deserialize_json(&bytes).unwrap();
assert_eq!(test, test_deserialized);
}

let bytes = SerRon::serialize_ron(&test);
let test_deserialized = DeRon::deserialize_ron(&bytes).unwrap();
assert_eq!(test, test_deserialized);
#[cfg(feature = "ron")]
{
let bytes = SerRon::serialize_ron(&test);
let test_deserialized = DeRon::deserialize_ron(&bytes).unwrap();
assert_eq!(test, test_deserialized);
}
}
1 change: 1 addition & 0 deletions tests/toml.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![cfg(feature = "toml")]
use nanoserde::TomlParser;

#[test]
Expand Down

0 comments on commit 04fee76

Please sign in to comment.