Skip to content
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

Add BorshSchema #56

Merged
merged 7 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions borsh-rs/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions borsh-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ members = [
"borsh",
"borsh-derive",
"borsh-derive-internal",
"borsh-schema-derive-internal",
"benchmarks",
]
exclude = [
Expand Down
2 changes: 1 addition & 1 deletion borsh-rs/borsh-derive-internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive-internal"
version = "0.2.10"
version = "0.3.0"
authors = ["Near Inc <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand Down
5 changes: 3 additions & 2 deletions borsh-rs/borsh-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "borsh-derive"
version = "0.2.10"
version = "0.3.0"
authors = ["Near Inc <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
Expand All @@ -16,6 +16,7 @@ Binary Object Representation Serializer for Hashing
proc-macro = true

[dependencies]
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.2.10"}
borsh-derive-internal = { path = "../borsh-derive-internal" , version="0.3.0"}
borsh-schema-derive-internal = { path = "../borsh-schema-derive-internal" , version="0.3.0"}
syn = {version = "1", features = ["full", "fold"] }

24 changes: 23 additions & 1 deletion borsh-rs/borsh-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
extern crate proc_macro;

use borsh_derive_internal::*;
use borsh_schema_derive_internal::*;
use proc_macro::TokenStream;
use syn::export::Span;
use syn::{ItemEnum, ItemStruct, ItemUnion};

#[proc_macro_derive(BorshSerialize, attributes(borsh_skip))]
Expand Down Expand Up @@ -39,3 +40,24 @@ pub fn borsh_deserialize(input: TokenStream) -> TokenStream {
Err(err) => err.to_compile_error(),
})
}

#[proc_macro_derive(BorshSchema, attributes(borsh_skip))]
pub fn borsh_schema(input: TokenStream) -> TokenStream {
let res = if let Ok(input) = syn::parse::<ItemStruct>(input.clone()) {
process_struct(&input)
} else if let Ok(input) = syn::parse::<ItemEnum>(input.clone()) {
process_enum(&input)
} else if let Ok(_) = syn::parse::<ItemUnion>(input.clone()) {
Err(syn::Error::new(
Span::call_site(),
"Borsh schema does not support unions yet.",
))
} else {
// Derive macros can only be defined on structs, enums, and unions.
unreachable!()
};
TokenStream::from(match res {
Ok(res) => res,
Err(err) => err.to_compile_error(),
})
}
17 changes: 17 additions & 0 deletions borsh-rs/borsh-schema-derive-internal/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "borsh-schema-derive-internal"
version = "0.3.0"
authors = ["Near Inc <[email protected]>"]
edition = "2018"
license = "Apache-2.0"
categories = ["encoding", "network-programming"]
repository = "https://github.com/nearprotocol/borsh"
homepage = "http://borsh.io"
description = """
Schema Generator for Borsh
"""

[dependencies]
proc-macro2 = "1"
syn = {version = "1", features = ["full", "fold"] }
quote = "1"
Loading