mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
mktemp: move to thiserror
This commit is contained in:
parent
e7c3a4d018
commit
3fe1cbe71f
2 changed files with 17 additions and 44 deletions
|
@ -20,6 +20,7 @@ path = "src/mktemp.rs"
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
rand = { workspace = true }
|
rand = { workspace = true }
|
||||||
tempfile = { workspace = true }
|
tempfile = { workspace = true }
|
||||||
|
thiserror = { workspace = true }
|
||||||
uucore = { workspace = true }
|
uucore = { workspace = true }
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
|
|
|
@ -11,9 +11,7 @@ use uucore::error::{FromIo, UError, UResult, UUsageError};
|
||||||
use uucore::{format_usage, help_about, help_usage};
|
use uucore::{format_usage, help_about, help_usage};
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
use std::error::Error;
|
|
||||||
use std::ffi::OsStr;
|
use std::ffi::OsStr;
|
||||||
use std::fmt::Display;
|
|
||||||
use std::io::ErrorKind;
|
use std::io::ErrorKind;
|
||||||
use std::iter;
|
use std::iter;
|
||||||
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
|
use std::path::{Path, PathBuf, MAIN_SEPARATOR};
|
||||||
|
@ -25,6 +23,7 @@ use std::os::unix::prelude::PermissionsExt;
|
||||||
|
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
use tempfile::Builder;
|
use tempfile::Builder;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
const ABOUT: &str = help_about!("mktemp.md");
|
const ABOUT: &str = help_about!("mktemp.md");
|
||||||
const USAGE: &str = help_usage!("mktemp.md");
|
const USAGE: &str = help_usage!("mktemp.md");
|
||||||
|
@ -46,21 +45,35 @@ const TMPDIR_ENV_VAR: &str = "TMPDIR";
|
||||||
#[cfg(windows)]
|
#[cfg(windows)]
|
||||||
const TMPDIR_ENV_VAR: &str = "TMP";
|
const TMPDIR_ENV_VAR: &str = "TMP";
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
enum MkTempError {
|
enum MkTempError {
|
||||||
|
#[error("could not persist file {path}", path = .0.quote())]
|
||||||
PersistError(PathBuf),
|
PersistError(PathBuf),
|
||||||
|
|
||||||
|
#[error("with --suffix, template {template} must end in X", template = .0.quote())]
|
||||||
MustEndInX(String),
|
MustEndInX(String),
|
||||||
|
|
||||||
|
#[error("too few X's in template {template}", template = .0.quote())]
|
||||||
TooFewXs(String),
|
TooFewXs(String),
|
||||||
|
|
||||||
/// The template prefix contains a path separator (e.g. `"a/bXXX"`).
|
/// The template prefix contains a path separator (e.g. `"a/bXXX"`).
|
||||||
|
#[error("invalid template, {template}, contains directory separator", template = .0.quote())]
|
||||||
PrefixContainsDirSeparator(String),
|
PrefixContainsDirSeparator(String),
|
||||||
|
|
||||||
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
|
/// The template suffix contains a path separator (e.g. `"XXXa/b"`).
|
||||||
|
#[error("invalid suffix {suffix}, contains directory separator", suffix = .0.quote())]
|
||||||
SuffixContainsDirSeparator(String),
|
SuffixContainsDirSeparator(String),
|
||||||
|
|
||||||
|
#[error("invalid template, {template}; with --tmpdir, it may not be absolute", template = .0.quote())]
|
||||||
InvalidTemplate(String),
|
InvalidTemplate(String),
|
||||||
|
|
||||||
|
#[error("too many templates")]
|
||||||
TooManyTemplates,
|
TooManyTemplates,
|
||||||
|
|
||||||
/// When a specified temporary directory could not be found.
|
/// 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),
|
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.
|
/// Options parsed from the command-line.
|
||||||
///
|
///
|
||||||
/// This provides a layer of indirection between the application logic
|
/// This provides a layer of indirection between the application logic
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue