Skip to content

Commit

Permalink
Fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Apr 23, 2024
1 parent ab55a2a commit 828ca94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 33 deletions.
44 changes: 17 additions & 27 deletions nautilus_core/cli/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use sqlx::PgPool;

use crate::opt::{DatabaseCommand, DatabaseOpt};

/// Scans current path with keyword nautilus_trader and build schema dir
/// Scans current path with keyword `nautilus_trader` and build schema dir
fn get_schema_dir() -> anyhow::Result<String> {
std::env::var("SCHEMA_DIR").or_else(|_| {
let nautilus_git_repo_name = "nautilus_trader";
Expand All @@ -46,7 +46,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
Err(err) => error!("Error creating schema public: {:?}", err),
}
// create role if not exists
match sqlx::query(format!("CREATE ROLE {} PASSWORD '{}' LOGIN;", database, password).as_str())
match sqlx::query(format!("CREATE ROLE {database} PASSWORD '{password}' LOGIN;").as_str())
.execute(pg)
.await
{
Expand All @@ -73,7 +73,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
}
}
// grant connect
match sqlx::query(format!("GRANT CONNECT ON DATABASE {0} TO {0};", database).as_str())
match sqlx::query(format!("GRANT CONNECT ON DATABASE {database} TO {database};").as_str())
.execute(pg)
.await
{
Expand All @@ -84,7 +84,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
),
}
// grant all schema privileges to the role
match sqlx::query(format!("GRANT ALL PRIVILEGES ON SCHEMA public TO {};", database).as_str())
match sqlx::query(format!("GRANT ALL PRIVILEGES ON SCHEMA public TO {database};").as_str())
.execute(pg)
.await
{
Expand All @@ -96,11 +96,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
}
// grant all table privileges to the role
match sqlx::query(
format!(
"GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {};",
database
)
.as_str(),
format!("GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO {database};").as_str(),
)
.execute(pg)
.await
Expand All @@ -113,11 +109,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
}
// grant all sequence privileges to the role
match sqlx::query(
format!(
"GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {};",
database
)
.as_str(),
format!("GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO {database};").as_str(),
)
.execute(pg)
.await
Expand All @@ -130,11 +122,7 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a
}
// grant all function privileges to the role
match sqlx::query(
format!(
"GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO {};",
database
)
.as_str(),
format!("GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA public TO {database};").as_str(),
)
.execute(pg)
.await
Expand All @@ -151,15 +139,15 @@ pub async fn init_postgres(pg: &PgPool, database: String, password: String) -> a

