trees_lang/compile/
errors.rs

1use std::{error::Error, fmt};
2
3use super::{ArgPlug, CompilingBlock, EdgeFragment};
4
5#[derive(Debug, PartialEq, Eq)]
6/// Errors that can occur during the compilation process.
7pub enum CompileError {
8  /// Error indicating that there are multiple blocks or no blocks without a block-plug.
9  NonUniqueStartBlock(Box<NonUniqueStartBlockError>),
10  /// Error indicating that an argument plug is not connected to any block.
11  DanglingArgEdge(Box<DanglingArgEdgeError>),
12}
13
14#[derive(Debug, PartialEq, Eq)]
15/// Represents an error where there are multiple or no blocks without a block-plug.
16pub struct NonUniqueStartBlockError {
17  /// A list of candidate blocks (i.e., blocks without a block-plug).
18  pub candinates: Vec<CompilingBlock>,
19}
20
21#[derive(Debug, PartialEq, Eq)]
22/// Represents an error where an argument plug is not connected to any block.
23pub struct DanglingArgEdgeError {
24  /// The block associated with the argument plug that is dangling.
25  pub block_of_arg_plug: CompilingBlock,
26  /// The argument plug that is dangling and not connected to any block.
27  pub arg_plug: ArgPlug,
28  /// A list of edge fragments associated with the dangling argument plug.
29  pub edge_fragments: Vec<EdgeFragment>,
30  /// The position (x, y) of the dangling edge's endpoint.
31  ///
32  /// This position is expected to be connected to a block.
33  pub dangling_position: (usize, usize),
34}
35
36impl fmt::Display for CompileError {
37  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38    match self {
39      CompileError::NonUniqueStartBlock(err) => write!(
40        f,
41        "The code must have exact one block which has no block-plug. Found: {}",
42        err.candinates.len() // Accessing through Box
43      ),
44      CompileError::DanglingArgEdge(err) => write!(
45        f,
46        "The arg-plug on ({}, {}) has an arg-plug which is not connected to any block. Expected position: ({}, {})",
47        err.arg_plug.x,
48        err.arg_plug.y,
49        err.dangling_position.0,
50        err.dangling_position.1 // Accessing through Box
51      ),
52    }
53  }
54}
55
56impl Error for CompileError {
57  fn source(&self) -> Option<&(dyn Error + 'static)> {
58    None
59  }
60}