parse_blogger_backup_xml/
utilities.rs

1use std::ffi::OsStr;
2use std::fs;
3use std::io;
4use std::io::Write as _;
5use std::path::Path;
6
7use crate::errors::EmptyResult;
8
9/// Copy a directory and all its contents to the destination directory.
10/// <https://stackoverflow.com/a/65192210>
11pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
12    fs::create_dir_all(&dst)?;
13    for entry in fs::read_dir(src)? {
14        let entry = entry?;
15        let ty = entry.file_type()?;
16        if ty.is_dir() {
17            copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
18        } else {
19            fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
20        }
21    }
22    Ok(())
23}
24
25pub fn save<S>(path: &S, text: String) -> EmptyResult
26where
27    S: AsRef<OsStr>,
28{
29    let file_path = Path::new(path);
30    if let Some(parent) = file_path.parent() {
31        fs::create_dir_all(parent)?;
32    }
33    match fs::remove_file(file_path) {
34        Ok(_) => (),
35        Err(_) => (),
36    };
37    let mut file = fs::OpenOptions::new()
38        .create(true)
39        .write(true)
40        .open(file_path)?;
41
42    file.write_all(text.as_bytes())?;
43    Ok(())
44}