feat(core): add path builder
This commit is contained in:
parent
7fa65c8197
commit
95e41a7b8d
3 changed files with 70 additions and 1 deletions
|
@ -1,4 +1,4 @@
|
|||
use postgres::{Client, NoTls, Error};
|
||||
use postgres::{Client, Error, NoTls};
|
||||
|
||||
pub fn connect(connection_string: &str) -> Result<Client, Error> {
|
||||
Client::connect(connection_string, NoTls)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#![deny(clippy::all)]
|
||||
|
||||
pub mod database;
|
||||
pub mod path;
|
||||
|
|
68
migra-core/src/path.rs
Normal file
68
migra-core/src/path.rs
Normal file
|
@ -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<P: AsRef<Path>>(path: P) -> Self {
|
||||
PathBuilder {
|
||||
buf: path.as_ref().to_path_buf(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn append<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
|
||||
self.buf.push(path);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn default_extension<S: AsRef<OsStr>>(&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(""));
|
||||
}
|
||||
}
|
Reference in a new issue