metrale_grammar/structural_tag/error.rs
1// SPDX-License-Identifier: MIT OR Apache-2.0
2//
3// Error type for the structural-tag subsystem — port of the
4// `StructuralTagError` family (`InvalidJSONError`,
5// `InvalidStructuralTagError`) from `cpp/exception.h`.
6//
7// The C++ uses a `Result<T, StructuralTagError>` where the error is a
8// `std::variant` of the two error classes. Here a single `enum` carries
9// the same distinction plus a human-readable message, returned as a
10// plain `Result` — no panics, no `unsafe`.
11
12/// Error produced while parsing or converting a structural tag.
13///
14/// Port of `StructuralTagError`. The two variants mirror the C++
15/// `InvalidJSONError` (the structural-tag JSON document is not valid
16/// JSON) and `InvalidStructuralTagError` (the JSON is well-formed but
17/// is not a valid structural tag).
18#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
19pub enum StructuralTagError {
20 /// The structural-tag document failed JSON parsing.
21 #[error("invalid structural tag JSON: {0}")]
22 InvalidJson(String),
23 /// The document parsed as JSON but is not a valid structural tag,
24 /// or grammar construction failed.
25 #[error("invalid structural tag: {0}")]
26 InvalidStructuralTag(String),
27}
28
29impl StructuralTagError {
30 /// Construct an [`StructuralTagError::InvalidJson`].
31 pub(crate) fn json(msg: impl Into<String>) -> Self {
32 StructuralTagError::InvalidJson(msg.into())
33 }
34
35 /// Construct an [`StructuralTagError::InvalidStructuralTag`].
36 pub(crate) fn invalid(msg: impl Into<String>) -> Self {
37 StructuralTagError::InvalidStructuralTag(msg.into())
38 }
39}
40
41/// Convenience alias for results in this module.
42pub type StructuralTagResult<T> = Result<T, StructuralTagError>;