diff --git a/Cargo.lock b/Cargo.lock index 5f9809096c..0761018274 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5627,9 +5627,9 @@ dependencies = [ name = "tailcall-prettier" version = "0.1.0" dependencies = [ - "derive_more 0.99.18", "lazy_static", "strum_macros", + "thiserror", "tokio", ] diff --git a/tailcall-prettier/Cargo.toml b/tailcall-prettier/Cargo.toml index 5889cc37e3..ea5597e7d3 100644 --- a/tailcall-prettier/Cargo.toml +++ b/tailcall-prettier/Cargo.toml @@ -6,7 +6,7 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -derive_more = { workspace = true } lazy_static = "1.4.0" strum_macros = "0.26.2" +thiserror.workspace = true tokio.workspace = true diff --git a/tailcall-prettier/src/error.rs b/tailcall-prettier/src/error.rs index 40edd1bb5c..e975bcbbed 100644 --- a/tailcall-prettier/src/error.rs +++ b/tailcall-prettier/src/error.rs @@ -1,49 +1,44 @@ -use std::fmt::Display; +use std::borrow::Cow; use std::string::FromUtf8Error; use std::sync::Arc; -use derive_more::{DebugCustom, From}; +use thiserror::Error; use tokio::task::JoinError; -#[derive(From, DebugCustom)] +#[derive(Error)] pub enum Error { - #[debug(fmt = "Std IO Error: {}", _0)] - IO(std::io::Error), + #[error("Std IO Error: {0}")] + IO(#[from] std::io::Error), - #[debug(fmt = "Join Error: {}", _0)] - Join(JoinError), + #[error("Join Error: {0}")] + Join(#[from] JoinError), - #[debug(fmt = "From Utf8 Error: {}", _0)] - FromUtf8(FromUtf8Error), + #[error("From Utf8 Error: {0}")] + FromUtf8(#[from] FromUtf8Error), - #[debug(fmt = "Prettier formatting failed: {}", _0)] + #[error("Prettier formatting failed: {0}")] PrettierFormattingFailed(String), - #[debug(fmt = "No file extension found")] + #[error("{0} command was not found. Ensure you have it installed and available in the PATH")] + CommandNotFound(String), + + #[error("No file extension found")] FileExtensionNotFound, - #[debug(fmt = "Unsupported file type")] + #[error("Unsupported file type")] UnsupportedFiletype, - #[debug(fmt = "{}\n\nCaused by:\n {}", context, source)] - Context { source: Arc, context: String }, + #[error("{}\n\nCaused by:\n {}", context, source)] + Context { + #[source] + source: Arc, + context: String, + }, } -impl Display for Error { +impl std::fmt::Debug for Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - Error::IO(error) => write!(f, "Std IO Error: {}", error), - Error::Join(error) => write!(f, "Join Error: {}", error), - Error::FromUtf8(error) => write!(f, "From Utf8 Error: {}", error), - Error::PrettierFormattingFailed(msg) => { - write!(f, "Prettier formatting failed: {}", msg) - } - Error::FileExtensionNotFound => write!(f, "No file extension found"), - Error::UnsupportedFiletype => write!(f, "Unsupported file type"), - Error::Context { source, context } => { - write!(f, "{}\n\nCaused by:\n {}", context, source) - } - } + std::fmt::Display::fmt(self, f) } } @@ -51,6 +46,13 @@ impl Error { pub fn with_context(self, context: String) -> Self { Error::Context { source: Arc::new(self), context } } + + pub fn from_io_error(command: Cow<'static, str>) -> impl Fn(std::io::Error) -> Self { + move |error| match error.kind() { + std::io::ErrorKind::NotFound => Error::CommandNotFound(command.to_string()), + _ => Error::IO(error), + } + } } pub type Result = std::result::Result; diff --git a/tailcall-prettier/src/prettier.rs b/tailcall-prettier/src/prettier.rs index 1129452aa3..639fe7a479 100644 --- a/tailcall-prettier/src/prettier.rs +++ b/tailcall-prettier/src/prettier.rs @@ -71,7 +71,11 @@ impl Prettier { child = child.arg("--config").arg(config); } - let mut child = child.stdin(Stdio::piped()).stdout(Stdio::piped()).spawn()?; + let mut child = child + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .spawn() + .map_err(Error::from_io_error("prettier".into()))?; if let Some(ref mut stdin) = child.stdin { stdin.write_all(source.as_bytes())?;