Skip to content

Commit

Permalink
merge derive/macro crates.
Browse files Browse the repository at this point in the history
  • Loading branch information
tensor-programming committed Apr 21, 2021
1 parent 1c5b731 commit 36c8983
Show file tree
Hide file tree
Showing 25 changed files with 118 additions and 242 deletions.
40 changes: 5 additions & 35 deletions .changes/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,11 @@
"packages": {
"utils-derive": {
"path": "./utils/utils-derive/",
"manager": "rust",
"publish": false
"manager": "rust"
},
"runtime": {
"stronghold-runtime": {
"path": "./engine/runtime/",
"manager": "rust",
"publish": false
},
"store": {
"path": "./engine/store/",
"manager": "rust",
"publish": false
"manager": "rust"
},
"communication-macros": {
"path": "./communication/communication-macros",
Expand All @@ -93,35 +86,15 @@
"stronghold-utils": {
"path": "./utils/utils/",
"manager": "rust",
"publish": false,
"dependencies": [
"utils-derive"
]
},
"vault": {
"path": "./engine/vault/",
"manager": "rust",
"publish": false,
"dependencies": [
"stronghold-utils"
]
},
"snapshot": {
"path": "./engine/snapshot/",
"manager": "rust",
"publish": false,
"dependencies": [
"stronghold-utils"
]
},
"stronghold-engine": {
"path": "./engine/",
"manager": "rust",
"dependencies": [
"vault",
"snapshot",
"store",
"runtime"
"stronghold-runtime"
]
},
"stronghold-communication": {
Expand All @@ -137,9 +110,6 @@
"manager": "rust",
"dependencies": [
"stronghold-engine",
"vault",
"snapshot",
"store",
"stronghold-communication",
"stronghold-utils",
"runtime"
Expand All @@ -154,4 +124,4 @@
]
}
}
}
}
Empty file added .changes/restructure.md
Empty file.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ members = [
"engine/runtime",
"client",
"communication",
"utils/utils",
"utils/utils-derive"
"utils",
"derive"
]
exclude = [
"products/commandline",
Expand Down
42 changes: 21 additions & 21 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@ thiserror = "1.0"
futures = "0.3"
riker = "0.4"

[dependencies.stronghold-engine]
path = "../engine"
version = "0.3"

[dependencies.iota-crypto]
version = "0.5.0"
features = [
"random",
"ed25519",
"sha",
"hmac",
"bip39-en",
"slip10",
"chacha"
[dependencies.stronghold-engine]
path = "../engine"
version = "0.3"

[dependencies.iota-crypto]
version = "0.5.0"
features = [
"random",
"ed25519",
"sha",
"hmac",
"bip39-en",
"slip10",
"chacha"
]

[dependencies.stronghold-communication]
path = "../communication"
optional = true
version = "0.4.0"
[dependencies.stronghold-communication]
path = "../communication"
optional = true
version = "0.4.0"

[dependencies.stronghold-utils]
path = "../utils/utils"
version = "0.2"
[dependencies.stronghold-utils]
path = "../utils"
version = "0.2"

[features]
communication = [ "stronghold-communication" ]
Expand Down
8 changes: 4 additions & 4 deletions communication/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ thiserror = "1.0"
serde = { version = "1.0", default-features = false, features = [ "alloc", "derive" ] }
serde_json = { version = "1.0", default-features = false, features = [ "alloc" ] }
riker = "0.4"
communication-macros = { path = "communication-macros", version = "0.1.1" }
stronghold-derive = { path = "../derive", version = "0.1.0" }

[dependencies.stronghold-utils]
path = "../utils/utils"
version = "0.2"
[dependencies.stronghold-utils]
path = "../utils"
version = "0.2"

[features]
default = [ "mdns" ]
Expand Down
15 changes: 0 additions & 15 deletions communication/communication-macros/CHANGELOG.md

This file was deleted.

17 changes: 0 additions & 17 deletions communication/communication-macros/Cargo.toml

This file was deleted.

62 changes: 0 additions & 62 deletions communication/communication-macros/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion communication/src/actor/firewall.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

pub use communication_macros::RequestPermissions;
use libp2p::PeerId;
use std::collections::HashMap;
pub use stronghold_derive::RequestPermissions;

/// The permission value for request variants.
/// It is a bit that is set at a certain index, therefore the value is always a power of 2.
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 4 additions & 3 deletions utils/utils-derive/Cargo.toml → derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "utils-derive"
version = "0.1.1"
name = "stronghold-derive"
version = "0.1.0"
authors = [
"IOTA Stiftung",
"tensor-programming <tensordeveloper@gmail.com>"
Expand All @@ -11,5 +11,6 @@ edition = "2018"
proc-macro = true

[dependencies]
syn = { version = "1.0", features = [ "parsing", "full", "extra-traits", "proc-macro" ] }
quote = "1.0"
syn = "1.0"
proc-macro2 = "1.0"
Original file line number Diff line number Diff line change
@@ -1,51 +1,11 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

extern crate proc_macro;

use proc_macro2::{Ident, TokenStream};
use quote::quote;
use syn::{Data, DataEnum, DeriveInput, Fields};

/// Implements the [`VariantPermission`] for struct/ unions with PermissionValue(1).
/// For enums, it implements [`ToPermissionVariants`], which creates an according new enum <Ident>Permission with Unit
/// variants, and implements [`VariantPermission`] by assigning different [`PermissionValue`] for each variant.
/// The permission value is the "index" in the enum as exponent for the power of 2, thus from top to bottom 1, 2, 4,
/// 8...
#[proc_macro_derive(RequestPermissions)]
pub fn permissions(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let derive_input = syn::parse_macro_input!(input as DeriveInput);
let name = format!("{}Permission", derive_input.ident);
let name = syn::Ident::new(&name, derive_input.ident.span());
let gen = match derive_input.data {
Data::Enum(data_enum) => {
let unit_enum = build_plain(&name, &data_enum);
let impl_permission = impl_permission(&name, &data_enum);
let to_permissioned = impl_to_permissioned(&derive_input.ident, &name, &data_enum);

quote! {
#unit_enum
#impl_permission
#to_permissioned
}
}
Data::Struct(_) | Data::Union(_) => {
let ident = derive_input.ident.clone();
quote! {
impl VariantPermission for #ident {
fn permission(&self) -> PermissionValue {
// Only panics for values > 31.
PermissionValue::new(0).unwrap()
}
}

}
}
};
gen.into()
}
use syn::{DataEnum, Fields};

fn build_plain(name: &Ident, data_enum: &DataEnum) -> TokenStream {
pub fn build_plain(name: &Ident, data_enum: &DataEnum) -> TokenStream {
let plain_variants = data_enum
.variants
.iter()
Expand All @@ -64,15 +24,15 @@ fn build_plain(name: &Ident, data_enum: &DataEnum) -> TokenStream {
}
}

fn match_fields(fields: &Fields) -> TokenStream {
pub fn match_fields(fields: &Fields) -> TokenStream {
match fields {
Fields::Unit => TokenStream::new(),
Fields::Unnamed(..) => quote! { (..) },
Fields::Named(..) => quote! { { .. } },
}
}

fn impl_to_permissioned(input: &Ident, name: &Ident, data_enum: &DataEnum) -> TokenStream {
pub fn impl_to_permissioned(input: &Ident, name: &Ident, data_enum: &DataEnum) -> TokenStream {
let match_variants = data_enum
.variants
.iter()
Expand All @@ -95,7 +55,7 @@ fn impl_to_permissioned(input: &Ident, name: &Ident, data_enum: &DataEnum) -> To
}
}

fn impl_permission(name: &Ident, data_enum: &DataEnum) -> TokenStream {
pub fn impl_permission(name: &Ident, data_enum: &DataEnum) -> TokenStream {
if data_enum.variants.len() > 32 {
panic!("More then 32 variants on enums are not supported.");
}
Expand Down
Loading

0 comments on commit 36c8983

Please sign in to comment.