Skip to content

Commit

Permalink
Change map-capacity-counting part of json_internal! macro to not repo…
Browse files Browse the repository at this point in the history
…rt errors.

Since these errors are already reported by the part of the macro that actually parses the map,
it is unnecessary to report them twice, so the capacity-counting part can just return 0 if
it encounters an error.
  • Loading branch information
zachs18 committed Oct 26, 2022
1 parent 99629eb commit e976482
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/macros.rs
Expand Up @@ -254,7 +254,9 @@ macro_rules! json_internal {
1 + json_internal!(@object_capacity () ($($rest)*) ($($rest)*))
};

// Current entry followed by unexpected token. The actual parsing macro will print the error message.
// Current entry followed by unexpected token. The part that parses the values
// will trigger a reasonable error message; here, we just return 0
// so that there is not a duplicated error message.
(@object_capacity entry $unexpected:tt $($rest:tt)*) => {
0
};
Expand Down Expand Up @@ -299,22 +301,30 @@ macro_rules! json_internal {
json_internal!(@object_capacity entry)
};

// Missing value for last entry. The actual parsing macro will print the error message.
// Missing value for last entry. The part that parses the values
// will trigger a reasonable error message; here, we just return 0
// so that there is not a duplicated error message.
(@object_capacity ($($key:tt)+) (:) $copy:tt) => {
0
};

// Missing colon and value for last entry. The actual parsing macro will print the error message.
// Missing colon and value for last entry. The part that parses the values
// will trigger a reasonable error message; here, we just return 0
// so that there is not a duplicated error message.
(@object_capacity ($($key:tt)+) () $copy:tt) => {
0
};

// Misplaced colon. The actual parsing macro will print the error message.
// Misplaced colon. The part that parses the values
// will trigger a reasonable error message; here, we just return 0
// so that there is not a duplicated error message.
(@object_capacity () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
0
};

// Found a comma inside a key. The actual parsing macro will print the error message.
// Found a comma inside a key. The part that parses the values
// will trigger a reasonable error message; here, we just return 0
// so that there is not a duplicated error message.
(@object_capacity ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
0
};
Expand All @@ -326,8 +336,10 @@ macro_rules! json_internal {
// };

// Refuse to absorb colon token into key expression.
// The part that parses the values will trigger a reasonable error message;
// here, we just return 0 so that there is not a duplicated error message.
(@object_capacity ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
json_expect_expr_comma!($($unexpected)+)
0
};

// Munch a token into the current key.
Expand Down

0 comments on commit e976482

Please sign in to comment.