1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
  #[error("Unused sender: {0}")]
  UnusedSender(String),

  #[error("Network contains circular references: {:?}", .0)]
  NetworkUnresolvable(Vec<String>),

  #[error("Missing component with id '{0}'")]
  MissingComponent(String),

  #[error("Could not find component referenced by id '{0}'")]
  ComponentIdNotFound(String),

  #[error("Missing operation '{name}' on component '{component}'")]
  MissingOperation { component: String, name: String },

  #[error("Invalid port '{port}' on operation '{component}::{operation}'")]
  InvalidPort {
    port: String,
    component: String,
    operation: String,
  },

  #[error("Input port '{port}' on operation '{component}::{operation}' not connected to anything")]
  MissingConnection {
    port: String,
    component: String,
    operation: String,
  },

  #[error("Unused output port '{port}' on component '{component}::{operation}'")]
  UnusedOutput {
    port: String,
    component: String,
    operation: String,
  },
}

#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
#[must_use]
pub struct OperationInvalid {
  errors: Vec<ValidationError>,
  schematic: String,
}

impl OperationInvalid {
  pub fn new(schematic: String, errors: Vec<ValidationError>) -> Self {
    Self { schematic, errors }
  }
}

impl std::fmt::Display for OperationInvalid {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "Schematic '{}' could not be validated: {}",
      self.schematic,
      self.errors.iter().map(|e| e.to_string()).collect::<Vec<_>>().join(", ")
    )
  }
}