From 947b9b54d61dc4f9c08f2cfc987dbf74942b9217 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 7 Jan 2023 10:12:56 +0200 Subject: refactor: move some errors into src/util.rs Sponsored-by: author --- src/error.rs | 24 +++--------------------- src/util.rs | 55 +++++++++++++++++++++++++++++++++++++++---------------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/src/error.rs b/src/error.rs index 6bc0c1d..640c023 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,6 +6,9 @@ pub enum SiteError { #[error(transparent)] WalkDir(#[from] crate::srcdir::SourceDirError), + #[error(transparent)] + Util(#[from] crate::util::UtilError), + #[error("could not read file: {0}")] FileRead(PathBuf, #[source] std::io::Error), @@ -18,21 +21,12 @@ pub enum SiteError { #[error("could not convert input text from {0} to UTF-8")] Utf8(PathBuf, #[source] std::string::FromUtf8Error), - #[error("failed to canonicalize path {0}")] - Canonicalize(PathBuf, #[source] std::io::Error), - #[error("failed to compute relative path for {0} against {1}")] Relative(PathBuf, PathBuf), #[error("failed to create {0}")] CreateFile(PathBuf, #[source] std::io::Error), - #[error("failed to copy {0} to {1}")] - CopyFile(PathBuf, PathBuf, #[source] std::io::Error), - - #[error("failed to create directory {0}")] - CreateDir(PathBuf, #[source] std::io::Error), - #[error("unknown directive {0}")] UnknownDirective(String), @@ -54,18 +48,6 @@ pub enum SiteError { #[error("attempt to use definition lists in Markdown: line {0}, column {1}")] DefinitionList(usize, usize), - #[error("failed to get file metadata: {0}")] - FileMetadata(PathBuf, #[source] std::io::Error), - - #[error("failed to get file modification time: {0}")] - FileMtime(PathBuf, #[source] std::io::Error), - - #[error("failed to set modification time for file {0}")] - Utimensat(PathBuf, #[source] std::io::Error), - - #[error("failed to convert time to Unix time")] - UnixTime(#[source] std::time::SystemTimeError), - #[error("failed to parse Unix timetamp: {0}")] ParseUnixTimestamp(String, #[source] std::num::ParseIntError), diff --git a/src/util.rs b/src/util.rs index bf78404..eac71f1 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,4 +1,3 @@ -use crate::error::SiteError; use libc::{timespec, utimensat, AT_FDCWD, AT_SYMLINK_NOFOLLOW}; use log::{debug, error, trace}; use std::ffi::CString; @@ -6,34 +5,58 @@ use std::os::unix::ffi::OsStrExt; use std::path::{Component, Path, PathBuf}; use std::time::{SystemTime, UNIX_EPOCH}; -pub fn canonicalize(path: &Path) -> Result { +#[derive(Debug, thiserror::Error)] +pub enum UtilError { + #[error("failed to canonicalize path {0}")] + Canonicalize(PathBuf, #[source] std::io::Error), + + #[error("failed to convert time to Unix time")] + UnixTime(#[source] std::time::SystemTimeError), + + #[error("failed to create directory {0}")] + CreateDir(PathBuf, #[source] std::io::Error), + + #[error("failed to copy {0} to {1}")] + CopyFile(PathBuf, PathBuf, #[source] std::io::Error), + + #[error("failed to get file metadata: {0}")] + FileMetadata(PathBuf, #[source] std::io::Error), + + #[error("failed to get file modification time: {0}")] + FileMtime(PathBuf, #[source] std::io::Error), + + #[error("failed to set modification time for file {0}")] + Utimensat(PathBuf, #[source] std::io::Error), +} + +pub fn canonicalize(path: &Path) -> Result { path.canonicalize() - .map_err(|e| SiteError::Canonicalize(path.into(), e)) + .map_err(|e| UtilError::Canonicalize(path.into(), e)) } -pub fn mkdir(path: &Path) -> Result<(), SiteError> { +pub fn mkdir(path: &Path) -> Result<(), UtilError> { debug!("creating directory {}", path.display()); - std::fs::create_dir_all(path).map_err(|e| SiteError::CreateDir(path.into(), e))?; + std::fs::create_dir_all(path).map_err(|e| UtilError::CreateDir(path.into(), e))?; Ok(()) } -pub fn copy(src: &Path, dest: &Path) -> Result<(), SiteError> { +pub fn copy(src: &Path, dest: &Path) -> Result<(), UtilError> { trace!("copying: {} -> {}", src.display(), dest.display()); - std::fs::copy(src, dest).map_err(|e| SiteError::CopyFile(src.into(), dest.into(), e))?; + std::fs::copy(src, dest).map_err(|e| UtilError::CopyFile(src.into(), dest.into(), e))?; let mtime = get_mtime(src)?; set_mtime(dest, mtime)?; Ok(()) } -pub fn get_mtime(src: &Path) -> Result { - let metadata = std::fs::metadata(src).map_err(|e| SiteError::FileMetadata(src.into(), e))?; +pub fn get_mtime(src: &Path) -> Result { + let metadata = std::fs::metadata(src).map_err(|e| UtilError::FileMetadata(src.into(), e))?; let mtime = metadata .modified() - .map_err(|e| SiteError::FileMtime(src.into(), e))?; + .map_err(|e| UtilError::FileMtime(src.into(), e))?; Ok(mtime) } -pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), SiteError> { +pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), UtilError> { trace!( "set_mtime: filename={} mtime={:?}", filename.display(), @@ -53,19 +76,19 @@ pub fn set_mtime(filename: &Path, mtime: SystemTime) -> Result<(), SiteError> { if utimensat(AT_FDCWD, path.as_ptr(), times, AT_SYMLINK_NOFOLLOW) == -1 { let error = std::io::Error::last_os_error(); error!("utimensat failed on {:?}", path); - return Err(SiteError::Utimensat(pathbuf, error)); + return Err(UtilError::Utimensat(pathbuf, error)); } } Ok(()) } -pub fn copy_file_from_source(filename: &Path, output: &Path) -> Result<(), SiteError> { +pub fn copy_file_from_source(filename: &Path, output: &Path) -> Result<(), UtilError> { debug!("copying {} -> {}", filename.display(), output.display()); if let Some(parent) = output.parent() { trace!("parent: {}", parent.display()); if !parent.exists() { trace!("create parent {}", parent.display()); - std::fs::create_dir_all(parent).map_err(|e| SiteError::CreateDir(parent.into(), e))?; + std::fs::create_dir_all(parent).map_err(|e| UtilError::CreateDir(parent.into(), e))?; } } else { trace!("does not have parent: {}", output.display()); @@ -116,10 +139,10 @@ pub fn make_path_absolute(path: &Path) -> PathBuf { Path::new("/").join(path) } -fn timespec(time: SystemTime) -> Result { +fn timespec(time: SystemTime) -> Result { let dur = time .duration_since(UNIX_EPOCH) - .map_err(SiteError::UnixTime)?; + .map_err(UtilError::UnixTime)?; let tv_sec = dur.as_secs() as libc::time_t; let tv_nsec = dur.subsec_nanos() as libc::c_long; Ok(timespec { tv_sec, tv_nsec }) -- cgit v1.2.1