bms_rs/bms/
error.rs

1//! This module defines enums of errors and warnings on parse process.
2
3use std::ops::RangeInclusive;
4
5use num::BigUint;
6use thiserror::Error;
7
8use super::{command::ObjId, prelude::*};
9
10/// An error occurred when parsing the [`TokenStream`].
11#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub enum ParseError {
14    /// Unexpected control flow.
15    #[error("unexpected control flow {0}")]
16    UnexpectedControlFlow(&'static str),
17    /// [`Rng`] generated a value outside the required [`RangeInclusive`] for a random block.
18    #[error("random generated value out of range: expected {expected:?}, got {actual}")]
19    RandomGeneratedValueOutOfRange {
20        /// The expected range of the random block.
21        expected: RangeInclusive<BigUint>,
22        /// The actual value generated by the [`Rng`].
23        actual: BigUint,
24    },
25    /// [`Rng`] generated a value outside the required [`RangeInclusive`] for a switch block.
26    #[error("switch generated value out of range: expected {expected:?}, got {actual}")]
27    SwitchGeneratedValueOutOfRange {
28        /// The expected range of the switch block.
29        expected: RangeInclusive<BigUint>,
30        /// The actual value generated by the [`Rng`].
31        actual: BigUint,
32    },
33}
34
35/// A parse error with position information.
36pub type ParseErrorWithRange = SourceRangeMixin<ParseError>;
37
38/// A warning occurred when parsing the [`TokenStream`].
39#[derive(Debug, Clone, PartialEq, Eq, Hash, Error)]
40#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
41pub enum ParseWarning {
42    /// Syntax formed from the commands was invalid.
43    #[error("syntax error: {0}")]
44    SyntaxError(String),
45    /// The object has required but not defined,
46    #[error("undefined object: {0:?}")]
47    UndefinedObject(ObjId),
48    /// Has duplicated definition, that `prompt_handler` returned [`DuplicationWorkaround::Warn`].
49    #[error("duplicating definition: {0}")]
50    DuplicatingDef(ObjId),
51    /// Has duplicated track object, that `prompt_handler` returned [`DuplicationWorkaround::Warn`].
52    #[error("duplicating track object: {0} {1}")]
53    DuplicatingTrackObj(Track, Channel),
54    /// Has duplicated channel object, that `prompt_handler` returned [`DuplicationWorkaround::Warn`].
55    #[error("duplicating channel object: {0} {1}")]
56    DuplicatingChannelObj(ObjTime, Channel),
57    /// Failed to convert a byte into a base-62 character `0-9A-Za-z`.
58    #[error("expected id format is base 62 (`0-9A-Za-z`)")]
59    OutOfBase62,
60}
61
62/// Type alias of `core::result::Result<T, ParseWarning>`
63pub(crate) type Result<T> = core::result::Result<T, ParseWarning>;
64
65/// A parse warning with position information.
66pub type ParseWarningWithRange = SourceRangeMixin<ParseWarning>;