Skip to content

Commit 8ff036d

Browse files
committed
TradeBuilderをマクロで再実装した
1 parent 04830b3 commit 8ff036d

File tree

2 files changed

+14
-69
lines changed

2 files changed

+14
-69
lines changed

src/main.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ fn main() {
2727
.finalize();
2828
println!("{}", api.exec().unwrap());
2929

30-
let api = TradeBuilder::new(access_key.clone())
31-
.currency_pair("zaif_jpy")
30+
let api = TradeBuilder::new()
31+
.access_key(access_key.clone())
32+
.currency_pair("zaif_jpy".to_string())
3233
.action(TradeAction::Bid)
3334
.price(1.0)
3435
.amount(0.1)

src/trade_api/trade.rs

Lines changed: 11 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use core::*;
66

77
#[derive(Copy, Clone)]
88
pub enum TradeAction {
9+
None,
910
Bid, // 買い
1011
Ask, // 売り
1112
}
@@ -14,19 +15,20 @@ impl TradeAction {
1415
match *self {
1516
TradeAction::Bid => "bid".to_string(),
1617
TradeAction::Ask => "ask".to_string(),
18+
_ => "".to_string(),
1719
}
1820
}
1921
}
2022

21-
pub struct Trade {
22-
access_key: AccessKey,
23-
currency_pair: String,
24-
action: TradeAction,
25-
price: f32,
26-
amount: f32,
27-
limit: Option<f32>,
28-
comment: Option<String>,
29-
}
23+
builder!(TradeBuilder => Trade {
24+
access_key: AccessKey = AccessKey::new("", ""),
25+
currency_pair: String = "".to_string(),
26+
action: TradeAction = TradeAction::None,
27+
price: f32 = 0.0,
28+
amount: f32 = 0.0,
29+
limit: Option<f32> = None,
30+
comment: Option<String> = None
31+
});
3032

3133
impl Trade {
3234
pub fn exec(&self) -> reqwest::Result<String> {
@@ -54,61 +56,3 @@ impl Trade {
5456
}
5557
}
5658

57-
pub struct TradeBuilder {
58-
access_key: AccessKey,
59-
currency_pair: Option<String>,
60-
action: Option<TradeAction>,
61-
price: Option<f32>,
62-
amount: Option<f32>,
63-
limit: Option<f32>,
64-
comment: Option<String>,
65-
}
66-
67-
impl TradeBuilder {
68-
pub fn new(access_key: AccessKey) -> TradeBuilder {
69-
TradeBuilder {
70-
access_key: access_key,
71-
currency_pair: None,
72-
action: None,
73-
price: None,
74-
amount: None,
75-
limit: None,
76-
comment: None,
77-
}
78-
}
79-
pub fn currency_pair(&mut self, currency_pair: &str) -> &mut TradeBuilder {
80-
self.currency_pair = Some(currency_pair.to_string());
81-
self
82-
}
83-
pub fn action(&mut self, action: TradeAction) -> &mut TradeBuilder {
84-
self.action = Some(action);
85-
self
86-
}
87-
pub fn price(&mut self, price: f32) -> &mut TradeBuilder {
88-
self.price = Some(price);
89-
self
90-
}
91-
pub fn amount(&mut self, amount: f32) -> &mut TradeBuilder {
92-
self.amount = Some(amount);
93-
self
94-
}
95-
pub fn limit(&mut self, limit: f32) -> &mut TradeBuilder {
96-
self.limit = Some(limit);
97-
self
98-
}
99-
pub fn comment(&mut self, comment: &str) -> &mut TradeBuilder {
100-
self.comment = Some(comment.to_string());
101-
self
102-
}
103-
pub fn finalize(&self) -> Trade {
104-
Trade {
105-
access_key: self.access_key.clone(),
106-
currency_pair: self.currency_pair.clone().unwrap().clone(),
107-
action: self.action.unwrap(),
108-
price: self.price.unwrap(),
109-
amount: self.amount.unwrap(),
110-
limit: self.limit.clone(),
111-
comment: self.comment.clone(),
112-
}
113-
}
114-
}

0 commit comments

Comments
 (0)