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,
}
impl PathBuilder {
pub fn new<P: AsRef<Path>>(path: P) -> Self {
impl<P: AsRef<Path>> From<P> for PathBuilder {
fn from(path: P) -> Self {
PathBuilder {
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 {
self.buf.push(path);
@ -37,21 +45,21 @@ mod tests {
#[test]
fn create_path_builder() {
let path = PathBuilder::new("test").build();
let path = PathBuilder::from("test").build();
assert_eq!(path, Path::new("test"))
}
#[test]
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"))
}
#[test]
fn add_default_extension_for_path() {
let path = PathBuilder::new("directory")
let path = PathBuilder::from("directory")
.append("schema")
.default_extension("sql")
.build();