You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
643B

  1. use std::io::prelude::*;
  2. use std::fs::{File, create_dir};
  3. use std::path::Path;
  4. use errors::{Result, ResultExt};
  5. pub fn create_file<P: AsRef<Path>>(path: P, content: &str) -> Result<()> {
  6. let mut file = File::create(&path)?;
  7. file.write_all(content.as_bytes())?;
  8. Ok(())
  9. }
  10. /// Very similar to create_dir from the std except it checks if the folder
  11. /// exists before creating it
  12. pub fn create_directory<P: AsRef<Path>>(path: P) -> Result<()> {
  13. let path = path.as_ref();
  14. if !path.exists() {
  15. create_dir(path)
  16. .chain_err(|| format!("Was not able to create folder {}", path.display()))?;
  17. }
  18. Ok(())
  19. }