gliner/util/error.rs
1use std::error;
2use std::fmt::Display;
3
4
5#[derive(Debug, Clone)]
6/// Defines an error caused by the use of an incorrect index in one of the
7/// structures exchanged during a pipeline. This is an internal error that
8/// only occurs if there is a bug in the algorithms ensuring the consistency
9/// of this data, and it cannot be recovered from except by abandoning the
10/// ongoing process and report an error. However, it is preferred over panicking
11/// to ensure safe usage of the library (please document an issue in this case,
12/// providing the message available in this struct).
13pub struct IndexError {
14 message: String,
15}
16
17impl IndexError {
18 pub fn new(array_desc: &str, index: usize) -> Self {
19 Self {
20 message: format!("error accessing index {index} in {array_desc}"),
21 }
22 }
23
24 pub fn with(message: &str) -> Self {
25 Self {
26 message: message.to_string(),
27 }
28 }
29}
30
31impl error::Error for IndexError { }
32
33impl Display for IndexError {
34 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35 f.write_str(&self.message)
36 }
37}