Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» proper usage of Path
Browse files Browse the repository at this point in the history
Co-authored-by: gibbz00 <gabrielhansson@gmail.com>
  • Loading branch information
gibbz00 and gibbz00 committed Nov 4, 2023
1 parent b0b9996 commit 258c576
Show file tree
Hide file tree
Showing 28 changed files with 112 additions and 109 deletions.
6 changes: 3 additions & 3 deletions src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub struct ApplyArgs<'a, C: Connection> {
pub display_logs: bool,
pub dry_run: bool,
pub validate_version_order: bool,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub enum ApplyOperation {
Expand Down Expand Up @@ -316,7 +316,7 @@ fn expect_migration_definitions_to_be_up_to_date(

#[allow(clippy::too_many_arguments)]
async fn apply_migrations<C: Connection>(
config_file: Option<&str>,
config_file: Option<&Path>,
definitions_path: PathBuf,
migration_files_to_execute: Vec<SurqlFile>,
last_migration_applied: Option<&ScriptMigration>,
Expand Down Expand Up @@ -434,7 +434,7 @@ CREATE {} SET script_name = '{}';",

#[allow(clippy::too_many_arguments)]
async fn revert_migrations<C: Connection>(
config_file: Option<&str>,
config_file: Option<&Path>,
definitions_path: PathBuf,
migration_files_to_execute: Vec<SurqlFile>,
migrations_applied: &[ScriptMigration],
Expand Down
13 changes: 8 additions & 5 deletions src/branch/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{fs, path::PathBuf};
use std::{
fs,
path::{Path, PathBuf},
};

use color_eyre::eyre::{Result};
use surrealdb::{engine::any::Any, Surreal};
Expand All @@ -9,7 +12,7 @@ use super::constants::{BRANCH_NS, BRANCH_TABLE, ORIGIN_BRANCH_NS};

#[allow(deprecated)]
pub async fn create_branching_feature_client(
config_file: Option<&str>,
config_file: Option<&Path>,
db_configuration: &SurrealdbConfiguration,
) -> Result<Surreal<Any>> {
const BRANCH_DATA_NS: &str = "features";
Expand All @@ -30,7 +33,7 @@ pub async fn create_branching_feature_client(

#[allow(deprecated)]
pub async fn create_branch_client(
config_file: Option<&str>,
config_file: Option<&Path>,
branch_name: &String,
db_configuration: &SurrealdbConfiguration,
) -> Result<Surreal<Any>> {
Expand All @@ -49,7 +52,7 @@ pub async fn create_branch_client(

#[allow(deprecated)]
pub async fn create_origin_branch_client(
config_file: Option<&str>,
config_file: Option<&Path>,
branch_name: &String,
db_configuration: &SurrealdbConfiguration,
) -> Result<Surreal<Any>> {
Expand All @@ -68,7 +71,7 @@ pub async fn create_origin_branch_client(

#[allow(deprecated)]
pub async fn create_main_branch_client(
config_file: Option<&str>,
config_file: Option<&Path>,
db_configuration: &SurrealdbConfiguration,
branch: &Branch,
) -> Result<Surreal<Any>> {
Expand Down
6 changes: 3 additions & 3 deletions src/branch/diff.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use color_eyre::eyre::{eyre, ContextCompat, Result};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::{collections::HashMap, path::Path};

use crate::{input::SurrealdbConfiguration, models::Branch, surrealdb::create_surrealdb_client};

Expand All @@ -13,7 +13,7 @@ use super::{
pub struct BranchDiffArgs<'a> {
pub name: String,
pub db_configuration: &'a SurrealdbConfiguration,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: BranchDiffArgs<'_>) -> Result<()> {
Expand Down Expand Up @@ -181,7 +181,7 @@ struct SurrealdbInfoForTableResponse {
}

async fn get_surrealdb_database_definition(
config_file: Option<&str>,
config_file: Option<&Path>,
db_configuration: SurrealdbConfiguration,
) -> Result<SurrealdbDatabaseDefinition> {
let client = create_surrealdb_client(config_file, &db_configuration).await?;
Expand Down
5 changes: 3 additions & 2 deletions src/branch/list.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use color_eyre::eyre::{Result};
use chrono::{DateTime, Utc};
use chrono_human_duration::ChronoHumanDuration;
use cli_table::{format::Border, Cell, ColorChoice, Style, Table};
use color_eyre::eyre::Result;
use std::path::Path;

use crate::{
branch::{
Expand All @@ -15,7 +16,7 @@ use crate::{
pub struct ListBranchArgs<'a> {
pub db_configuration: &'a SurrealdbConfiguration,
pub no_color: bool,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: ListBranchArgs<'_>) -> Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion src/branch/merge/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use color_eyre::eyre::{eyre, Result};
use std::path::Path;

use crate::{
branch::constants::BRANCH_TABLE, cli::BranchMergeMode, input::SurrealdbConfiguration,
Expand All @@ -15,7 +16,7 @@ pub struct MergeBranchArgs<'a> {
pub name: String,
pub mode: BranchMergeMode,
pub db_configuration: &'a SurrealdbConfiguration,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: MergeBranchArgs<'_>) -> Result<()> {
Expand Down
8 changes: 4 additions & 4 deletions src/branch/merge/overwrite.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::eyre::{Result};
use std::path::PathBuf;
use color_eyre::eyre::Result;
use std::path::{Path, PathBuf};

use crate::{
branch::{
Expand All @@ -18,7 +18,7 @@ use crate::{
pub struct MergeOverwriteBranchArgs<'a> {
pub branch: Branch,
pub db_configuration: &'a SurrealdbConfiguration,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: MergeOverwriteBranchArgs<'_>) -> Result<()> {
Expand Down Expand Up @@ -53,7 +53,7 @@ pub async fn main(args: MergeOverwriteBranchArgs<'_>) -> Result<()> {
}

async fn apply_changes_to_main_branch(
config_file: Option<&str>,
config_file: Option<&Path>,
db_configuration: &SurrealdbConfiguration,
branch: &Branch,
dump_file_path: &PathBuf,
Expand Down
10 changes: 5 additions & 5 deletions src/branch/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::Utc;
use color_eyre::eyre::{eyre, ContextCompat, Result};
use include_dir::{include_dir, Dir};
use names::{Generator, Name};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use surrealdb::{engine::any::Any, Surreal};

use crate::{
Expand All @@ -27,7 +27,7 @@ use super::{
pub struct NewBranchArgs<'a> {
pub name: Option<String>,
pub db_configuration: &'a SurrealdbConfiguration,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: NewBranchArgs<'_>) -> Result<()> {
Expand Down Expand Up @@ -138,7 +138,7 @@ fn fails_if_branch_already_exists(
}

async fn export_original_branch_data_in_dump_file(
config_file: Option<&str>,
config_file: Option<&Path>,
db_configuration: &SurrealdbConfiguration,
dump_file_path: PathBuf,
) -> Result<()> {
Expand All @@ -149,7 +149,7 @@ async fn export_original_branch_data_in_dump_file(
}

async fn replicate_database_into_branch(
config_file: Option<&str>,
config_file: Option<&Path>,
name: Option<String>,
existing_branch_names: Vec<String>,
db_configuration: &SurrealdbConfiguration,
Expand Down Expand Up @@ -190,7 +190,7 @@ fn generate_random_branch_name(existing_branch_names: Vec<String>) -> Result<Str
}

async fn import_branch_data_from_dump_file(
config_file: Option<&str>,
config_file: Option<&Path>,
branch_name: &String,
db_configuration: &SurrealdbConfiguration,
dump_file_path: PathBuf,
Expand Down
3 changes: 2 additions & 1 deletion src/branch/remove.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use color_eyre::eyre::{eyre, Result};
use std::path::Path;

use crate::{
branch::{
Expand All @@ -15,7 +16,7 @@ use crate::{
pub struct RemoveBranchArgs<'a> {
pub name: String,
pub db_configuration: &'a SurrealdbConfiguration,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: RemoveBranchArgs<'_>) -> Result<()> {
Expand Down
3 changes: 2 additions & 1 deletion src/branch/status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use chrono::{DateTime, Utc};
use chrono_human_duration::ChronoHumanDuration;
use color_eyre::eyre::{eyre, Result};
use std::path::Path;

use crate::{
branch::{
Expand All @@ -13,7 +14,7 @@ use crate::{

pub struct BranchStatusArgs<'a> {
pub name: String,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub async fn main(args: BranchStatusArgs<'_>) -> Result<()> {
Expand Down
4 changes: 3 additions & 1 deletion src/cli/args.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};

use super::{ApplyArgs, BranchArgs, CreateArgs, ListArgs, ScaffoldAction};
Expand All @@ -12,7 +14,7 @@ pub struct Args {
/// Path to the configuration file
/// Default value is `.surrealdb`.
#[clap(long, global = true)]
pub config_file: Option<String>,
pub config_file: Option<PathBuf>,
}

#[derive(Subcommand, Debug)]
Expand Down
10 changes: 6 additions & 4 deletions src/config/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use ini::Ini;
use std::path::Path;

pub fn load_config(config_file: Option<&str>) -> Option<Ini> {
let config_file_path = config_file.unwrap_or(".surrealdb");
let surrealdb_config_file = Path::new(&config_file_path);
pub fn load_config(config_file: Option<&Path>) -> Option<Ini> {
let ini = match config_file {
Some(config_file) => Ini::load_from_file(config_file),
None => Ini::load_from_file(".surrealdb"),
};

Ini::load_from_file(surrealdb_config_file).ok()
ini.ok()
}

pub fn retrieve_config_value(config: &Ini, section: &str, key: &str) -> Option<String> {
Expand Down
6 changes: 4 additions & 2 deletions src/config/core.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use super::common::{load_config, retrieve_config_value};

#[allow(dead_code)]
Expand All @@ -6,7 +8,7 @@ pub enum TableSchemaDesign {
Schemaless,
}

pub fn retrieve_folder_path(config_file: Option<&str>) -> Option<String> {
pub fn retrieve_folder_path(config_file: Option<&Path>) -> Option<String> {
let config = load_config(config_file);

if let Some(config) = config {
Expand All @@ -17,7 +19,7 @@ pub fn retrieve_folder_path(config_file: Option<&str>) -> Option<String> {
}

#[allow(dead_code)]
pub fn retrieve_table_schema_design(config_file: Option<&str>) -> Option<TableSchemaDesign> {
pub fn retrieve_table_schema_design(config_file: Option<&Path>) -> Option<TableSchemaDesign> {
let config = load_config(config_file);

let schema_str = if let Some(config) = config {
Expand Down
4 changes: 3 additions & 1 deletion src/config/db.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::path::Path;

use super::common::{load_config, retrieve_config_value};

#[allow(dead_code)]
Expand All @@ -11,7 +13,7 @@ pub struct DbConfig {
}

#[allow(dead_code)]
pub fn retrieve_db_config(config_file: Option<&str>) -> DbConfig {
pub fn retrieve_db_config(config_file: Option<&Path>) -> DbConfig {
let config = load_config(config_file);

if let Some(config) = config {
Expand Down
8 changes: 4 additions & 4 deletions src/create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use color_eyre::eyre::{eyre, Result};
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crate::{
config::{self, retrieve_table_schema_design},
Expand All @@ -10,7 +10,7 @@ use crate::{
pub struct CreateArgs<'a> {
pub name: String,
pub operation: CreateOperation,
pub config_file: Option<&'a str>,
pub config_file: Option<&'a Path>,
}

pub enum CreateOperation {
Expand Down Expand Up @@ -119,7 +119,7 @@ fn get_filename(operation: &CreateOperation, name: &String) -> String {
}

fn generate_file_content(
config_file: Option<&str>,
config_file: Option<&Path>,
operation: &CreateOperation,
name: String,
) -> Result<String> {
Expand Down Expand Up @@ -159,7 +159,7 @@ DEFINE EVENT {0} ON TABLE {0} WHEN $event == \"CREATE\" THEN (
}

fn get_table_schema_design_str(
config_file: Option<&str>,
config_file: Option<&Path>,
schemafull: bool,
) -> Result<&'static str> {
const SCHEMAFULL: &str = "SCHEMAFULL";
Expand Down
Loading

0 comments on commit 258c576

Please sign in to comment.