Skip to content

Commit 61d593b

Browse files
authored
Merge pull request #26 from yajamon/builder_macro
Builder macroつくったった Fix #25
2 parents 5af7458 + 97366de commit 61d593b

File tree

11 files changed

+86
-231
lines changed

11 files changed

+86
-231
lines changed

src/builder.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
macro_rules! builder {
2+
( $src_name:ident => $dest_name:ident {
3+
$($attr_name:ident : $attr_type:ty = $attr_default:expr),*
4+
}) => {
5+
pub struct $dest_name {
6+
$( $attr_name : $attr_type ),*
7+
}
8+
9+
pub struct $src_name {
10+
$( $attr_name : $attr_type ),*
11+
}
12+
impl $src_name {
13+
pub fn new() -> $src_name {
14+
$src_name {
15+
$( $attr_name : $attr_default ),*
16+
}
17+
}
18+
19+
pub fn finalize(&self) -> $dest_name {
20+
$dest_name {
21+
$( $attr_name : self.$attr_name.clone() ),*
22+
}
23+
}
24+
25+
$(
26+
pub fn $attr_name(&mut self, value: $attr_type) -> &mut Self {
27+
self.$attr_name = value;
28+
self
29+
}
30+
)*
31+
}
32+
}
33+
}

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#[macro_use]
2+
mod builder;
13
mod core;
4+
25
pub mod public_api;
36
pub mod trade_api;
47

src/main.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ use zaif_api::public_api::*;
99
use zaif_api::trade_api::*;
1010

