Skip to content

Commit

Permalink
Add Rustfmt (#10)
Browse files Browse the repository at this point in the history
* Add rustfmt.toml

* Format code

* Add GitHub action

* Add pre-commit hook

* Run pre-commit hooks

* Remove whitespace
  • Loading branch information
tompntn authored Nov 4, 2022
1 parent 9afc7c8 commit b0c5b25
Show file tree
Hide file tree
Showing 10 changed files with 415 additions and 238 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
push:
branches:
- main

pull_request:
branches:
- main
Expand Down
19 changes: 19 additions & 0 deletions .github/workflows/rustfmt.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Check Rustfmt

on:
pull_request:
push:
branches: [main]

jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
components: rustfmt
- uses: pre-commit/[email protected]
14 changes: 14 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.2.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
- repo: local
hooks:
- id: cargo-fmt-nightly
name: rustfmt
language: "rust"
entry: cargo +nightly fmt
pass_filenames: false
6 changes: 3 additions & 3 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@
{ self
, nixpkgs
, fenix
, flake-utils
, flake-utils
}:

# Generate a Flake Configuration for each supported system.
flake-utils.lib.eachDefaultSystem (system:
let
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
shell = import ./shell.nix { inherit pkgs; };
rust = pkgs.makeRustPlatform {
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ channel = "stable"
profile = "minimal"
components = [
"rustfmt",
"clippy"
"clippy"
]
17 changes: 17 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Merge all imports into a clean vertical list of module imports.
imports_granularity = "One"
group_imports = "One"
imports_layout = "Vertical"

# Better grep-ability.
empty_item_single_line = false

# Consistent pipe layout.
match_arm_leading_pipes = "Preserve"

# Align Fields
enum_discrim_align_threshold = 80
struct_field_align_threshold = 80

# Allow up to two blank lines for visual grouping.
blank_lines_upper_bound = 2
172 changes: 106 additions & 66 deletions src/publisher/pythd/adapter.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,42 @@
use std::{collections::HashMap, time::Duration};

use super::super::store::{global, PriceIdentifier};
use super::api::{
self, Conf, NotifyPrice, NotifyPriceSched, Price, PriceAccountMetadata, ProductAccount,
ProductAccountMetadata, SubscriptionID,
};
use anyhow::{anyhow, Result};
use pyth_sdk::Identifier;
use slog::Logger;
use tokio::{
sync::{broadcast, mpsc, oneshot},
time::{self, Interval},
use {
super::{
super::store::{
global,
PriceIdentifier,
},
api::{
self,
Conf,
NotifyPrice,
NotifyPriceSched,
Price,
PriceAccountMetadata,
ProductAccount,
ProductAccountMetadata,
SubscriptionID,
},
},
anyhow::{
anyhow,
Result,
},
pyth_sdk::Identifier,
slog::Logger,
std::{
collections::HashMap,
time::Duration,
},
tokio::{
sync::{
broadcast,
mpsc,
oneshot,
},
time::{
self,
Interval,
},
},
};

/// Adapter is the adapter between the pythd websocket API, and the stores.
Expand Down Expand Up @@ -42,7 +68,7 @@ pub struct Adapter {
/// Represents a single Notify Price Sched subscription
struct NotifyPriceSchedSubscription {
/// ID of this subscription
subscription_id: SubscriptionID,
subscription_id: SubscriptionID,
/// Channel notifications are sent on
notify_price_sched_tx: mpsc::Sender<NotifyPriceSched>,
}
Expand All @@ -53,27 +79,27 @@ pub enum Message {
result_tx: oneshot::Sender<Result<Vec<ProductAccountMetadata>>>,
},
GetProduct {
account: api::Pubkey,
account: api::Pubkey,
result_tx: oneshot::Sender<Result<ProductAccount>>,
},
GetAllProducts {
result_tx: oneshot::Sender<Result<Vec<ProductAccount>>>,
},
SubscribePrice {
account: api::Pubkey,
account: api::Pubkey,
notify_price_tx: mpsc::Sender<NotifyPrice>,
result_tx: oneshot::Sender<Result<SubscriptionID>>,
result_tx: oneshot::Sender<Result<SubscriptionID>>,
},
SubscribePriceSched {
account: api::Pubkey,
account: api::Pubkey,
notify_price_sched_tx: mpsc::Sender<NotifyPriceSched>,
result_tx: oneshot::Sender<Result<SubscriptionID>>,
result_tx: oneshot::Sender<Result<SubscriptionID>>,
},
UpdatePrice {
account: api::Pubkey,
price: Price,
conf: Conf,
status: String,
price: Price,
conf: Conf,
status: String,
},
}

Expand Down Expand Up @@ -172,17 +198,17 @@ impl Adapter {
.map(|acc| (price_account_key, acc))
})
.map(|(price_account_key, price_account)| PriceAccountMetadata {
account: price_account_key.to_string(),
price_type: "price".to_owned(),
account: price_account_key.to_string(),
price_type: "price".to_owned(),
price_exponent: price_account.expo as i64,
})
.collect();

// Create the product account metadata struct
result.push(ProductAccountMetadata {
account: product_account_key.to_string(),
account: product_account_key.to_string(),
attr_dict: product_account.attr_dict,
prices: price_accounts_metadata,
prices: price_accounts_metadata,
})
}

Expand Down Expand Up @@ -235,30 +261,44 @@ impl Adapter {

#[cfg(test)]
mod tests {
use iobuffer::IoBuffer;
use slog_extlog::slog_test;
use std::{
collections::{BTreeMap, HashMap},
str::FromStr,
time::Duration,
};
use tokio::{
sync::{broadcast, mpsc, oneshot},
task::JoinHandle,
};

use crate::publisher::{
pythd::api::{NotifyPriceSched, PriceAccountMetadata, ProductAccountMetadata},
store::global,
use {
super::{
Adapter,
Message,
},
crate::publisher::{
pythd::api::{
NotifyPriceSched,
PriceAccountMetadata,
ProductAccountMetadata,
},
store::global,
},
iobuffer::IoBuffer,
slog_extlog::slog_test,
std::{
collections::{
BTreeMap,
HashMap,
},
str::FromStr,
time::Duration,
},
tokio::{
sync::{
broadcast,
mpsc,
oneshot,
},
task::JoinHandle,
},
};

use super::{Adapter, Message};

struct TestAdapter {
message_tx: mpsc::Sender<Message>,
shutdown_tx: broadcast::Sender<()>,
message_tx: mpsc::Sender<Message>,
shutdown_tx: broadcast::Sender<()>,
global_store_rx: mpsc::Receiver<global::Message>,
jh: JoinHandle<()>,
jh: JoinHandle<()>,
}

impl Drop for TestAdapter {
Expand Down Expand Up @@ -317,7 +357,7 @@ mod tests {
assert_eq!(
notify_price_sched_rx.recv().await.unwrap(),
NotifyPriceSched {
subscription: subscription_id
subscription: subscription_id,
}
)
}
Expand All @@ -332,7 +372,7 @@ mod tests {
)
.unwrap(),
global::ProductAccountMetadata {
attr_dict: BTreeMap::from(
attr_dict: BTreeMap::from(
[
("symbol", "Crypto.LTC/USD"),
("asset_type", "Crypto"),
Expand Down Expand Up @@ -365,7 +405,7 @@ mod tests {
)
.unwrap(),
global::ProductAccountMetadata {
attr_dict: BTreeMap::from(
attr_dict: BTreeMap::from(
[
("symbol", "Crypto.ETH/USD"),
("asset_type", "Crypto"),
Expand Down Expand Up @@ -393,7 +433,7 @@ mod tests {
},
),
]),
price_accounts_metadata: HashMap::from([
price_accounts_metadata: HashMap::from([
(
solana_sdk::pubkey::Pubkey::from_str(
"GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU",
Expand Down Expand Up @@ -464,7 +504,7 @@ mod tests {
// Check that the result is what we expected
let expected = vec![
ProductAccountMetadata {
account: "BjHoZWRxo9dgbR1NQhPyTiUs6xFiX6mGS4TMYvy3b2yc".to_string(),
account: "BjHoZWRxo9dgbR1NQhPyTiUs6xFiX6mGS4TMYvy3b2yc".to_string(),
attr_dict: BTreeMap::from(
[
("symbol", "Crypto.ETH/USD"),
Expand All @@ -476,26 +516,26 @@ mod tests {
]
.map(|(k, v)| (k.to_string(), v.to_string())),
),
prices: vec![
prices: vec![
PriceAccountMetadata {
account: "GG3FTE7xhc9Diy7dn9P6BWzoCrAEE4D3p5NBYrDAm5DD".to_string(),
price_type: "price".to_string(),
account: "GG3FTE7xhc9Diy7dn9P6BWzoCrAEE4D3p5NBYrDAm5DD".to_string(),
price_type: "price".to_string(),
price_exponent: -9,
},
PriceAccountMetadata {
account: "fTNjSfj5uW9e4CAMHzUcm65ftRNBxCN1gG5GS1mYfid".to_string(),
price_type: "price".to_string(),
account: "fTNjSfj5uW9e4CAMHzUcm65ftRNBxCN1gG5GS1mYfid".to_string(),
price_type: "price".to_string(),
price_exponent: -6,
},
PriceAccountMetadata {
account: "GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ".to_string(),
price_type: "price".to_string(),
account: "GKNcUmNacSJo4S2Kq3DuYRYRGw3sNUfJ4tyqd198t6vQ".to_string(),
price_type: "price".to_string(),
price_exponent: 2,
},
],
},
ProductAccountMetadata {
account: "CkMrDWtmFJZcmAUC11qNaWymbXQKvnRx4cq1QudLav7t".to_string(),
account: "CkMrDWtmFJZcmAUC11qNaWymbXQKvnRx4cq1QudLav7t".to_string(),
attr_dict: BTreeMap::from(
[
("symbol", "Crypto.LTC/USD"),
Expand All @@ -507,20 +547,20 @@ mod tests {
]
.map(|(k, v)| (k.to_string(), v.to_string())),
),
prices: vec![
prices: vec![
PriceAccountMetadata {
account: "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU".to_string(),
price_type: "price".to_string(),
account: "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU".to_string(),
price_type: "price".to_string(),
price_exponent: -8,
},
PriceAccountMetadata {
account: "3VQwtcntVQN1mj1MybQw8qK7Li3KNrrgNskSQwZAPGNr".to_string(),
price_type: "price".to_string(),
account: "3VQwtcntVQN1mj1MybQw8qK7Li3KNrrgNskSQwZAPGNr".to_string(),
price_type: "price".to_string(),
price_exponent: -10,
},
PriceAccountMetadata {
account: "2V7t5NaKY7aGkwytCWQgvUYZfEr9XMwNChhJEakTExk6".to_string(),
price_type: "price".to_string(),
account: "2V7t5NaKY7aGkwytCWQgvUYZfEr9XMwNChhJEakTExk6".to_string(),
price_type: "price".to_string(),
price_exponent: -6,
},
],
Expand Down
Loading

0 comments on commit b0c5b25

Please sign in to comment.