zola/src/utils.rs

24 lines
643 B
Rust
Raw Normal View History

2016-12-13 06:22:24 +00:00
use std::io::prelude::*;
2017-03-10 11:39:58 +00:00
use std::fs::{File, create_dir};
2016-12-13 06:22:24 +00:00
use std::path::Path;
2017-03-10 11:39:58 +00:00
use errors::{Result, ResultExt};
2016-12-13 06:22:24 +00:00
pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
let mut file = File::create(&path)?;
file.write_all(content.as_bytes())?;
Ok(())
}
2017-03-10 11:39:58 +00:00
/// 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();
if !path.exists() {
create_dir(path)
.chain_err(|| format!("Was not able to create folder {}", path.display()))?;
}
Ok(())
}