From 52b5c6f82d6af75b09331990d62fbbadeefa6d57 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 6 Nov 2023 14:12:43 -0600 Subject: [PATCH] refactor(edit): Pull error out of parser --- .../src/{parser/errors.rs => error.rs} | 71 ------------------ crates/toml_edit/src/lib.rs | 3 +- crates/toml_edit/src/parser/datetime.rs | 2 +- crates/toml_edit/src/parser/error.rs | 74 +++++++++++++++++++ crates/toml_edit/src/parser/inline_table.rs | 2 +- crates/toml_edit/src/parser/key.rs | 2 +- crates/toml_edit/src/parser/mod.rs | 12 +-- crates/toml_edit/src/parser/state.rs | 2 +- crates/toml_edit/src/parser/strings.rs | 2 +- 9 files changed, 87 insertions(+), 83 deletions(-) rename crates/toml_edit/src/{parser/errors.rs => error.rs} (74%) create mode 100644 crates/toml_edit/src/parser/error.rs diff --git a/crates/toml_edit/src/parser/errors.rs b/crates/toml_edit/src/error.rs similarity index 74% rename from crates/toml_edit/src/parser/errors.rs rename to crates/toml_edit/src/error.rs index 685e9f71..1e03d9ab 100644 --- a/crates/toml_edit/src/parser/errors.rs +++ b/crates/toml_edit/src/error.rs @@ -2,7 +2,6 @@ use std::error::Error as StdError; use std::fmt::{Display, Formatter, Result}; use crate::parser::prelude::*; -use crate::Key; use winnow::error::ContextError; use winnow::error::ParseError; @@ -243,73 +242,3 @@ mod test_translate_position { assert_eq!(position, (1, 2)); } } - -#[derive(Debug, Clone)] -pub(crate) enum CustomError { - DuplicateKey { - key: String, - table: Option>, - }, - DottedKeyExtendWrongType { - key: Vec, - actual: &'static str, - }, - OutOfRange, - #[cfg_attr(feature = "unbounded", allow(dead_code))] - RecursionLimitExceeded, -} - -impl CustomError { - pub(crate) fn duplicate_key(path: &[Key], i: usize) -> Self { - assert!(i < path.len()); - let key = &path[i]; - let repr = key.display_repr(); - Self::DuplicateKey { - key: repr.into(), - table: Some(path[..i].to_vec()), - } - } - - pub(crate) fn extend_wrong_type(path: &[Key], i: usize, actual: &'static str) -> Self { - assert!(i < path.len()); - Self::DottedKeyExtendWrongType { - key: path[..=i].to_vec(), - actual, - } - } -} - -impl StdError for CustomError { - fn description(&self) -> &'static str { - "TOML parse error" - } -} - -impl Display for CustomError { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { - match self { - CustomError::DuplicateKey { key, table } => { - if let Some(table) = table { - if table.is_empty() { - write!(f, "duplicate key `{}` in document root", key) - } else { - let path = table.iter().map(|k| k.get()).collect::>().join("."); - write!(f, "duplicate key `{}` in table `{}`", key, path) - } - } else { - write!(f, "duplicate key `{}`", key) - } - } - CustomError::DottedKeyExtendWrongType { key, actual } => { - let path = key.iter().map(|k| k.get()).collect::>().join("."); - write!( - f, - "dotted key `{}` attempted to extend non-table type ({})", - path, actual - ) - } - CustomError::OutOfRange => write!(f, "value is out of range"), - CustomError::RecursionLimitExceeded => write!(f, "recursion limit exceeded"), - } - } -} diff --git a/crates/toml_edit/src/lib.rs b/crates/toml_edit/src/lib.rs index 854fb645..9b973682 100644 --- a/crates/toml_edit/src/lib.rs +++ b/crates/toml_edit/src/lib.rs @@ -66,6 +66,7 @@ mod array; mod array_of_tables; mod document; mod encode; +mod error; mod index; mod inline_table; mod internal_string; @@ -90,6 +91,7 @@ pub use crate::array_of_tables::{ ArrayOfTables, ArrayOfTablesIntoIter, ArrayOfTablesIter, ArrayOfTablesIterMut, }; pub use crate::document::Document; +pub use crate::error::TomlError; pub use crate::inline_table::{ InlineEntry, InlineOccupiedEntry, InlineTable, InlineTableIntoIter, InlineTableIter, InlineTableIterMut, InlineVacantEntry, @@ -97,7 +99,6 @@ pub use crate::inline_table::{ pub use crate::internal_string::InternalString; pub use crate::item::{array, table, value, Item}; pub use crate::key::{Key, KeyMut}; -pub use crate::parser::TomlError; pub use crate::raw_string::RawString; pub use crate::repr::{Decor, Formatted, Repr}; pub use crate::table::{ diff --git a/crates/toml_edit/src/parser/datetime.rs b/crates/toml_edit/src/parser/datetime.rs index 96a3854d..636620b0 100644 --- a/crates/toml_edit/src/parser/datetime.rs +++ b/crates/toml_edit/src/parser/datetime.rs @@ -1,6 +1,6 @@ use std::ops::RangeInclusive; -use crate::parser::errors::CustomError; +use crate::parser::error::CustomError; use crate::parser::prelude::*; use crate::parser::trivia::from_utf8_unchecked; diff --git a/crates/toml_edit/src/parser/error.rs b/crates/toml_edit/src/parser/error.rs new file mode 100644 index 00000000..fd4a2114 --- /dev/null +++ b/crates/toml_edit/src/parser/error.rs @@ -0,0 +1,74 @@ +use std::error::Error as StdError; +use std::fmt::{Display, Formatter, Result}; + +use crate::Key; + +#[derive(Debug, Clone)] +pub(crate) enum CustomError { + DuplicateKey { + key: String, + table: Option>, + }, + DottedKeyExtendWrongType { + key: Vec, + actual: &'static str, + }, + OutOfRange, + #[cfg_attr(feature = "unbounded", allow(dead_code))] + RecursionLimitExceeded, +} + +impl CustomError { + pub(crate) fn duplicate_key(path: &[Key], i: usize) -> Self { + assert!(i < path.len()); + let key = &path[i]; + let repr = key.display_repr(); + Self::DuplicateKey { + key: repr.into(), + table: Some(path[..i].to_vec()), + } + } + + pub(crate) fn extend_wrong_type(path: &[Key], i: usize, actual: &'static str) -> Self { + assert!(i < path.len()); + Self::DottedKeyExtendWrongType { + key: path[..=i].to_vec(), + actual, + } + } +} + +impl StdError for CustomError { + fn description(&self) -> &'static str { + "TOML parse error" + } +} + +impl Display for CustomError { + fn fmt(&self, f: &mut Formatter<'_>) -> Result { + match self { + CustomError::DuplicateKey { key, table } => { + if let Some(table) = table { + if table.is_empty() { + write!(f, "duplicate key `{}` in document root", key) + } else { + let path = table.iter().map(|k| k.get()).collect::>().join("."); + write!(f, "duplicate key `{}` in table `{}`", key, path) + } + } else { + write!(f, "duplicate key `{}`", key) + } + } + CustomError::DottedKeyExtendWrongType { key, actual } => { + let path = key.iter().map(|k| k.get()).collect::>().join("."); + write!( + f, + "dotted key `{}` attempted to extend non-table type ({})", + path, actual + ) + } + CustomError::OutOfRange => write!(f, "value is out of range"), + CustomError::RecursionLimitExceeded => write!(f, "recursion limit exceeded"), + } + } +} diff --git a/crates/toml_edit/src/parser/inline_table.rs b/crates/toml_edit/src/parser/inline_table.rs index f7cf2e9c..d66eadd6 100644 --- a/crates/toml_edit/src/parser/inline_table.rs +++ b/crates/toml_edit/src/parser/inline_table.rs @@ -5,7 +5,7 @@ use winnow::token::one_of; use winnow::trace::trace; use crate::key::Key; -use crate::parser::errors::CustomError; +use crate::parser::error::CustomError; use crate::parser::key::key; use crate::parser::prelude::*; use crate::parser::trivia::ws; diff --git a/crates/toml_edit/src/parser/key.rs b/crates/toml_edit/src/parser/key.rs index bd8804a2..9ed76c8f 100644 --- a/crates/toml_edit/src/parser/key.rs +++ b/crates/toml_edit/src/parser/key.rs @@ -7,7 +7,7 @@ use winnow::token::take_while; use winnow::trace::trace; use crate::key::Key; -use crate::parser::errors::CustomError; +use crate::parser::error::CustomError; use crate::parser::prelude::*; use crate::parser::strings::{basic_string, literal_string}; use crate::parser::trivia::{from_utf8_unchecked, ws}; diff --git a/crates/toml_edit/src/parser/mod.rs b/crates/toml_edit/src/parser/mod.rs index eb475505..1440cdc0 100644 --- a/crates/toml_edit/src/parser/mod.rs +++ b/crates/toml_edit/src/parser/mod.rs @@ -3,7 +3,7 @@ pub(crate) mod array; pub(crate) mod datetime; pub(crate) mod document; -pub(crate) mod errors; +pub(crate) mod error; pub(crate) mod inline_table; pub(crate) mod key; pub(crate) mod numbers; @@ -13,7 +13,7 @@ pub(crate) mod table; pub(crate) mod trivia; pub(crate) mod value; -pub use errors::TomlError; +pub use crate::error::TomlError; pub(crate) fn parse_document(raw: &str) -> Result { use prelude::*; @@ -95,11 +95,11 @@ pub(crate) mod prelude { #[cfg(not(feature = "unbounded"))] impl RecursionCheck { - pub(crate) fn check_depth(depth: usize) -> Result<(), super::errors::CustomError> { + pub(crate) fn check_depth(depth: usize) -> Result<(), super::error::CustomError> { if depth < 128 { Ok(()) } else { - Err(super::errors::CustomError::RecursionLimitExceeded) + Err(super::error::CustomError::RecursionLimitExceeded) } } @@ -114,7 +114,7 @@ pub(crate) mod prelude { Err(winnow::error::ErrMode::from_external_error( input, winnow::error::ErrorKind::Eof, - super::errors::CustomError::RecursionLimitExceeded, + super::error::CustomError::RecursionLimitExceeded, )) } } @@ -126,7 +126,7 @@ pub(crate) mod prelude { #[cfg(feature = "unbounded")] impl RecursionCheck { - pub(crate) fn check_depth(_depth: usize) -> Result<(), super::errors::CustomError> { + pub(crate) fn check_depth(_depth: usize) -> Result<(), super::error::CustomError> { Ok(()) } diff --git a/crates/toml_edit/src/parser/state.rs b/crates/toml_edit/src/parser/state.rs index 235391c7..8388c884 100644 --- a/crates/toml_edit/src/parser/state.rs +++ b/crates/toml_edit/src/parser/state.rs @@ -1,5 +1,5 @@ use crate::key::Key; -use crate::parser::errors::CustomError; +use crate::parser::error::CustomError; use crate::repr::Decor; use crate::table::TableKeyValue; use crate::{ArrayOfTables, Document, InternalString, Item, RawString, Table}; diff --git a/crates/toml_edit/src/parser/strings.rs b/crates/toml_edit/src/parser/strings.rs index 8c366fad..dacaf582 100644 --- a/crates/toml_edit/src/parser/strings.rs +++ b/crates/toml_edit/src/parser/strings.rs @@ -21,7 +21,7 @@ use winnow::token::tag; use winnow::token::take_while; use winnow::trace::trace; -use crate::parser::errors::CustomError; +use crate::parser::error::CustomError; use crate::parser::numbers::HEXDIG; use crate::parser::prelude::*; use crate::parser::trivia::{from_utf8_unchecked, newline, ws, ws_newlines, NON_ASCII, WSCHAR};