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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use std::path::Path;

use anyhow::Result;
use tokio::{
    fs::{create_dir_all, File, OpenOptions},
    io::AsyncWriteExt,
    spawn,
    task::JoinHandle,
};

async fn create_parent_dirs_for(path: &Path) -> Result<()> {
    create_dir_all(path.parent().unwrap()).await?;
    Ok(())
}

pub async fn create_file<P>(name: P) -> Result<File>
where
    P: AsRef<Path>,
{
    create_parent_dirs_for(name.as_ref()).await?;
    let file = File::create(name).await?;
    Ok(file)
}

pub async fn save_file<P, B>(name: P, bytes: B) -> Result<()>
where
    P: AsRef<Path>,
    B: AsRef<[u8]>,
{
    let mut file = create_file(name).await?;
    file.write_all(bytes.as_ref()).await?;
    Ok(())
}

pub async fn append_file<P>(name: P) -> Result<File>
where
    P: AsRef<Path>,
{
    create_parent_dirs_for(name.as_ref()).await?;
    let file = OpenOptions::new()
        .append(true)
        .create(true)
        .open(&name)
        .await?;
    Ok(file)
}

pub async fn append_to_file<P>(name: P, bytes: &[u8]) -> Result<()>
where
    P: AsRef<Path>,
{
    let mut file = append_file(name).await?;
    file.write_all(bytes).await?;
    Ok(())
}

#[derive(Debug)]
pub struct Writer {
    pub handle: JoinHandle<Result<()>>,
}

impl Writer {
    pub async fn spawn<P, B>(name: P, bytes: B) -> Self
    where
        P: AsRef<Path>,
        B: AsRef<[u8]>,
    {
        let name = name.as_ref().to_owned();
        let bytes = bytes.as_ref().to_owned();
        Self {
            handle: spawn(async {
                save_file(name, bytes).await?;
                Ok(())
            }),
        }
    }

    pub async fn wait(self) -> Result<()> {
        self.handle.await?
    }
}