File size: 870 Bytes
d90101d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use std::path::PathBuf;

use anyhow::Context;

pub struct TempDir {
    temp_dir: tempfile::TempDir,
}

impl TempDir {
    const START_MARKER: &'static str = "___START___";
    const END_MARKER: &'static str = "___END___";
    pub fn new() -> anyhow::Result<Self> {
        let temp_dir = Self::temp_dir()?;
        Ok(Self {
            temp_dir: tempfile::Builder::new()
                .prefix(Self::START_MARKER)
                .suffix(Self::END_MARKER)
                .tempdir_in(temp_dir.clone())
                .with_context(|| {
                    format!("failed to create temp directory in: {}", temp_dir.display())
                })?,
        })
    }

    pub fn path(&self) -> std::path::PathBuf {
        self.temp_dir.path().to_path_buf()
    }

    fn temp_dir() -> anyhow::Result<PathBuf> {
        Ok(std::env::temp_dir().canonicalize()?)
    }
}