1111
fn main() {
12-
let api = CurrenciesBuilder::new().name("btc").finalize();
12+
let api = CurrenciesBuilder::new().name("btc".to_string()).finalize();
1313
println!("{}", api.exec().unwrap());
1414

1515
let api = CurrencyPairsBuilder::new().finalize();
1616
println!("{}", api.exec().unwrap());
1717

18-
let api = LastPriceBuilder::new().currency_pair("btc_jpy").finalize();
18+
let api = LastPriceBuilder::new().currency_pair("btc_jpy".to_string()).finalize();
1919
println!("{}", api.exec().unwrap());
2020

21-
let api = DepthBuilder::new().currency_pair("btc_jpy").finalize();
21+
let api = DepthBuilder::new().currency_pair("btc_jpy".to_string()).finalize();
2222
println!("{}", api.exec().unwrap());
2323

2424
let access_key = AccessKey::new("YOUR_API_KEY", "YOUR_API_SECRET");
25-
let api = GetInfo2Builder::new(access_key.clone()).finalize();
25+
let api = GetInfo2Builder::new()
26+
.access_key(access_key.clone())
27+
.finalize();
2628
println!("{}", api.exec().unwrap());
2729

28-
let api = TradeBuilder::new(access_key.clone())
29-
.currency_pair("zaif_jpy")
30+
let api = TradeBuilder::new()
31+
.access_key(access_key.clone())
32+
.currency_pair("zaif_jpy".to_string())
3033
.action(TradeAction::Bid)
3134
.price(1.0)
3235
.amount(0.1)
@@ -36,9 +39,10 @@ fn main() {
3639
println!("{}", res);
3740
let json: Value = serde_json::from_str(res.as_str()).unwrap();
3841
let order_id = json["return"]["order_id"].as_u64().unwrap();
39-
let api = CancelOrderBuilder::new(access_key.clone())
42+
let api = CancelOrderBuilder::new()
43+
.access_key(access_key.clone())
4044
.order_id(order_id)
41-
.currency_pair("zaif_jpy")
45+
.currency_pair(Some("zaif_jpy".to_string()))
4246
.finalize();
4347
let wait_time = time::Duration::from_secs(5);
4448
thread::sleep(wait_time);
@@ -47,8 +51,9 @@ fn main() {
4751
_ => return,
4852
}
4953

50-
let api = ActiveOrdersBuilder::new(access_key.clone())
51-
.currency_pair("zaif_jpy")
54+
let api = ActiveOrdersBuilder::new()
55+
.access_key(access_key.clone())
56+
.currency_pair(Some("zaif_jpy".to_string()))
5257
.finalize();
5358
println!("{}", api.exec().unwrap());
5459
}

src/public_api/currencies.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ extern crate reqwest;
22

33
use core::*;
44

5-
pub struct Currencies {
6-
name: String,
7-
}
5+
builder!(CurrenciesBuilder => Currencies {
6+
name: String = "all".to_string()
7+
});
88

99
impl Currencies {
1010
pub fn exec(&self) -> reqwest::Result<String> {
@@ -17,20 +17,3 @@ impl Currencies {
1717
api.exec()
1818
}
1919
}
20-
21-
pub struct CurrenciesBuilder {
22-
name: String,
23-
}
24-
25-
impl CurrenciesBuilder {
26-
pub fn new() -> CurrenciesBuilder {
27-
CurrenciesBuilder { name: "all".to_string() }
28-
}
29-
pub fn name(&mut self, name: &str) -> &mut CurrenciesBuilder {
30-
self.name = name.to_string();
31-
self
32-
}
33-
pub fn finalize(&self) -> Currencies {
34-
Currencies { name: self.name.clone() }
35-
}
36-
}

src/public_api/currency_pairs.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ extern crate reqwest;
22

33
use core::*;
44

5-
pub struct CurrencyPairs {
6-
currency_pair: String,
7-
}
5+
builder!(CurrencyPairsBuilder => CurrencyPairs {
6+
currency_pair: String = "all".to_string()
7+
});
88

99
impl CurrencyPairs {
1010
pub fn exec(&self) -> reqwest::Result<String> {
@@ -21,19 +21,3 @@ impl CurrencyPairs {
2121
}
2222
}
2323

24-
pub struct CurrencyPairsBuilder {
25-
currency_pair: String,
26-
}
27-
28-
impl CurrencyPairsBuilder {
29-
pub fn new() -> CurrencyPairsBuilder {
30-
CurrencyPairsBuilder { currency_pair: "all".to_string() }
31-
}
32-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut CurrencyPairsBuilder {
33-
self.currency_pair = currency_pair.to_string();
34-
self
35-
}
36-
pub fn finalize(&self) -> CurrencyPairs {
37-
CurrencyPairs { currency_pair: self.currency_pair.clone() }
38-
}
39-
}

src/public_api/depth.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ extern crate reqwest;
22

33
use core::*;
44

5-
pub struct Depth {
6-
currency_pair: String,
7-
}
5+
builder!(DepthBuilder => Depth {
6+
currency_pair: String = "btc_jpy".to_string()
7+
});
88

99
impl Depth {
1010
pub fn exec(&self) -> reqwest::Result<String> {
@@ -18,19 +18,3 @@ impl Depth {
1818
}
1919
}
2020

21-
pub struct DepthBuilder {
22-
currency_pair: String,
23-
}
24-
25-
impl DepthBuilder {
26-
pub fn new() -> DepthBuilder {
27-
DepthBuilder { currency_pair: "btc_jpy".to_string() }
28-
}
29-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut DepthBuilder {
30-
self.currency_pair = currency_pair.to_string();
31-
self
32-
}
33-
pub fn finalize(&self) -> Depth {
34-
Depth { currency_pair: self.currency_pair.clone() }
35-
}
36-
}

src/public_api/last_price.rs

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ extern crate reqwest;
22

33
use core::*;
44

5-
pub struct LastPrice {
6-
currency_pair: String,
7-
}
5+
builder!(LastPriceBuilder => LastPrice {
6+
currency_pair: String = "btc_jpy".to_string()
7+
});
88

99
impl LastPrice {
1010
pub fn exec(&self) -> reqwest::Result<String> {
@@ -21,19 +21,3 @@ impl LastPrice {
2121
}
2222
}
2323

24-
pub struct LastPriceBuilder {
25-
currency_pair: String,
26-
}
27-
28-
impl LastPriceBuilder {
29-
pub fn new() -> LastPriceBuilder {
30-
LastPriceBuilder { currency_pair: "btc_jpy".to_string() }
31-
}
32-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut LastPriceBuilder {
33-
self.currency_pair = currency_pair.to_string();
34-
self
35-
}
36-
pub fn finalize(&self) -> LastPrice {
37-
LastPrice { currency_pair: self.currency_pair.clone() }
38-
}
39-
}

src/trade_api/active_orders.rs

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ use std::collections::HashMap;
44

55
use core::*;
66

7-
pub struct ActiveOrders {
8-
access_key: AccessKey,
9-
currency_pair: Option<String>,
10-
}
7+
builder!(ActiveOrdersBuilder => ActiveOrders {
8+
access_key: AccessKey = AccessKey::new("", ""),
9+
currency_pair: Option<String> = None
10+
});
1111

1212
impl ActiveOrders {
1313
pub fn exec(&self) -> reqwest::Result<String> {
@@ -31,26 +31,3 @@ impl ActiveOrders {
3131
}
3232
}
3333

34-
pub struct ActiveOrdersBuilder {
35-
access_key: AccessKey,
36-
currency_pair: Option<String>,
37-
}
38-
39-
impl ActiveOrdersBuilder {
40-
pub fn new(access_key: AccessKey) -> ActiveOrdersBuilder {
41-
ActiveOrdersBuilder {
42-
access_key: access_key,
43-
currency_pair: None,
44-
}
45-
}
46-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut ActiveOrdersBuilder {
47-
self.currency_pair = Some(currency_pair.to_string());
48-
self
49-
}
50-
pub fn finalize(&self) -> ActiveOrders {
51-
ActiveOrders {
52-
access_key: self.access_key.clone(),
53-
currency_pair: self.currency_pair.clone(),
54-
}
55-
}
56-
}

src/trade_api/cancel_order.rs

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ use std::collections::HashMap;
44

55
use core::*;
66

7-
pub struct CancelOrder {
8-
access_key: AccessKey,
9-
order_id: u64,
10-
currency_pair: Option<String>,
11-
}
7+
builder!(CancelOrderBuilder => CancelOrder {
8+
access_key: AccessKey = AccessKey::new("", ""),
9+
order_id: u64 = 0,
10+
currency_pair: Option<String> = None
11+
});
1212

1313
impl CancelOrder {
1414
pub fn exec(&self) -> reqwest::Result<String> {
@@ -33,33 +33,3 @@ impl CancelOrder {
3333
}
3434
}
3535

36-
pub struct CancelOrderBuilder {
37-
access_key: AccessKey,
38-
order_id: Option<u64>,
39-
currency_pair: Option<String>,
40-
}
41-
42-
impl CancelOrderBuilder {
43-
pub fn new(access_key: AccessKey) -> CancelOrderBuilder {
44-
CancelOrderBuilder {
45-
access_key: access_key,
46-
order_id: None,
47-
currency_pair: None,
48-
}
49-
}
50-
pub fn order_id(&mut self, order_id: u64) -> &mut CancelOrderBuilder {
51-
self.order_id = Some(order_id);
52-
self
53-
}
54-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut CancelOrderBuilder {
55-
self.currency_pair = Some(currency_pair.to_string());
56-
self
57-
}
58-
pub fn finalize(&self) -> CancelOrder {
59-
CancelOrder {
60-
access_key: self.access_key.clone(),
61-
order_id: self.order_id.unwrap(),
62-
currency_pair: self.currency_pair.clone(),
63-
}
64-
}
65-
}

src/trade_api/get_info2.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::collections::HashMap;
44

55
use core::*;
66

7-
pub struct GetInfo2 {
8-
access_key: AccessKey,
9-
}
7+
builder!(GetInfo2Builder => GetInfo2 {
8+
access_key: AccessKey = AccessKey::new("", "")
9+
});
1010

1111
impl GetInfo2 {
1212
pub fn new(access_key: AccessKey) -> GetInfo2 {
@@ -27,15 +27,3 @@ impl GetInfo2 {
2727
}
2828
}
2929

30-
pub struct GetInfo2Builder {
31-
access_key: AccessKey,
32-
}
33-
34-
impl GetInfo2Builder {
35-
pub fn new(access_key: AccessKey) -> GetInfo2Builder {
36-
GetInfo2Builder { access_key: access_key }
37-
}
38-
pub fn finalize(&self) -> GetInfo2 {
39-
GetInfo2 { access_key: self.access_key.clone() }
40-
}
41-
}

0 commit comments

Comments
 (0)