pub async fn drop_postgres(pg: &PgPool, database: String) -> anyhow::Result<()> {
// execute drop owned
match sqlx::query(format!("DROP OWNED BY {}", database).as_str())
match sqlx::query(format!("DROP OWNED BY {database}").as_str())
.execute(pg)
.await
{
Ok(_) => info!("Dropped owned objects by role {}", database),
Err(err) => error!("Error dropping owned by role {}: {:?}", database, err),
}
// revoke connect
match sqlx::query(format!("REVOKE CONNECT ON DATABASE {0} FROM {0};", database).as_str())
match sqlx::query(format!("REVOKE CONNECT ON DATABASE {database} FROM {database};").as_str())
.execute(pg)
.await
{
Expand All @@ -170,9 +158,11 @@ pub async fn drop_postgres(pg: &PgPool, database: String) -> anyhow::Result<()>
),
}
// revoke privileges
match sqlx::query(format!("REVOKE ALL PRIVILEGES ON DATABASE {0} FROM {0};", database).as_str())
.execute(pg)
.await
match sqlx::query(
format!("REVOKE ALL PRIVILEGES ON DATABASE {database} FROM {database};").as_str(),
)
.execute(pg)
.await
{
Ok(_) => info!("Revoked all privileges from role {}", database),
Err(err) => error!(
Expand All @@ -189,7 +179,7 @@ pub async fn drop_postgres(pg: &PgPool, database: String) -> anyhow::Result<()>
Err(err) => error!("Error dropping schema public: {:?}", err),
}
// drop role
match sqlx::query(format!("DROP ROLE IF EXISTS {};", database).as_str())
match sqlx::query(format!("DROP ROLE IF EXISTS {database};").as_str())
.execute(pg)
.await
{
Expand Down Expand Up @@ -218,7 +208,7 @@ pub async fn run_database_command(opt: DatabaseOpt) -> anyhow::Result<()> {
pg_connect_options.database,
pg_connect_options.password,
)
.await?
.await?;
}
DatabaseCommand::Drop(config) => {
let pg_connect_options = get_postgres_connect_options(
Expand All @@ -230,7 +220,7 @@ pub async fn run_database_command(opt: DatabaseOpt) -> anyhow::Result<()> {
)
.unwrap();
let pg = connect_pg(pg_connect_options.clone().into()).await?;
drop_postgres(&pg, pg_connect_options.database).await?
drop_postgres(&pg, pg_connect_options.database).await?;
}
}
Ok(())
Expand Down
16 changes: 12 additions & 4 deletions nautilus_core/common/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,11 +1186,12 @@ impl Cache {
pub fn get(&self, key: &str) -> anyhow::Result<Option<&[u8]>> {
check_valid_string(key, stringify!(key))?;

Ok(self.general.get(key).map(|x| x.as_slice()))
Ok(self.general.get(key).map(std::vec::Vec::as_slice))
}

// -- DATA QUERIES --------------------------------------------------------

#[must_use]
pub fn price(&self, instrument_id: &InstrumentId, price_type: PriceType) -> Option<Price> {
match price_type {
PriceType::Bid => self
Expand All @@ -1217,40 +1218,47 @@ impl Cache {
}
}

#[must_use]
pub fn quote_ticks(&self, instrument_id: &InstrumentId) -> Option<Vec<QuoteTick>> {
self.quotes
.get(instrument_id)
.map(|quotes| quotes.iter().cloned().collect())
.map(|quotes| quotes.iter().copied().collect())
}

#[must_use]
pub fn trade_ticks(&self, instrument_id: &InstrumentId) -> Option<Vec<TradeTick>> {
self.trades
.get(instrument_id)
.map(|trades| trades.iter().cloned().collect())
.map(|trades| trades.iter().copied().collect())
}

#[must_use]
pub fn bars(&self, bar_type: &BarType) -> Option<Vec<Bar>> {
self.bars
.get(bar_type)
.map(|bars| bars.iter().cloned().collect())
.map(|bars| bars.iter().copied().collect())
}

#[must_use]
pub fn order_book(&self, instrument_id: &InstrumentId) -> Option<&OrderBook> {
self.books.get(instrument_id)
}

#[must_use]
pub fn quote_tick(&self, instrument_id: &InstrumentId) -> Option<&QuoteTick> {
self.quotes
.get(instrument_id)
.and_then(|quotes| quotes.front())
}

#[must_use]
pub fn trade_tick(&self, instrument_id: &InstrumentId) -> Option<&TradeTick> {
self.trades
.get(instrument_id)
.and_then(|trades| trades.front())
}

#[must_use]
pub fn bar(&self, bar_type: &BarType) -> Option<&Bar> {
self.bars.get(bar_type).and_then(|bars| bars.front())
}
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/common/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ mod tests {

// Create a new LiveTimer with no stop time
let clock = get_atomic_clock_realtime();
let start_time = UnixNanos::from(clock.get_time_ns());
let start_time = clock.get_time_ns();
let interval_ns = 100 * NANOSECONDS_IN_MILLISECOND;
let mut timer =
LiveTimer::new("TEST_TIMER", interval_ns, start_time, None, handler).unwrap();
Expand All @@ -504,7 +504,7 @@ mod tests {

// Create a new LiveTimer with a stop time
let clock = get_atomic_clock_realtime();
let start_time = UnixNanos::from(clock.get_time_ns());
let start_time = clock.get_time_ns();
let interval_ns = 100 * NANOSECONDS_IN_MILLISECOND;
let stop_time = start_time + 500 * NANOSECONDS_IN_MILLISECOND;
let mut timer = LiveTimer::new(
Expand Down

0 comments on commit 828ca94

Please sign in to comment.