use std::fmt::Debug;
use std::fmt::Formatter;
use std::fmt::Result as FmtResult;
use crate::FileSystem;
use crate::error::FsResult;
use crate::path::Path;
use crate::path::PathComponent;
use crate::path::RelativePath;
use crate::spi::TempResourceSpi;
use crate::temp::PersistFailure;
use crate::temp::PersistOptions;
use crate::temp::PersistOutcome;
use crate::temp::TempResourceState;
use crate::temp::internal::SyncTempResource;
pub struct TempDirectory {
core: SyncTempResource,
}
impl TempDirectory {
pub(crate) fn new(filesystem: FileSystem, path: Path, session: Box<dyn TempResourceSpi>) -> Self {
Self {
core: SyncTempResource::new(
filesystem,
path,
session,
"temporary directory cannot perform this lifecycle operation",
),
}
}
#[inline]
#[must_use]
pub const fn path(&self) -> &Path {
self.core.path()
}
#[inline]
pub const fn state(&self) -> TempResourceState {
self.core.state()
}
#[inline]
#[must_use]
pub fn child(&self, component: &PathComponent) -> Path {
self.core.path().child(component)
}
#[inline]
#[must_use]
pub fn descendant(&self, relative: &RelativePath) -> Path {
self.core.path().join(relative)
}
#[allow(clippy::result_large_err)]
pub fn persist(&mut self, target: &Path, options: PersistOptions) -> Result<PersistOutcome, PersistFailure> {
self.core.persist(target, options)
}
#[allow(clippy::result_large_err)]
pub fn keep(&mut self) -> Result<PersistOutcome, PersistFailure> {
self.core.keep()
}
pub fn cleanup(&mut self) -> FsResult<()> {
self.core.cleanup()
}
}
impl Debug for TempDirectory {
#[inline]
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.debug_struct("TempDirectory")
.field("path", self.core.path())
.field("state", &self.core.state())
.finish_non_exhaustive()
}
}