mech_core/
error.rs

1// # Errors
2
3// Defines a struct for errors and an enum which enumerates the error types
4
5// ## Prelude
6use crate::ValueKind;
7use crate::nodes::{SourceRange, Token};
8use std::fmt;
9
10type Rows = usize;
11type Cols = usize;
12pub type ParserErrorReport = Vec<ParserErrorContext>;
13
14#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
15pub struct MechError{ 
16  pub id: u32,
17  pub file: String,
18  pub tokens: Vec<Token>,
19  pub kind: MechErrorKind,
20  pub msg: String,
21}
22
23#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
24pub struct ParserErrorContext {
25  pub cause_rng: SourceRange,
26  pub err_message: String,
27  pub annotation_rngs: Vec<SourceRange>,
28}
29
30#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
31pub enum MechErrorKind {
32  UndefinedField(u64),                               // Accessed a field of a record that's not defined
33  UndefinedVariable(u64),                            // Accessed a variable that's not defined
34  UndefinedKind(u64),                                // Used a kind that's not defined
35  MissingTable(u64),                               // TableId of missing table
36  WrongTableColumnKind,
37  MissingBlock(u64),                             // BlockId of missing block
38  PendingExpression,                              // id of pending variable
39  PendingTable(u64),                             // TableId of pending table                          
40  DimensionMismatch(Vec<(Rows,Cols)>),               // Argument dimensions are mismatched ((row,col),(row,col))
41  KindMismatch(ValueKind,ValueKind),
42  UnhandledIndexKind,
43  //MissingColumn((TableId,TableIndex)),             // The identified table is missing a needed column
44  //ColumnKindMismatch(Vec<ValueKind>),              // Excepted kind versus given kind
45  //SubscriptOutOfBounds(((Rows,Cols),(Rows,Cols))), // (target) vs (actual) index
46  LinearSubscriptOutOfBounds((Rows,Rows)),           // (target) vs (actual) index
47  //DomainMismatch(u64, u64),                        // domain IDs (target vs actual)
48  MissingFunction(u64),                              // ID of missing function
49  //TransformationPending(Transformation),           // Block is unsatisfied so the transformation is not added
50  //IncorrectFunctionArgumentType,
51  ZeroIndex,                                         // Zero cannot ever be used as an index.
52  VariableRedefined(u64),
53  NotMutable(u64), 
54  BlockDisabled,
55  IoError,
56  GenericError(String),
57  FileNotFound(String),
58  Unhandled,
59  OutputUndefinedInFunctionBody(u64),
60  UnknownFunctionArgument(u64),
61  UnknownColumnKind(u64),
62  UnknownEnumVairant(u64,u64),
63  UnableToConvertValueKind,
64  UnhandledFunctionArgumentKind,
65  CouldNotAssignKindToValue,
66  ExpectedNumericForSize,                            // When something non-numeric is passed as a size
67  MatrixMustHaveHomogenousKind,                      // When multiple element kinds are specified for a matrix
68  IncorrectNumberOfArguments,
69  //UnhandledTableShape(TableShape),
70  TooManyInputArguments(usize,usize),                // (given,expected)
71  ParserError(ParserErrorReport, String),
72  //MissingCapability(Capability),
73  InvalidCapabilityToken,
74  UnknownReplCommand(String),
75  NoCode,
76  None,
77}
78
79impl From<std::io::Error> for MechError{ 
80  fn from(n: std::io::Error) -> MechError{ 
81    MechError{file: file!().to_string(), tokens: vec![], msg: "".to_string(), id: 74892, kind: MechErrorKind::IoError}
82  } 
83}
84
85/*
86impl fmt::Debug for MechErrorKind {
87  #[inline]
88  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
89    match self {
90      _ => write!(f,"No Format")?;
91    }
92    Ok(())
93  }
94}*/