mech_core/
error.rs

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