local_fmt/message/
mod.rs

1use crate::panic_builder;
2
3pub mod alloc;
4pub use alloc::*;
5
6pub mod refer;
7pub use refer::*;
8
9/// Represents errors that can occur when working with constant messages.
10///
11/// This enum provides detailed error information for invalid or missing argument numbers
12/// in constant messages.
13#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
14pub enum CreateMessageError {
15    /// Error indicating that an argument number is out of the allowed range.
16    ///
17    /// This error occurs when an argument number is provided that is not within the valid range
18    /// of 0 to N-1, where N is the number of expected arguments.
19    #[error("Invalid argument number: {number} is out of the allowed range (0 <= number < {n}).")]
20    InvalidNumber { number: usize, n: usize },
21
22    /// Error indicating that a required argument number is missing.
23    ///
24    /// This error occurs when an expected argument number is not found within the valid range
25    /// of 0 to N-1, where N is the number of expected arguments.
26    #[error("Missing argument number: {number} is not found within the allowed range (0 <= number < {n}).")]
27    WithoutNumber { number: usize, n: usize },
28    /// Error indicating that a placeholder is empty.
29    ///
30    /// This error occurs when a placeholder is empty.
31    /// This can happen when a placeholder is found without a number.
32    #[error("Empty placeholder found: a placeholder was opened but not closed properly. Ensure all placeholders are correctly formatted.")]
33    EmptyPlaceholder,
34}
35
36impl CreateMessageError {
37    #[track_caller]
38    pub const fn panic(&self) -> ! {
39        match self {
40            Self::InvalidNumber { number, n } => {
41                const MESSAGE: StaticMessage<2> = local_fmt::StaticMessage::new_panic(&[
42                    local_fmt::RefMessageFormat::RefText("Invalid argument number: "),
43                    local_fmt::RefMessageFormat::Placeholder(0usize),
44                    local_fmt::RefMessageFormat::RefText(
45                        " is out of the allowed range (0 <= number < ",
46                    ),
47                    local_fmt::RefMessageFormat::Placeholder(1usize),
48                    local_fmt::RefMessageFormat::RefText(")."),
49                ]);
50                panic_builder!(MESSAGE, [u; *number], [u; *n])
51            }
52            Self::WithoutNumber { number, n } => {
53                const MESSAGE: StaticMessage<2> = local_fmt::StaticMessage::<2usize>::new_panic(&[
54                    local_fmt::RefMessageFormat::RefText("Missing argument number: "),
55                    local_fmt::RefMessageFormat::Placeholder(0usize),
56                    local_fmt::RefMessageFormat::RefText(
57                        " is not found within the allowed range (0 <= number < ",
58                    ),
59                    local_fmt::RefMessageFormat::Placeholder(1usize),
60                    local_fmt::RefMessageFormat::RefText(")."),
61                ]);
62
63                panic_builder!(MESSAGE, [u; *number], [u; *n])
64            }
65            Self::EmptyPlaceholder => {
66                panic!("Empty placeholder found: a placeholder was opened but not closed properly. Ensure all placeholders are correctly formatted.")
67            }
68        }
69    }
70}