Skip to content

Commit 5f942ac

Browse files
Add common derives to generated contract types (#759)
### 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
1 parent e84e3cd commit 5f942ac

6 files changed

Lines changed: 47 additions & 6 deletions

File tree

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

soroban-sdk/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ mod contract_udt_struct;
1111
mod contract_udt_struct_tuple;
1212
mod contractfile_with_sha256;
1313
mod contractimport;
14+
mod contractimport_with_error;
1415
mod contractimport_with_sha256;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
use crate as soroban_sdk;
2+
use soroban_sdk::{contractimpl, symbol, BytesN, Env, Symbol};
3+
4+
mod errcontract {
5+
use crate as soroban_sdk;
6+
soroban_sdk::contractimport!(
7+
file = "../target/wasm32-unknown-unknown/release/test_errors.wasm"
8+
);
9+
}
10+
11+
pub struct Contract;
12+
13+
#[contractimpl]
14+
impl Contract {
15+
pub fn hello_with(env: Env, contract_id: BytesN<32>, flag: u32) -> Symbol {
16+
errcontract::Client::new(&env, &contract_id).hello(&flag)
17+
}
18+
}
19+
20+
#[test]
21+
fn test_functional() {
22+
let e = Env::default();
23+
24+
let err_contract_id = e.register_contract_wasm(None, errcontract::WASM);
25+
26+
let contract_id = e.register_contract(None, Contract);
27+
let client = ContractClient::new(&e, &contract_id);
28+
29+
let z = client.hello_with(&err_contract_id, &0);
30+
assert!(z == symbol!("hello"));
31+
}

soroban-spec/src/gen/rust.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,21 +133,25 @@ pub trait Contract {
133133
fn add(env: soroban_sdk::Env, a: UdtEnum, b: UdtEnum) -> i64;
134134
}
135135
#[soroban_sdk::contracttype(export = false)]
136+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
136137
pub struct UdtTuple(pub i64, pub soroban_sdk::Vec<i64>);
137138
#[soroban_sdk::contracttype(export = false)]
139+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
138140
pub struct UdtStruct {
139141
pub a: i64,
140142
pub b: i64,
141143
pub c: soroban_sdk::Vec<i64>,
142144
}
143145
#[soroban_sdk::contracttype(export = false)]
146+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
144147
pub enum UdtEnum {
145148
UdtA,
146149
UdtB(UdtStruct),
147150
UdtC(UdtEnum2),
148151
UdtD(UdtTuple),
149152
}
150153
#[soroban_sdk::contracttype(export = false)]
154+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
151155
pub enum UdtEnum2 {
152156
A = 10,
153157
B = 15,

soroban-spec/src/gen/rust/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn generate_struct(spec: &ScSpecUdtStructV0) -> TokenStream {
3131
});
3232
quote! {
3333
#[soroban_sdk::contracttype(export = false)]
34+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
3435
pub struct #ident ( #(#fields),* );
3536
}
3637
} else {
@@ -42,6 +43,7 @@ pub fn generate_struct(spec: &ScSpecUdtStructV0) -> TokenStream {
4243
});
4344
quote! {
4445
#[soroban_sdk::contracttype(export = false)]
46+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
4547
pub struct #ident { #(#fields,)* }
4648
}
4749
}
@@ -68,6 +70,7 @@ pub fn generate_union(spec: &ScSpecUdtUnionV0) -> TokenStream {
6870
});
6971
quote! {
7072
#[soroban_sdk::contracttype(export = false)]
73+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
7174
pub enum #ident { #(#variants,)* }
7275
}
7376
}
@@ -90,6 +93,7 @@ pub fn generate_enum(spec: &ScSpecUdtEnumV0) -> TokenStream {
9093
});
9194
quote! {
9295
#[soroban_sdk::contracttype(export = false)]
96+
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
9397
pub enum #ident { #(#variants,)* }
9498
}
9599
}
@@ -112,6 +116,7 @@ pub fn generate_error_enum(spec: &ScSpecUdtErrorEnumV0) -> TokenStream {
112116
});
113117
quote! {
114118
#[soroban_sdk::contracterror(export = false)]
119+
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
115120
pub enum #ident { #(#variants,)* }
116121
}
117122
}

tests/errors/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "test_result"
2+
name = "test_errors"
33
version = "0.2.0"
44
authors = ["Stellar Development Foundation <info@stellar.org>"]
55
license = "Apache-2.0"

0 commit comments

Comments
 (0)