Browse Source

Use Path for fs utils rather than AsRef

index-subcmd
Vincent Prouillet 7 years ago
parent
commit
59b4b07cb3
3 changed files with 17 additions and 21 deletions
  1. +3
    -3
      src/bin/cmd/init.rs
  2. +4
    -8
      src/fs.rs
  3. +10
    -10
      src/site.rs

+ 3
- 3
src/bin/cmd/init.rs View File

@@ -16,8 +16,8 @@ base_url = "https://example.com"
"#;


pub fn create_new_project<P: AsRef<Path>>(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<P: AsRef<Path>>(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"))?;


+ 4
- 8
src/fs.rs View File

@@ -5,15 +5,14 @@ use std::path::Path;
use errors::{Result, ResultExt};

/// Create a file with the content given
pub fn create_file<P: AsRef<Path>>(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<P: AsRef<Path>>(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<P: AsRef<Path>>(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<P: AsRef<Path>>(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<P: AsRef<Path>>(path: P) -> Result<()> {
}

/// Return the content of a file, with error handling added
pub fn read_file<P: AsRef<Path>>(path: P) -> Result<String> {
let path = path.as_ref();

pub fn read_file(path: &Path) -> Result<String> {
let mut content = String::new();
File::open(path)
.chain_err(|| format!("Failed to open '{:?}'", path.display()))?


+ 10
- 10
src/site.rs View File

@@ -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(&current_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(&section.permalink, &self.tera)?)?;
create_file(&output_path.join("index.html"), &self.inject_livereload(output))?;
create_file(&page_path.join("index.html"), &render_redirect_template(&section.permalink, &self.tera)?)?;
}
}



Loading…
Cancel
Save