1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use crate::prelude::*;
use std::path::PathBuf;
use std::fs;
use std::io::Write;
pub fn create_parent_directories(path: &PathBuf) -> Result<()> {
let file_dir = path
.parent()
.ok_or_else(||
format!("Failed to get the parent of {}.", path.display())
)?;
Ok(fs::create_dir_all(file_dir)?)
}
pub fn write_file(path: &PathBuf, content: String) -> Result<()> {
create_parent_directories(path)?;
let mut file = fs::File::create(path)?;
Ok(file.write_all(content.as_bytes())?)
}
pub fn copy(from: &PathBuf, to: &PathBuf) -> Result<()> {
create_parent_directories(to)?;
std::fs::copy(from, to)?;
Ok(())
}