From 95e41a7b8d683ddd02ec72ce81c78317f73e5f93 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Sun, 31 Jan 2021 14:49:34 +0300 Subject: [PATCH] feat(core): add path builder --- migra-core/src/database.rs | 2 +- migra-core/src/lib.rs | 1 + migra-core/src/path.rs | 68 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 migra-core/src/path.rs diff --git a/migra-core/src/database.rs b/migra-core/src/database.rs index 2971b39..62e072b 100644 --- a/migra-core/src/database.rs +++ b/migra-core/src/database.rs @@ -1,4 +1,4 @@ -use postgres::{Client, NoTls, Error}; +use postgres::{Client, Error, NoTls}; pub fn connect(connection_string: &str) -> Result { Client::connect(connection_string, NoTls) diff --git a/migra-core/src/lib.rs b/migra-core/src/lib.rs index d37e273..99766a7 100644 --- a/migra-core/src/lib.rs +++ b/migra-core/src/lib.rs @@ -1,3 +1,4 @@ #![deny(clippy::all)] pub mod database; +pub mod path; diff --git a/migra-core/src/path.rs b/migra-core/src/path.rs new file mode 100644 index 0000000..cc8ae73 --- /dev/null +++ b/migra-core/src/path.rs @@ -0,0 +1,68 @@ +use std::ffi::OsStr; +use std::path::{Path, PathBuf}; + +#[derive(Debug, Default)] +pub struct PathBuilder { + buf: PathBuf, +} + +impl PathBuilder { + pub fn new>(path: P) -> Self { + PathBuilder { + buf: path.as_ref().to_path_buf(), + } + } + + 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 crate::path::Path; + use crate::path::PathBuilder; + + #[test] + fn create_path_builder() { + let path = PathBuilder::new("test").build(); + + assert_eq!(path, Path::new("test")) + } + + #[test] + fn append_path_to_builder() { + let path = PathBuilder::new("directory").append("schema.sql").build(); + + assert_eq!(path, Path::new("directory/schema.sql")) + } + + #[test] + fn add_default_extension_for_path() { + let path = PathBuilder::new("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("")); + } +}