Skip to content

Commit

Permalink
Add common derives to generated contract types (#759)
Browse files Browse the repository at this point in the history
### What
Add common derives to generated contract types.

### Why
Some of them are required, and all of them are convenient to have on the
data objects/types that are generated by contract imports.

 Close #758
  • Loading branch information
leighmcculloch authored Nov 4, 2022
1 parent e84e3cd commit 5f942ac
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
10 changes: 5 additions & 5 deletions 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 soroban-sdk/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ mod contract_udt_struct;
mod contract_udt_struct_tuple;
mod contractfile_with_sha256;
mod contractimport;
mod contractimport_with_error;
mod contractimport_with_sha256;
31 changes: 31 additions & 0 deletions soroban-sdk/src/tests/contractimport_with_error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate as soroban_sdk;
use soroban_sdk::{contractimpl, symbol, BytesN, Env, Symbol};

mod errcontract {
use crate as soroban_sdk;
soroban_sdk::contractimport!(
file = "../target/wasm32-unknown-unknown/release/test_errors.wasm"
);
}

pub struct Contract;

#[contractimpl]
impl Contract {
pub fn hello_with(env: Env, contract_id: BytesN<32>, flag: u32) -> Symbol {
errcontract::Client::new(&env, &contract_id).hello(&flag)
}
}

#[test]
fn test_functional() {
let e = Env::default();

let err_contract_id = e.register_contract_wasm(None, errcontract::WASM);

let contract_id = e.register_contract(None, Contract);
let client = ContractClient::new(&e, &contract_id);

let z = client.hello_with(&err_contract_id, &0);
assert!(z == symbol!("hello"));
}
4 changes: 4 additions & 0 deletions soroban-spec/src/gen/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,25 @@ pub trait Contract {
fn add(env: soroban_sdk::Env, a: UdtEnum, b: UdtEnum) -> i64;
}
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct UdtTuple(pub i64, pub soroban_sdk::Vec<i64>);
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct UdtStruct {
pub a: i64,
pub b: i64,
pub c: soroban_sdk::Vec<i64>,
}
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum UdtEnum {
UdtA,
UdtB(UdtStruct),
UdtC(UdtEnum2),
UdtD(UdtTuple),
}
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum UdtEnum2 {
A = 10,
B = 15,
Expand Down
5 changes: 5 additions & 0 deletions soroban-spec/src/gen/rust/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn generate_struct(spec: &ScSpecUdtStructV0) -> TokenStream {
});
quote! {
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct #ident ( #(#fields),* );
}
} else {
Expand All @@ -42,6 +43,7 @@ pub fn generate_struct(spec: &ScSpecUdtStructV0) -> TokenStream {
});
quote! {
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub struct #ident { #(#fields,)* }
}
}
Expand All @@ -68,6 +70,7 @@ pub fn generate_union(spec: &ScSpecUdtUnionV0) -> TokenStream {
});
quote! {
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum #ident { #(#variants,)* }
}
}
Expand All @@ -90,6 +93,7 @@ pub fn generate_enum(spec: &ScSpecUdtEnumV0) -> TokenStream {
});
quote! {
#[soroban_sdk::contracttype(export = false)]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum #ident { #(#variants,)* }
}
}
Expand All @@ -112,6 +116,7 @@ pub fn generate_error_enum(spec: &ScSpecUdtErrorEnumV0) -> TokenStream {
});
quote! {
#[soroban_sdk::contracterror(export = false)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum #ident { #(#variants,)* }
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/errors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "test_result"
name = "test_errors"
version = "0.2.0"
authors = ["Stellar Development Foundation <[email protected]>"]
license = "Apache-2.0"
Expand Down

0 comments on commit 5f942ac

Please sign in to comment.