Skip to content

Commit

Permalink
Code hygiene (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
wboayue authored Oct 2, 2024
1 parent 76033d1 commit 01a521d
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 55 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ibapi"
version = "0.4.3"
version = "0.5.0"
edition = "2021"
authors = ["Wil Boayue <[email protected]>"]
description = "A synchronous implementation of the Interactive Brokers TWS API."
Expand Down
26 changes: 26 additions & 0 deletions contributors/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,29 @@ RUST_LOG=debug cargo run --bin find_contract_details

`RUST_LOG=debug`
`IBAPI_RECORDING_DIR=/tmp`

## Creating and publishing releases.

1. Make sure build is clean and tests are passing.

```bash
cargo build --all-targets
cargo test
```

2. Update version number in [Cargo.toml](https://github.com/wboayue/rust-ibapi/blob/76033d170f2b87d55ed2cd96fef17bf124161d5f/Cargo.toml#L3) using [semantic versioning](https://semver.org/). Commit and push.

3. Create tag with new version number and push.

```bash
git tag v0.4.0 main
git push origin tag v0.4.0
```

4. [Create release](https://github.com/wboayue/rust-ibapi/releases/new) pointing to new tag. Describe changes in release.

5. Publish to crates.io

```bash
cargo publish
```
6 changes: 3 additions & 3 deletions examples/tick_by_tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ fn main() {
thread::sleep(Duration::from_secs(5));
}

fn stream_last(client: &mut Client, symbol: &str) -> anyhow::Result<()> {
fn stream_last(client: &mut Client, _symbol: &str) -> anyhow::Result<()> {
let contract = contract_gc();
let ticks = client.tick_by_tick_last(&contract, 0, false)?;

Expand Down Expand Up @@ -87,7 +87,7 @@ fn stream_all_last(client: &Client, symbol: &str) -> anyhow::Result<()> {
Ok(())
}

fn stream_bid_ask(client: &mut Client, symbol: &str) -> anyhow::Result<()> {
fn stream_bid_ask(client: &mut Client, _symbol: &str) -> anyhow::Result<()> {
let contract = contract_es();
let ticks = client.tick_by_tick_bid_ask(&contract, 0, false)?;

Expand All @@ -98,7 +98,7 @@ fn stream_bid_ask(client: &mut Client, symbol: &str) -> anyhow::Result<()> {
Ok(())
}

fn stream_mid_point(client: &mut Client, symbol: &str) -> anyhow::Result<()> {
fn stream_mid_point(client: &mut Client, _symbol: &str) -> anyhow::Result<()> {
let contract = contract_es();
let ticks = client.tick_by_tick_midpoint(&contract, 0, false)?;

Expand Down
1 change: 0 additions & 1 deletion src/accounts/decoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ pub(crate) fn decode_family_codes(message: &mut ResponseMessage) -> Result<Vec<F
let family_code = FamilyCode {
account_id: message.next_string()?,
family_code: message.next_string()?,
..Default::default()
};
family_codes.push(family_code);
}
Expand Down
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl Client {

/// The time of the server when the client connected
pub fn connection_time(&self) -> Option<OffsetDateTime> {
self.connection_time.clone()
self.connection_time
}

/// Returns the managed accounts.
Expand Down
2 changes: 1 addition & 1 deletion src/client/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::HashMap;
use std::io::{prelude::*, Cursor};
use std::iter::Iterator;
use std::net::TcpStream;
use std::sync::Mutex;
use std::sync::{Arc, RwLock};
use std::thread::{self, JoinHandle};
use std::time::Duration;
use std::sync::Mutex;

use byteorder::{BigEndian, ReadBytesExt, WriteBytesExt};
use crossbeam::channel::{self, Receiver, Sender};
Expand Down
1 change: 0 additions & 1 deletion src/contracts/encoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ pub(crate) fn request_market_rule(market_rule_id: i32) -> Result<RequestMessage,

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn request_market_rule() {
Expand Down
2 changes: 1 addition & 1 deletion src/contracts/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn request_stock_contract_details() {
]
}));

let mut client = Client::stubbed(message_bus, server_versions::SIZE_RULES);
let client = Client::stubbed(message_bus, server_versions::SIZE_RULES);

let contract = Contract::stock("TSLA");

Expand Down
73 changes: 36 additions & 37 deletions src/market_data/historical.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use std::collections::VecDeque;
use std::fmt::Debug;
use std::fmt::{Debug, Display};

use log::{error, warn};
use time::{Date, OffsetDateTime};
use time_tz::Tz;

use crate::client::transport::ResponseIterator;
use crate::contracts::Contract;
Expand Down Expand Up @@ -60,28 +59,28 @@ pub enum BarSize {
Month,
}

impl ToString for BarSize {
fn to_string(&self) -> String {
impl Display for BarSize {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Sec => "1 sec".into(),
Self::Sec5 => "5 secs".into(),
Self::Sec15 => "15 secs".into(),
Self::Sec30 => "30 secs".into(),
Self::Min => "1 min".into(),
Self::Min2 => "2 mins".into(),
Self::Min3 => "3 mins".into(),
Self::Min5 => "5 mins".into(),
Self::Min15 => "15 mins".into(),
Self::Min20 => "20 mins".into(),
Self::Min30 => "30 mins".into(),
Self::Hour => "1 hour".into(),
Self::Hour2 => "2 hours".into(),
Self::Hour3 => "3 hours".into(),
Self::Hour4 => "4 hours".into(),
Self::Hour8 => "8 hours".into(),
Self::Day => "1 day".into(),
Self::Week => "1 week".into(),
Self::Month => "1 month".into(),
Self::Sec => write!(f, "1 sec"),
Self::Sec5 => write!(f, "5 secs"),
Self::Sec15 => write!(f, "15 secs"),
Self::Sec30 => write!(f, "30 secs"),
Self::Min => write!(f, "1 min"),
Self::Min2 => write!(f, "2 mins"),
Self::Min3 => write!(f, "3 mins"),
Self::Min5 => write!(f, "5 mins"),
Self::Min15 => write!(f, "15 mins"),
Self::Min20 => write!(f, "20 mins"),
Self::Min30 => write!(f, "30 mins"),
Self::Hour => write!(f, "1 hour"),
Self::Hour2 => write!(f, "2 hours"),
Self::Hour3 => write!(f, "3 hours"),
Self::Hour4 => write!(f, "4 hours"),
Self::Hour8 => write!(f, "8 hours"),
Self::Day => write!(f, "1 day"),
Self::Week => write!(f, "1 week"),
Self::Month => write!(f, "1 month"),
}
}
}
Expand Down Expand Up @@ -126,9 +125,9 @@ impl Duration {
}
}

impl ToString for Duration {
fn to_string(&self) -> String {
format!("{} {}", self.value, self.unit)
impl Display for Duration {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{} {}", self.value, self.unit)
}
}

Expand Down Expand Up @@ -265,18 +264,18 @@ pub enum WhatToShow {
Schedule,
}

impl ToString for WhatToShow {
fn to_string(&self) -> String {
impl std::fmt::Display for WhatToShow {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Trades => "TRADES".to_string(),
Self::MidPoint => "MIDPOINT".to_string(),
Self::Bid => "BID".to_string(),
Self::Ask => "ASK".to_string(),
Self::BidAsk => "BID_ASK".to_string(),
Self::HistoricalVolatility => "HISTORICAL_VOLATILITY".to_string(),
Self::OptionImpliedVolatility => "OPTION_IMPLIED_VOLATILITY".to_string(),
Self::FeeRate => "FEE_RATE".to_string(),
Self::Schedule => "SCHEDULE".to_string(),
Self::Trades => write!(f, "TRADES"),
Self::MidPoint => write!(f, "MIDPOINT"),
Self::Bid => write!(f, "BID"),
Self::Ask => write!(f, "ASK"),
Self::BidAsk => write!(f, "BID_ASK"),
Self::HistoricalVolatility => write!(f, "HISTORICAL_VOLATILITY"),
Self::OptionImpliedVolatility => write!(f, "OPTION_IMPLIED_VOLATILITY"),
Self::FeeRate => write!(f, "FEE_RATE"),
Self::Schedule => write!(f, "SCHEDULE"),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/market_data/historical/encoders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pub(super) fn encode_request_historical_data(
}

// Encodes message to request historical ticks
#[allow(clippy::too_many_arguments)]
pub(super) fn encode_request_historical_ticks(
request_id: i32,
contract: &Contract,
Expand Down
12 changes: 6 additions & 6 deletions src/market_data/realtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,13 @@ pub enum WhatToShow {
Ask,
}

impl ToString for WhatToShow {
fn to_string(&self) -> String {
impl std::fmt::Display for WhatToShow {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Self::Trades => "TRADES".to_string(),
Self::MidPoint => "MIDPOINT".to_string(),
Self::Bid => "BID".to_string(),
Self::Ask => "ASK".to_string(),
Self::Trades => write!(f, "TRADES"),
Self::MidPoint => write!(f, "MIDPOINT"),
Self::Bid => write!(f, "BID"),
Self::Ask => write!(f, "ASK"),
}
}
}
Expand Down
15 changes: 12 additions & 3 deletions src/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ impl MessageBus for MessageBusStub {
}

fn write_message(&mut self, message: &RequestMessage) -> Result<(), Error> {
self.request_messages.write().expect("MessageBus.request_messages is poisoned").push(message.clone());
self.request_messages
.write()
.expect("MessageBus.request_messages is poisoned")
.push(message.clone());
Ok(())
}

Expand Down Expand Up @@ -71,7 +74,10 @@ impl MessageBus for MessageBusStub {
}

fn mock_request(stub: &mut MessageBusStub, _request_id: i32, message: &RequestMessage) -> Result<ResponseIterator, Error> {
stub.request_messages.write().expect("MessageBus.request_messages is poisoned").push(message.clone());
stub.request_messages
.write()
.expect("MessageBus.request_messages is poisoned")
.push(message.clone());

let (sender, receiver) = channel::unbounded();
let (s1, _r1) = channel::unbounded();
Expand All @@ -84,7 +90,10 @@ fn mock_request(stub: &mut MessageBusStub, _request_id: i32, message: &RequestMe
}

fn mock_global_request(stub: &mut MessageBusStub, message: &RequestMessage) -> Result<GlobalResponseIterator, Error> {
stub.request_messages.write().expect("MessageBus.request_messages is poisoned").push(message.clone());
stub.request_messages
.write()
.expect("MessageBus.request_messages is poisoned")
.push(message.clone());

let (sender, receiver) = channel::unbounded();

Expand Down

0 comments on commit 01a521d

Please sign in to comment.