From 59b4b07cb39685be3961758223f3d9fdafcf78fe Mon Sep 17 00:00:00 2001 From: Vincent Prouillet Date: Tue, 16 May 2017 14:54:50 +0900 Subject: [PATCH] Use Path for fs utils rather than AsRef --- src/bin/cmd/init.rs | 6 +++--- src/fs.rs | 12 ++++-------- src/site.rs | 20 ++++++++++---------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/src/bin/cmd/init.rs b/src/bin/cmd/init.rs index 210f06e..3313299 100644 --- a/src/bin/cmd/init.rs +++ b/src/bin/cmd/init.rs @@ -16,8 +16,8 @@ base_url = "https://example.com" "#; -pub fn create_new_project>(name: P) -> Result<()> { - let path = name.as_ref(); +pub fn create_new_project(name: &str) -> Result<()> { + let path = Path::new(name); // Better error message than the rust default if path.exists() && path.is_dir() { @@ -26,7 +26,7 @@ pub fn create_new_project>(name: P) -> Result<()> { // main folder create_dir(path)?; - create_file(path.join("config.toml"), CONFIG.trim_left())?; + create_file(&path.join("config.toml"), CONFIG.trim_left())?; // content folder create_dir(path.join("content"))?; diff --git a/src/fs.rs b/src/fs.rs index 43572cc..9500e65 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -5,15 +5,14 @@ use std::path::Path; use errors::{Result, ResultExt}; /// Create a file with the content given -pub fn create_file>(path: P, content: &str) -> Result<()> { +pub fn create_file(path: &Path, content: &str) -> Result<()> { let mut file = File::create(&path)?; file.write_all(content.as_bytes())?; Ok(()) } /// Create a directory at the given path if it doesn't exist already -pub fn ensure_directory_exists>(path: P) -> Result<()> { - let path = path.as_ref(); +pub fn ensure_directory_exists(path: &Path) -> Result<()> { if !path.exists() { create_directory(&path)?; } @@ -22,8 +21,7 @@ pub fn ensure_directory_exists>(path: P) -> Result<()> { /// Very similar to `create_dir` from the std except it checks if the folder /// exists before creating it -pub fn create_directory>(path: P) -> Result<()> { - let path = path.as_ref(); +pub fn create_directory(path: &Path) -> Result<()> { if !path.exists() { create_dir(path) .chain_err(|| format!("Was not able to create folder {}", path.display()))?; @@ -32,9 +30,7 @@ pub fn create_directory>(path: P) -> Result<()> { } /// Return the content of a file, with error handling added -pub fn read_file>(path: P) -> Result { - let path = path.as_ref(); - +pub fn read_file(path: &Path) -> Result { let mut content = String::new(); File::open(path) .chain_err(|| format!("Failed to open '{:?}'", path.display()))? diff --git a/src/site.rs b/src/site.rs index 1d50c43..608a108 100644 --- a/src/site.rs +++ b/src/site.rs @@ -299,7 +299,7 @@ impl Site { // Finally, create a index.html file there with the page rendered let output = page.render_html(&self.tera, &self.config)?; - create_file(current_path.join("index.html"), &self.inject_livereload(output))?; + create_file(¤t_path.join("index.html"), &self.inject_livereload(output))?; // Copy any asset we found previously into the same directory as the index.html for asset in &page.assets { @@ -332,7 +332,7 @@ impl Site { pub fn render_robots(&self) -> Result<()> { ensure_directory_exists(&self.output_path)?; create_file( - self.output_path.join("robots.txt"), + &self.output_path.join("robots.txt"), &self.tera.render("robots.txt", &Context::new())? ) } @@ -361,14 +361,14 @@ impl Site { let output_path = self.output_path.join(&taxonomy.get_list_name()); let list_output = taxonomy.render_list(&self.tera, &self.config)?; create_directory(&output_path)?; - create_file(output_path.join("index.html"), &self.inject_livereload(list_output))?; + create_file(&output_path.join("index.html"), &self.inject_livereload(list_output))?; for item in &taxonomy.items { let single_output = taxonomy.render_single_item(item, &self.tera, &self.config)?; create_directory(&output_path.join(&item.slug))?; create_file( - output_path.join(&item.slug).join("index.html"), + &output_path.join(&item.slug).join("index.html"), &self.inject_livereload(single_output) )?; } @@ -410,7 +410,7 @@ impl Site { let sitemap = self.tera.render("sitemap.xml", &context)?; - create_file(self.output_path.join("sitemap.xml"), &sitemap)?; + create_file(&self.output_path.join("sitemap.xml"), &sitemap)?; Ok(()) } @@ -443,7 +443,7 @@ impl Site { let sitemap = self.tera.render("rss.xml", &context)?; - create_file(self.output_path.join("rss.xml"), &sitemap)?; + create_file(&self.output_path.join("rss.xml"), &sitemap)?; Ok(()) } @@ -489,7 +489,7 @@ impl Site { &self.tera, &self.config, )?; - create_file(output_path.join("index.html"), &self.inject_livereload(output))?; + create_file(&output_path.join("index.html"), &self.inject_livereload(output))?; } Ok(()) @@ -535,10 +535,10 @@ impl Site { create_directory(&page_path)?; let output = paginator.render_pager(pager, self)?; if i > 0 { - create_file(page_path.join("index.html"), &self.inject_livereload(output))?; + create_file(&page_path.join("index.html"), &self.inject_livereload(output))?; } else { - create_file(output_path.join("index.html"), &self.inject_livereload(output))?; - create_file(page_path.join("index.html"), &render_redirect_template(§ion.permalink, &self.tera)?)?; + create_file(&output_path.join("index.html"), &self.inject_livereload(output))?; + create_file(&page_path.join("index.html"), &render_redirect_template(§ion.permalink, &self.tera)?)?; } }