From f4ad03cee0229914c5c8d1144266e9f583d9f08b Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Mon, 22 Feb 2021 23:13:50 +0300 Subject: [PATCH] refac: use join instead path builder --- migra-cli/src/commands/apply.rs | 12 +++--- migra-cli/src/commands/make.rs | 22 +++++------ migra-cli/src/config.rs | 9 +---- migra-cli/src/main.rs | 1 - migra-cli/src/migration.rs | 5 +-- migra-cli/src/path.rs | 70 --------------------------------- 6 files changed, 20 insertions(+), 99 deletions(-) delete mode 100644 migra-cli/src/path.rs diff --git a/migra-cli/src/commands/apply.rs b/migra-cli/src/commands/apply.rs index c9cadb8..56326ac 100644 --- a/migra-cli/src/commands/apply.rs +++ b/migra-cli/src/commands/apply.rs @@ -1,17 +1,19 @@ use crate::config::Config; use crate::migration::{DatabaseMigrationManager, MigrationManager}; use crate::opts::ApplyCommandOpt; -use crate::path::PathBuilder; use crate::StdResult; use std::convert::TryFrom; pub(crate) fn apply_sql(config: Config, opts: ApplyCommandOpt) -> StdResult<()> { let mut manager = MigrationManager::try_from(&config)?; - let file_path = PathBuilder::from(config.directory_path()) - .append(opts.file_name) - .default_extension("sql") - .build(); + let file_path = { + let mut file_path = config.directory_path().join(opts.file_name); + if file_path.extension().is_none() { + file_path.set_extension("sql"); + } + file_path + }; let content = std::fs::read_to_string(file_path)?; diff --git a/migra-cli/src/commands/make.rs b/migra-cli/src/commands/make.rs index d02cfd7..1a3e3d4 100644 --- a/migra-cli/src/commands/make.rs +++ b/migra-cli/src/commands/make.rs @@ -1,8 +1,8 @@ use crate::opts::MakeCommandOpt; -use crate::path::PathBuilder; use crate::Config; use crate::StdResult; use chrono::Local; +use std::fs; pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult<()> { let now = Local::now().format("%y%m%d%H%M%S"); @@ -17,25 +17,21 @@ pub(crate) fn make_migration(config: Config, opts: MakeCommandOpt) -> StdResult< }) .collect(); - let migration_dir_path = PathBuilder::from(config.migration_dir_path()) - .append(format!("{}_{}", now, migration_name)) - .build(); + let migration_dir_path = config + .migration_dir_path() + .join(format!("{}_{}", now, migration_name)); if !migration_dir_path.exists() { - std::fs::create_dir_all(&migration_dir_path)?; + fs::create_dir_all(&migration_dir_path)?; } - let upgrade_migration_path = PathBuilder::from(&migration_dir_path) - .append("up.sql") - .build(); + let upgrade_migration_path = &migration_dir_path.join("up.sql"); if !upgrade_migration_path.exists() { - std::fs::write(upgrade_migration_path, "-- Your SQL goes here\n\n")?; + fs::write(upgrade_migration_path, "-- Your SQL goes here\n\n")?; } - let downgrade_migration_path = PathBuilder::from(&migration_dir_path) - .append("down.sql") - .build(); + let downgrade_migration_path = &migration_dir_path.join("down.sql"); if !downgrade_migration_path.exists() { - std::fs::write( + fs::write( downgrade_migration_path, "-- This file should undo anything in `up.sql`\n\n", )?; diff --git a/migra-cli/src/config.rs b/migra-cli/src/config.rs index a9e0494..2cb0869 100644 --- a/migra-cli/src/config.rs +++ b/migra-cli/src/config.rs @@ -1,6 +1,5 @@ use crate::error::{Error, MigraResult}; use crate::migration::Migration; -use crate::path::PathBuilder; use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; use std::{env, fs, io}; @@ -108,15 +107,11 @@ impl Config { impl Config { pub fn directory_path(&self) -> PathBuf { - PathBuilder::from(&self.manifest_root) - .append(&self.root) - .build() + self.manifest_root.join(&self.root) } pub fn migration_dir_path(&self) -> PathBuf { - PathBuilder::from(&self.directory_path()) - .append("migrations") - .build() + self.directory_path().join("migrations") } pub fn migrations(&self) -> MigraResult> { diff --git a/migra-cli/src/main.rs b/migra-cli/src/main.rs index 2ae301c..3a28b1c 100644 --- a/migra-cli/src/main.rs +++ b/migra-cli/src/main.rs @@ -8,7 +8,6 @@ mod databases; mod error; mod migration; mod opts; -mod path; use crate::error::StdResult; use config::Config; diff --git a/migra-cli/src/migration.rs b/migra-cli/src/migration.rs index d42390a..bf44e2b 100644 --- a/migra-cli/src/migration.rs +++ b/migra-cli/src/migration.rs @@ -1,7 +1,6 @@ use crate::config::Config; use crate::database::DatabaseConnection; use crate::databases::DatabaseConnectionManager; -use crate::path::PathBuilder; use crate::StdResult; use std::convert::TryFrom; use std::fs; @@ -21,8 +20,8 @@ impl Migration { .file_name() .and_then(|name| name.to_str()) .unwrap_or_default(); - let upgrade_sql = PathBuilder::from(directory).append("up.sql").build(); - let downgrade_sql = PathBuilder::from(directory).append("down.sql").build(); + let upgrade_sql = directory.join("up.sql"); + let downgrade_sql = directory.join("down.sql"); if upgrade_sql.exists() && downgrade_sql.exists() { return Some(Migration { diff --git a/migra-cli/src/path.rs b/migra-cli/src/path.rs deleted file mode 100644 index eb7ca42..0000000 --- a/migra-cli/src/path.rs +++ /dev/null @@ -1,70 +0,0 @@ -use std::ffi::OsStr; -use std::path::{Path, PathBuf}; - -#[derive(Debug, Default)] -pub struct PathBuilder { - buf: PathBuf, -} - -impl> From

for PathBuilder { - fn from(path: P) -> Self { - PathBuilder { - buf: path.as_ref().to_path_buf(), - } - } -} - -impl PathBuilder { - pub fn append>(&mut self, path: P) -> &mut Self { - self.buf.push(path); - self - } - - pub fn default_extension>(&mut self, extension: S) -> &mut Self { - if self.buf.as_path().extension().is_none() { - self.buf.set_extension(extension); - } - self - } - - pub fn build(&self) -> PathBuf { - self.buf.clone() - } -} - -#[cfg(test)] -mod tests { - use super::Path; - use super::PathBuilder; - - #[test] - fn create_path_builder() { - let path = PathBuilder::from("test").build(); - - assert_eq!(path, Path::new("test")) - } - - #[test] - fn append_path_to_builder() { - let path = PathBuilder::from("directory").append("schema.sql").build(); - - assert_eq!(path, Path::new("directory/schema.sql")) - } - - #[test] - fn add_default_extension_for_path() { - let path = PathBuilder::from("directory") - .append("schema") - .default_extension("sql") - .build(); - - assert_eq!(path, Path::new("directory/schema.sql")); - } - - #[test] - fn build_default_path() { - let path = PathBuilder::default().build(); - - assert_eq!(path, Path::new("")); - } -}