Skip to content

Commit

Permalink
Allow persisting Utf8TempDir without changing its type (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
9999years authored Oct 18, 2024
1 parent 3fd1dfd commit 14616f4
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/utf8tempdir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,31 @@ use tempfile::TempDir;
#[derive(Debug)]
pub struct Utf8TempDir {
#[allow(dead_code)]
inner: TempDir,
inner: Option<TempDir>,
path: Utf8PathBuf,
}

impl Utf8TempDir {
pub fn new() -> miette::Result<Self> {
let inner = tempfile::tempdir().into_diagnostic()?;
let path = inner.path().to_owned().try_into().into_diagnostic()?;
Ok(Self { inner, path })
Ok(Self {
inner: Some(inner),
path,
})
}

/// Keep this directory when it goes out of scope, without changing its type.
pub fn persist(&mut self) {
let inner = self.inner.take();
if let Some(tempdir) = inner {
let _ = tempdir.into_path();
}
}

/// Keep this directory when it goes out of scope.
pub fn into_path(self) -> Utf8PathBuf {
let _ = self.inner.into_path();
pub fn into_path(mut self) -> Utf8PathBuf {
self.persist();
self.path
}

Expand Down

0 comments on commit 14616f4

Please sign in to comment.