|
- use std::io::prelude::*;
- use std::fs::{File, create_dir};
- 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<()> {
- 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();
- if !path.exists() {
- create_directory(&path)?;
- }
- Ok(())
- }
-
- /// 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(())
- }
-
- /// 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();
-
- let mut content = String::new();
- File::open(path)
- .chain_err(|| format!("Failed to open '{:?}'", path.display()))?
- .read_to_string(&mut content)?;
-
- Ok(content)
- }
|