Archived
1
0
Fork 0

refac(core): impl from path trait for builder

This commit is contained in:
Dmitriy Pleshevskiy 2021-02-01 23:50:40 +03:00
parent 49f14be548
commit cc6899d416

View file

@ -6,12 +6,20 @@ pub struct PathBuilder {
buf: PathBuf, buf: PathBuf,
} }
impl PathBuilder { impl<P: AsRef<Path>> From<P> for PathBuilder {
pub fn new<P: AsRef<Path>>(path: P) -> Self { fn from(path: P) -> Self {
PathBuilder { PathBuilder {
buf: path.as_ref().to_path_buf(), buf: path.as_ref().to_path_buf(),
} }
} }
}
impl PathBuilder {
pub fn new<P: AsRef<Path>>() -> Self {
PathBuilder {
buf: PathBuf::new(),
}
}
pub fn append<P: AsRef<Path>>(&mut self, path: P) -> &mut Self { pub fn append<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
self.buf.push(path); self.buf.push(path);
@ -37,21 +45,21 @@ mod tests {
#[test] #[test]
fn create_path_builder() { fn create_path_builder() {
let path = PathBuilder::new("test").build(); let path = PathBuilder::from("test").build();
assert_eq!(path, Path::new("test")) assert_eq!(path, Path::new("test"))
} }
#[test] #[test]
fn append_path_to_builder() { fn append_path_to_builder() {
let path = PathBuilder::new("directory").append("schema.sql").build(); let path = PathBuilder::from("directory").append("schema.sql").build();
assert_eq!(path, Path::new("directory/schema.sql")) assert_eq!(path, Path::new("directory/schema.sql"))
} }
#[test] #[test]
fn add_default_extension_for_path() { fn add_default_extension_for_path() {
let path = PathBuilder::new("directory") let path = PathBuilder::from("directory")
.append("schema") .append("schema")
.default_extension("sql") .default_extension("sql")
.build(); .build();