diff --git a/Cargo.lock b/Cargo.lock index ab3e87f..abfcf85 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,17 +109,17 @@ dependencies = [ [[package]] name = "dprint-core" -version = "0.67.4" +version = "0.68.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c1d827947704a9495f705d6aeed270fa21a67f825f22902c28f38dc3af7a9ae" +checksum = "7eb1dc2cac6929b352e64ee15d45cecb990f081eebbec99be0444edb01e642a8" dependencies = [ - "anyhow", "bumpalo", - "hashbrown", + "hashbrown 0.15.2", "indexmap", "rustc-hash", "serde", "serde_json", + "thiserror", "unicode-width 0.2.0", ] @@ -150,7 +150,6 @@ dependencies = [ name = "dprint-plugin-markdown" version = "0.22.1" dependencies = [ - "anyhow", "dprint-core", "dprint-core-macros", "dprint-development", @@ -158,6 +157,7 @@ dependencies = [ "regex", "serde", "serde_json", + "thiserror", "unicode-width 0.1.10", ] @@ -211,15 +211,22 @@ dependencies = [ "foldhash", ] +[[package]] +name = "hashbrown" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a" + [[package]] name = "indexmap" -version = "2.7.1" +version = "2.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652" +checksum = "d466e9454f08e4a911e14806c24e16fba1b4c121d1ea474396f396069cf949d9" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.17.1", "serde", + "serde_core", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 76ff9ce..f02d1e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,13 +29,13 @@ path = "tests/spec_test.rs" harness = false [dependencies] -anyhow = "1.0.64" -dprint-core = { version = "0.67.4", features = ["formatting"] } +dprint-core = { version = "0.68.2", features = ["formatting"] } dprint-core-macros = "0.1.0" pulldown-cmark = { version = "0.11.2", default-features = false } regex = "1" serde = { version = "1.0.144", features = ["derive"] } serde_json = { version = "1.0", optional = true } +thiserror = "2.0" unicode-width = "0.1.10" [dev-dependencies] diff --git a/src/format_text.rs b/src/format_text.rs index 2c5e2d8..8ee2c5b 100644 --- a/src/format_text.rs +++ b/src/format_text.rs @@ -1,5 +1,3 @@ -use anyhow::bail; -use anyhow::Result; use dprint_core::configuration::resolve_new_line_kind; use dprint_core::formatting::*; @@ -10,14 +8,31 @@ use super::generation::parse_cmark_ast; use super::generation::strip_metadata_header; use super::generation::Context; +/// Error that can occur while formatting markdown text. +#[derive(Debug, thiserror::Error)] +pub enum FormatError { + /// The text could not be parsed as markdown. + #[error("{0}")] + Parse(String), + /// An error occurred while formatting the text of a code block. + #[error("{0}")] + CodeBlock(Box), +} + +impl From for FormatError { + fn from(err: std::string::FromUtf8Error) -> Self { + FormatError::CodeBlock(err.into()) + } +} + /// Formats a file. /// /// Returns the file text or an error when it failed to parse. pub fn format_text( file_text: &str, config: &Configuration, - format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result>, -) -> Result> { + format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result, FormatError>, +) -> Result, FormatError> { let result = format_text_inner(file_text, config, format_code_block_text)?; match result { @@ -30,8 +45,8 @@ pub fn format_text( fn format_text_inner( file_text: &str, config: &Configuration, - format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result>, -) -> Result> { + format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result, FormatError>, +) -> Result, FormatError> { let file_text = strip_bom(file_text); let (source_file, markdown_text) = match parse_source_file(file_text, config)? { ParseFileResult::IgnoreFile => return Ok(None), @@ -54,7 +69,7 @@ fn format_text_inner( pub fn trace_file( file_text: &str, config: &Configuration, - format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result>, + format_code_block_text: impl for<'a> FnMut(&str, &'a str, u32) -> Result, FormatError>, ) -> dprint_core::formatting::TracingResult { let (source_file, markdown_text) = match parse_source_file(file_text, config).unwrap() { ParseFileResult::IgnoreFile => panic!("Cannot trace file because it has an ignore file comment."), @@ -80,7 +95,7 @@ enum ParseFileResult<'a> { SourceFile((crate::generation::common::SourceFile, &'a str)), } -fn parse_source_file<'a>(file_text: &'a str, config: &Configuration) -> Result> { +fn parse_source_file<'a>(file_text: &'a str, config: &Configuration) -> Result, FormatError> { // check for the presence of a dprint-ignore-file comment before parsing if file_has_ignore_file_directive(strip_metadata_header(file_text), &config.ignore_file_directive) { return Ok(ParseFileResult::IgnoreFile); @@ -88,14 +103,13 @@ fn parse_source_file<'a>(file_text: &'a str, config: &Configuration) -> Result

Ok(ParseFileResult::SourceFile((source_file, file_text))), - Err(error) => bail!( - "{}", + Err(error) => Err(FormatError::Parse( dprint_core::formatting::utils::string_utils::format_diagnostic( Some((error.range.start, error.range.end)), &error.message, - file_text - ) - ), + file_text, + ), + )), } } diff --git a/src/generation/gen_types.rs b/src/generation/gen_types.rs index b1933db..f08a5f2 100644 --- a/src/generation/gen_types.rs +++ b/src/generation/gen_types.rs @@ -8,9 +8,9 @@ use regex::Regex; use super::utils::*; use crate::configuration::Configuration; use crate::format_text; -use anyhow::Result; +use crate::format_text::FormatError; -type FormatResult = Result>; +type FormatResult = Result, FormatError>; #[allow(clippy::enum_variant_names)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] diff --git a/src/lib.rs b/src/lib.rs index d4ad72f..9d0d2ca 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -15,6 +15,7 @@ mod format_text; mod generation; pub use format_text::format_text; +pub use format_text::FormatError; #[cfg(feature = "tracing")] pub use format_text::trace_file; diff --git a/src/wasm_plugin.rs b/src/wasm_plugin.rs index 9ca354d..407582e 100644 --- a/src/wasm_plugin.rs +++ b/src/wasm_plugin.rs @@ -6,6 +6,7 @@ use dprint_core::generate_plugin_code; use dprint_core::plugins::CheckConfigUpdatesMessage; use dprint_core::plugins::ConfigChange; use dprint_core::plugins::FileMatchingInfo; +use dprint_core::plugins::FormatError as CoreFormatError; use dprint_core::plugins::FormatRange; use dprint_core::plugins::FormatResult; use dprint_core::plugins::PluginInfo; @@ -46,7 +47,7 @@ impl SyncPluginHandler for MarkdownPluginHandler { } } - fn check_config_updates(&self, _message: CheckConfigUpdatesMessage) -> Result, anyhow::Error> { + fn check_config_updates(&self, _message: CheckConfigUpdatesMessage) -> Result, CoreFormatError> { Ok(Vec::new()) } @@ -91,13 +92,14 @@ impl SyncPluginHandler for MarkdownPluginHandler { match result { Ok(Some(bytes)) => Ok(Some(String::from_utf8(bytes)?)), Ok(None) => Ok(None), - Err(err) => Err(err), + Err(err) => Err(super::FormatError::CodeBlock(err.into())), } } else { Ok(None) } }) - .map(|maybe_text| maybe_text.map(|t| t.into_bytes())); + .map(|maybe_text| maybe_text.map(|t| t.into_bytes())) + .map_err(CoreFormatError::new); fn tag_to_extension<'a>(tag: &str, config: &'a Configuration) -> Option<&'a str> { let tag_lower = tag.trim().to_lowercase(); diff --git a/tests/spec_test.rs b/tests/spec_test.rs index 0fdb357..5ceb38c 100644 --- a/tests/spec_test.rs +++ b/tests/spec_test.rs @@ -37,6 +37,7 @@ fn main() { Ok(None) } }) + .map_err(Into::into) }) }, Arc::new(move |_, _file_text, _spec_config| {