1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 20:17:45 +00:00

mktemp: move to thiserror

This commit is contained in:
Sylvestre Ledru 2025-01-20 18:18:07 +01:00
parent e7c3a4d018
commit 3fe1cbe71f
2 changed files with 17 additions and 44 deletions

View file

@ -20,6 +20,7 @@ path = "src/mktemp.rs"
clap = { workspace = true }
rand = { workspace = true }
tempfile = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true }
[[bin]]

View file

@ -11,9 +11,7 @@ use uucore::error::{FromIo, UError, UResult, UUsageError};
use uucore::{format_usage, help_about, help_usage};
use std::env;
use std::error::Error;
use std::ffi::OsStr;
use std::fmt::Display;
use std::io::ErrorKind;
use std::iter;
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
@ -25,6 +23,7 @@ use std::os::unix::prelude::PermissionsExt;
use rand::Rng;
use tempfile::Builder;
use thiserror::Error;
const ABOUT: &str = help_about!("mktemp.md");
const USAGE: &str = help_usage!("mktemp.md");
@ -46,21 +45,35 @@ const TMPDIR_ENV_VAR: &str = "TMPDIR";
#[cfg(windows)]
const TMPDIR_ENV_VAR: &str = "TMP";
#[derive(Debug)]
#[derive(Debug, Error)]
enum MkTempError {
#[error("could not persist file {path}", path = .0.quote())]
PersistError(PathBuf),
#[error("with --suffix, template {template} must end in X", template = .0.quote())]
MustEndInX(String),
#[error("too few X's in template {template}", template = .0.quote())]
TooFewXs(String),
/// The template prefix contains a path separator (e.g. `"a/bXXX"`).
#[error("invalid template, {template}, contains directory separator", template = .0.quote())]
PrefixContainsDirSeparator(String),
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
#[error("invalid suffix {suffix}, contains directory separator", suffix = .0.quote())]
SuffixContainsDirSeparator(String),
#[error("invalid template, {template}; with --tmpdir, it may not be absolute", template = .0.quote())]
InvalidTemplate(String),
#[error("too many templates")]
TooManyTemplates,
/// When a specified temporary directory could not be found.
#[error("failed to create {template_type} via template {template}: No such file or directory",
template_type = .0,
template = .1.quote())]
NotFound(String, String),
}
@ -70,47 +83,6 @@ impl UError for MkTempError {
}
}
impl Error for MkTempError {}
impl Display for MkTempError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use MkTempError::*;
match self {
PersistError(p) => write!(f, "could not persist file {}", p.quote()),
MustEndInX(s) => write!(f, "with --suffix, template {} must end in X", s.quote()),
TooFewXs(s) => write!(f, "too few X's in template {}", s.quote()),
PrefixContainsDirSeparator(s) => {
write!(
f,
"invalid template, {}, contains directory separator",
s.quote()
)
}
SuffixContainsDirSeparator(s) => {
write!(
f,
"invalid suffix {}, contains directory separator",
s.quote()
)
}
InvalidTemplate(s) => write!(
f,
"invalid template, {}; with --tmpdir, it may not be absolute",
s.quote()
),
TooManyTemplates => {
write!(f, "too many templates")
}
NotFound(template_type, s) => write!(
f,
"failed to create {} via template {}: No such file or directory",
template_type,
s.quote()
),
}
}
}
/// Options parsed from the command-line.
///
/// This provides a layer of indirection between the application logic