Skip to content

Commit

Permalink
Add debug optional feature for sewup, sewup-derive (#267)
Browse files Browse the repository at this point in the history
* Add debug trait for Protocol, Wrapper in rdb feature
* Use Debug of Protocol in RDB example
* Let debug trait optional for sewup
* Enable debug feature in examples
  • Loading branch information
yanganto committed Dec 7, 2021
1 parent b2c3f8d commit 50cdf97
Show file tree
Hide file tree
Showing 14 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/ballot-contract/Cargo.toml
Expand Up @@ -9,7 +9,7 @@ path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
sewup = { version = "*", path = "../../sewup", features = [ "kv" ] }
sewup = { version = "*", path = "../../sewup", features = [ "kv", "debug" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "kv" ] }
hex = "0.4.3"
serde = "1.0"
Expand Down
2 changes: 1 addition & 1 deletion examples/kv-contract/Cargo.toml
Expand Up @@ -19,7 +19,7 @@ anyhow = "1.0.40"
serde = "1.0"
serde_derive = "1.0"

sewup = { version = "*", path = "../../sewup", features = [ "kv" ] }
sewup = { version = "*", path = "../../sewup", features = [ "kv", "debug" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "kv" ] }
thiserror = "1.0.24"

Expand Down
4 changes: 2 additions & 2 deletions examples/rdb-contract/Cargo.toml
Expand Up @@ -19,8 +19,8 @@ anyhow = "1.0.40"
serde = "1.0"
serde_derive = "1.0"

sewup ={ version = "*", path = "../../sewup", features = [ "rdb" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "rdb" ] }
sewup ={ version = "*", path = "../../sewup", features = [ "rdb", "debug" ] }
sewup-derive = { version = "*", path = "../../sewup-derive", features = [ "rdb", "debug" ] }
thiserror = "1.0.24"
serde-value = "0.7.0"
paste = "1.0"
Expand Down
5 changes: 5 additions & 0 deletions examples/rdb-contract/src/lib.rs
Expand Up @@ -336,6 +336,11 @@ mod tests {
let create_input = person::protocol(person.clone());
let mut expect_output = create_input.clone();
expect_output.set_id(1);

// debug trait will auto enabled in test, if you need debug trait in contrait, please
// provide a debug feature when building contract
println!("{:?}", expect_output);

ewasm_auto_assert_eq!(person::create(create_input), expect_output);

ewasm_assert_ok!(add_address_table());
Expand Down
1 change: 1 addition & 0 deletions sewup-derive/Cargo.toml
Expand Up @@ -36,6 +36,7 @@ kv = []
rdb = []
token = []
test = ["rdb", "kv"]
debug = []

[package.metadata.docs.rs]
all-features = true
2 changes: 2 additions & 0 deletions sewup-derive/src/lib.rs
Expand Up @@ -817,6 +817,7 @@ pub fn derive_table(item: TokenStream) -> TokenStream {
let mut output = quote!(
impl sewup::rdb::traits::Record for #struct_name {}

#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(Clone, sewup::Serialize, sewup::Deserialize)]
pub struct #protocol_name {
pub select_fields: Option<std::collections::HashSet::<String>>,
Expand Down Expand Up @@ -906,6 +907,7 @@ pub fn derive_table(item: TokenStream) -> TokenStream {
pub const DELETE_SIG: [u8; 4] = ewasm_fn_sig!(#struct_name::delete());
}

#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(Default, Clone, sewup::Serialize, sewup::Deserialize)]
pub struct #wrapper_name {
#(pub #wrapper_field_names: #wrapper_field_types,)*
Expand Down
1 change: 1 addition & 0 deletions sewup/Cargo.toml
Expand Up @@ -39,6 +39,7 @@ default = [ ]
token = [ ]
kv = []
rdb = []
debug = []

[package.metadata.docs.rs]
all-features = true
3 changes: 2 additions & 1 deletion sewup/src/kv/mod.rs
Expand Up @@ -29,7 +29,8 @@
//! bucket2.set(b"long".into(), "Testing".to_string().into());
//! ```

#[derive(Debug, PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(PartialEq)]
pub enum Feature {
Default = 1,
}
Expand Down
3 changes: 2 additions & 1 deletion sewup/src/rdb/db.rs
Expand Up @@ -25,7 +25,8 @@ const CONFIG_ADDR: [u8; 32] = [0; 32];
pub(crate) type TableSig = [u8; 4];

/// Metadata of table
#[derive(Serialize, Deserialize, Clone, Default, Debug, PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(Serialize, Deserialize, Clone, Default, PartialEq)]
pub struct TableInfo {
pub(crate) sig: TableSig,
pub range: Range<u32>,
Expand Down
3 changes: 2 additions & 1 deletion sewup/src/rdb/mod.rs
@@ -1,7 +1,8 @@
//! `rdb` feature provides a simple way to store things with relations into ethereum runtime.

/// DB feature flag to enable the different feature for db
#[derive(Debug, PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(PartialEq)]
pub enum Feature {
Default = 1,
}
Expand Down
6 changes: 3 additions & 3 deletions sewup/src/runtimes/handler.rs
Expand Up @@ -2,7 +2,6 @@
//! The handler helps you deploy contract and test the contract you developing
use std::cell::RefCell;
use std::convert::TryInto;
use std::fmt;
use std::fs::read;
use std::sync::Arc;

Expand All @@ -23,8 +22,9 @@ pub struct ContractHandler {
pub rt: Option<Arc<RefCell<dyn RT>>>,
}

impl fmt::Debug for ContractHandler {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(any(feature = "debug", test))]
impl std::fmt::Debug for ContractHandler {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ContractHandler")
.field("call_data", &self.call_data)
.field("rt", &self.rt.is_some())
Expand Down
10 changes: 6 additions & 4 deletions sewup/src/runtimes/traits.rs
Expand Up @@ -61,21 +61,23 @@ pub enum VmError {
}

// TODO: abstract this, such that this can suitable for other chain than ETH
#[derive(Debug, Default)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(Default)]
pub struct VMResult {
pub(crate) gas_left: i64,
pub output_data: Vec<u8>,
pub(crate) create_address: Option<Raw>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(PartialEq)]
pub enum Flags {
Default = 0,
Static = 1,
}

// TODO: abstract this, such that this can suitable for other chain than ETH
#[derive(Debug)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
pub struct VMMessage<'a> {
pub kind: evmc_call_kind,
pub flags: Flags,
Expand All @@ -89,7 +91,7 @@ pub struct VMMessage<'a> {
pub create2_salt: Option<()>,
}

#[derive(Debug)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
pub struct VMMessageBuilder<'a> {
pub kind: evmc_call_kind,
pub flags: Flags,
Expand Down
3 changes: 2 additions & 1 deletion sewup/src/types/address.rs
Expand Up @@ -11,7 +11,8 @@ pub use ewasm_api::types::{Address as EwasmAddress, Bytes20};
use crate::types::Raw;

#[cfg(not(target_arch = "wasm32"))]
#[derive(Clone, PartialEq, Default, Debug)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(Clone, PartialEq, Default)]
pub struct AddressType {
pub inner: [u8; 20],
}
Expand Down
9 changes: 5 additions & 4 deletions sewup/src/types/raw.rs
@@ -1,6 +1,5 @@
use std::{
convert::{TryFrom, TryInto},
fmt,
iter::FromIterator,
};

Expand All @@ -24,7 +23,8 @@ pub struct Raw {
// flag: u8,
}

#[derive(DeserializeDerive, Debug, PartialEq)]
#[cfg_attr(any(feature = "debug", test), derive(Debug))]
#[derive(DeserializeDerive, PartialEq)]
struct RawHelper {
e01: u8,
e02: u8,
Expand Down Expand Up @@ -471,8 +471,9 @@ impl PartialEq<[u8]> for Raw {

impl Eq for Raw {}

impl fmt::Debug for Raw {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
#[cfg(any(feature = "debug", test))]
impl std::fmt::Debug for Raw {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.as_ref().fmt(f)
}
}

0 comments on commit 50cdf97

Please sign in to comment.