Skip to content

Commit

Permalink
refactor(parser): Pull out error constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 21, 2022
1 parent 5e50115 commit eeb2794
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 27 deletions.
18 changes: 18 additions & 0 deletions crates/toml_edit/src/parser/errors.rs
Expand Up @@ -266,6 +266,24 @@ pub(crate) enum CustomError {
OutOfRange,
}

impl CustomError {
pub(crate) fn duplicate_key(path: &[Key], i: usize) -> Self {
assert!(i < path.len());
Self::DuplicateKey {
key: path[i].to_repr().as_ref().as_raw().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"
Expand Down
3 changes: 1 addition & 2 deletions crates/toml_edit/src/parser/inline_table.rs
@@ -1,7 +1,6 @@
use crate::key::Key;
use crate::parser::errors::CustomError;
use crate::parser::key::key;
use crate::parser::table::extend_wrong_type;
use crate::parser::trivia::ws;
use crate::parser::value::value;
use crate::table::TableKeyValue;
Expand Down Expand Up @@ -62,7 +61,7 @@ fn descend_path<'a>(
table = sweet_child_of_mine;
}
ref v => {
return Err(extend_wrong_type(path, i, v.type_name()));
return Err(CustomError::extend_wrong_type(path, i, v.type_name()));
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions crates/toml_edit/src/parser/state.rs
@@ -1,7 +1,5 @@
use super::table::duplicate_key;
use crate::key::Key;
use crate::parser::errors::CustomError;
use crate::parser::table::extend_wrong_type;
use crate::repr::Decor;
use crate::table::TableKeyValue;
use crate::{ArrayOfTables, Document, InternalString, Item, Table};
Expand Down Expand Up @@ -88,7 +86,7 @@ impl ParseState {
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
entry
.as_array_of_tables()
.ok_or_else(|| duplicate_key(&path, path.len() - 1))?;
.ok_or_else(|| CustomError::duplicate_key(&path, path.len() - 1))?;

self.current_table_position += 1;
self.current_table.decor = decor;
Expand All @@ -114,7 +112,7 @@ impl ParseState {
Item::Table(t) if t.implicit => {
self.current_table = t;
}
_ => return Err(duplicate_key(&path, path.len() - 1)),
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}

Expand Down Expand Up @@ -144,7 +142,7 @@ impl ParseState {
.or_insert(Item::ArrayOfTables(ArrayOfTables::new()));
let array = entry
.as_array_of_tables_mut()
.ok_or_else(|| duplicate_key(&path, path.len() - 1))?;
.ok_or_else(|| CustomError::duplicate_key(&path, path.len() - 1))?;
array.push(table);
} else {
let parent_table = Self::descend_path(root, &path[..path.len() - 1], false)?;
Expand All @@ -158,7 +156,7 @@ impl ParseState {
Item::Table(ref mut t) if t.implicit => {
std::mem::swap(t, &mut table);
}
_ => return Err(duplicate_key(&path, path.len() - 1)),
_ => return Err(CustomError::duplicate_key(&path, path.len() - 1)),
}
}
crate::Entry::Vacant(entry) => {
Expand Down Expand Up @@ -186,7 +184,7 @@ impl ParseState {
});
match *entry {
Item::Value(ref v) => {
return Err(extend_wrong_type(path, i, v.type_name()));
return Err(CustomError::extend_wrong_type(path, i, v.type_name()));
}
Item::ArrayOfTables(ref mut array) => {
debug_assert!(!array.is_empty());
Expand Down
18 changes: 0 additions & 18 deletions crates/toml_edit/src/parser/table.rs
@@ -1,5 +1,3 @@
use crate::key::Key;
use crate::parser::errors::CustomError;
use crate::parser::key::key;
use crate::parser::trivia::line_trailing;
use crate::parser::ParseState;
Expand Down Expand Up @@ -61,19 +59,3 @@ parser! {
.message("While parsing a Table Header")
}
}

pub(crate) fn duplicate_key(path: &[Key], i: usize) -> CustomError {
assert!(i < path.len());
CustomError::DuplicateKey {
key: path[i].to_repr().as_ref().as_raw().into(),
table: Some(path[..i].to_vec()),
}
}

pub(crate) fn extend_wrong_type(path: &[Key], i: usize, actual: &'static str) -> CustomError {
assert!(i < path.len());
CustomError::DottedKeyExtendWrongType {
key: path[..=i].to_vec(),
actual,
}
}

0 comments on commit eeb2794

Please sign in to comment.