use crate::{errors::IoResultExt, Builder};
use camino::{Utf8Path, Utf8PathBuf};
use std::{
convert::{TryFrom, TryInto},
error,
ffi::OsStr,
fmt,
fs::File,
io,
io::{Read, Seek, SeekFrom, Write},
ops::Deref,
path::Path,
};
use tempfile::{NamedTempFile, TempPath};
pub fn tempfile() -> io::Result<File> {
tempfile::tempfile()
}
pub fn tempfile_in<P: AsRef<Path>>(dir: P) -> io::Result<File> {
tempfile::tempfile_in(dir)
}
#[derive(Debug)]
pub struct Utf8PathPersistError {
pub error: io::Error,
pub path: Utf8TempPath,
}
impl From<Utf8PathPersistError> for io::Error {
#[inline]
fn from(error: Utf8PathPersistError) -> io::Error {
error.error
}
}
impl From<Utf8PathPersistError> for Utf8TempPath {
#[inline]
fn from(error: Utf8PathPersistError) -> Utf8TempPath {
error.path
}
}
impl fmt::Display for Utf8PathPersistError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to persist temporary file path: {}", self.error)
}
}
impl error::Error for Utf8PathPersistError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&self.error)
}
}
pub struct Utf8TempPath {
inner: TempPath,
}
impl Utf8TempPath {
pub(crate) fn from_temp_path(inner: TempPath) -> io::Result<Self> {
let path: &Path = inner.as_ref();
Utf8PathBuf::try_from(path.to_path_buf()).map_err(|error| error.into_io_error())?;
Ok(Self { inner })
}
pub fn close(self) -> io::Result<()> {
self.inner.close()
}
pub fn persist<P: AsRef<Path>>(self, new_path: P) -> Result<(), Utf8PathPersistError> {
self.inner.persist(new_path.as_ref()).map_err(|error| {
Utf8PathPersistError {
error: error.error,
path: Self { inner: error.path },
}
})
}
pub fn persist_noclobber<P: AsRef<Path>>(
self,
new_path: P,
) -> Result<(), Utf8PathPersistError> {
self.inner
.persist_noclobber(new_path.as_ref())
.map_err(|error| {
Utf8PathPersistError {
error: error.error,
path: Self { inner: error.path },
}
})
}
pub fn keep(self) -> Result<Utf8PathBuf, Utf8PathPersistError> {
match self.inner.keep() {
Ok(path) => Ok(Utf8PathBuf::try_from(path).expect("invariant: path is UTF-8")),
Err(error) => {
Err(Utf8PathPersistError {
error: error.error,
path: Self { inner: error.path },
})
}
}
}
pub fn disable_cleanup(&mut self, disable_cleanup: bool) {
self.inner.disable_cleanup(disable_cleanup);
}
pub fn from_path(path: impl Into<Utf8PathBuf>) -> Self {
Self {
inner: TempPath::from_path(path.into()),
}
}
}
impl fmt::Debug for Utf8TempPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.inner.fmt(f)
}
}
impl Deref for Utf8TempPath {
type Target = Utf8Path;
fn deref(&self) -> &Utf8Path {
self.inner
.deref()
.try_into()
.expect("invariant: path is UTF-8")
}
}
impl AsRef<Utf8Path> for Utf8TempPath {
#[inline]
fn as_ref(&self) -> &Utf8Path {
let path: &Path = self.as_ref();
path.try_into().expect("invariant: path is valid UTF-8")
}
}
impl AsRef<Path> for Utf8TempPath {
#[inline]
fn as_ref(&self) -> &Path {
self.inner.as_ref()
}
}
impl AsRef<str> for Utf8TempPath {
#[inline]
fn as_ref(&self) -> &str {
let path: &Utf8Path = self.as_ref();
path.as_str()
}
}
impl AsRef<OsStr> for Utf8TempPath {
#[inline]
fn as_ref(&self) -> &OsStr {
let path: &Path = self.as_ref();
path.as_os_str()
}
}
pub struct NamedUtf8TempFile<F = File> {
inner: NamedTempFile<F>,
}
impl<F> NamedUtf8TempFile<F> {
pub(crate) fn from_temp_file(inner: NamedTempFile<F>) -> io::Result<Self> {
let path = inner.path();
Utf8PathBuf::try_from(path.to_owned()).map_err(|error| error.into_io_error())?;
Ok(Self { inner })
}
}
impl<F> fmt::Debug for NamedUtf8TempFile<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "NamedUtf8TempFile({})", self.path())
}
}
impl<F> AsRef<Utf8Path> for NamedUtf8TempFile<F> {
#[inline]
fn as_ref(&self) -> &Utf8Path {
self.path()
}
}
impl<F> AsRef<Path> for NamedUtf8TempFile<F> {
#[inline]
fn as_ref(&self) -> &Path {
self.path().as_std_path()
}
}
impl NamedUtf8TempFile<File> {
pub fn new() -> io::Result<NamedUtf8TempFile> {
Builder::new().tempfile()
}
pub fn new_in<P: AsRef<Utf8Path>>(dir: P) -> io::Result<NamedUtf8TempFile> {
Builder::new().tempfile_in(dir)
}
pub fn with_prefix<S: AsRef<str>>(prefix: S) -> io::Result<NamedUtf8TempFile> {
Builder::new().prefix(&prefix).tempfile()
}
pub fn with_prefix_in<S: AsRef<str>, P: AsRef<Utf8Path>>(
prefix: S,
dir: P,
) -> io::Result<NamedUtf8TempFile> {
Builder::new().prefix(&prefix).tempfile_in(dir)
}
pub fn with_suffix<S: AsRef<str>>(suffix: S) -> io::Result<NamedUtf8TempFile> {
Builder::new().suffix(&suffix).tempfile()
}
pub fn with_suffix_in<S: AsRef<str>, P: AsRef<Utf8Path>>(
suffix: S,
dir: P,
) -> io::Result<NamedUtf8TempFile> {
Builder::new().suffix(&suffix).tempfile_in(dir)
}
}
impl<F> NamedUtf8TempFile<F> {
#[inline]
pub fn path(&self) -> &Utf8Path {
self.inner
.path()
.try_into()
.expect("invariant: path is valid UTF-8")
}
pub fn close(self) -> io::Result<()> {
self.inner.close()
}
pub fn persist<P: AsRef<Path>>(self, new_path: P) -> Result<F, Utf8PersistError<F>> {
self.inner.persist(new_path).map_err(|error| {
Utf8PersistError {
file: NamedUtf8TempFile { inner: error.file },
error: error.error,
}
})
}
pub fn persist_noclobber<P: AsRef<Path>>(self, new_path: P) -> Result<F, Utf8PersistError<F>> {
self.inner.persist_noclobber(new_path).map_err(|error| {
Utf8PersistError {
file: NamedUtf8TempFile { inner: error.file },
error: error.error,
}
})
}
pub fn keep(self) -> Result<(F, Utf8PathBuf), Utf8PersistError<F>> {
match self.inner.keep() {
Ok((file, path)) => Ok((
file,
path.try_into().expect("invariant: path is valid UTF-8"),
)),
Err(error) => Err(Utf8PersistError {
file: NamedUtf8TempFile { inner: error.file },
error: error.error,
}),
}
}
pub fn disable_cleanup(&mut self, disable_cleanup: bool) {
self.inner.disable_cleanup(disable_cleanup);
}
pub fn as_file(&self) -> &F {
self.inner.as_file()
}
pub fn as_file_mut(&mut self) -> &mut F {
self.inner.as_file_mut()
}
pub fn into_file(self) -> F {
self.inner.into_file()
}
pub fn into_temp_path(self) -> Utf8TempPath {
Utf8TempPath::from_temp_path(self.inner.into_temp_path())
.expect("invariant: inner path is UTF-8")
}
pub fn into_parts(self) -> (F, Utf8TempPath) {
let (file, path) = self.inner.into_parts();
let path = Utf8TempPath::from_temp_path(path).expect("invariant: inner path is UTF-8");
(file, path)
}
pub fn from_parts(file: F, path: Utf8TempPath) -> Self {
let inner = NamedTempFile::from_parts(file, path.inner);
Self { inner }
}
}
impl NamedUtf8TempFile<File> {
pub fn reopen(&self) -> io::Result<File> {
self.inner.reopen()
}
}
impl<F: Read> Read for NamedUtf8TempFile<F> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.as_file_mut().read(buf).with_err_path(|| self.path())
}
}
impl Read for &NamedUtf8TempFile<File> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.as_file().read(buf).with_err_path(|| self.path())
}
}
impl<F: Write> Write for NamedUtf8TempFile<F> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.as_file_mut().write(buf).with_err_path(|| self.path())
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
self.as_file_mut().flush().with_err_path(|| self.path())
}
}
impl Write for &NamedUtf8TempFile<File> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.as_file().write(buf).with_err_path(|| self.path())
}
#[inline]
fn flush(&mut self) -> io::Result<()> {
self.as_file().flush().with_err_path(|| self.path())
}
}
impl<F: Seek> Seek for NamedUtf8TempFile<F> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.as_file_mut().seek(pos).with_err_path(|| self.path())
}
}
impl Seek for &NamedUtf8TempFile<File> {
fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.as_file().seek(pos).with_err_path(|| self.path())
}
}
#[cfg(unix)]
impl<F> std::os::unix::io::AsRawFd for NamedUtf8TempFile<F>
where
F: std::os::unix::io::AsRawFd,
{
#[inline]
fn as_raw_fd(&self) -> std::os::unix::io::RawFd {
self.as_file().as_raw_fd()
}
}
#[cfg(windows)]
impl<F> std::os::windows::io::AsRawHandle for NamedUtf8TempFile<F>
where
F: std::os::windows::io::AsRawHandle,
{
#[inline]
fn as_raw_handle(&self) -> std::os::windows::io::RawHandle {
self.as_file().as_raw_handle()
}
}
pub struct Utf8PersistError<F = File> {
pub error: io::Error,
pub file: NamedUtf8TempFile<F>,
}
impl<F> fmt::Debug for Utf8PersistError<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Utf8PersistError({:?})", self.error)
}
}
impl<F> From<Utf8PersistError<F>> for io::Error {
#[inline]
fn from(error: Utf8PersistError<F>) -> io::Error {
error.error
}
}
impl<F> From<Utf8PersistError<F>> for NamedUtf8TempFile<F> {
#[inline]
fn from(error: Utf8PersistError<F>) -> NamedUtf8TempFile<F> {
error.file
}
}
impl<F> fmt::Display for Utf8PersistError<F> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "failed to persist temporary file: {}", self.error)
}
}
impl<F> error::Error for Utf8PersistError<F> {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
Some(&self.error)
}
}