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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#[derive(thiserror::Error, Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
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 '{id}' ('{component}::{operation}')")]
  InvalidPort {
    port: String,
    id: String,
    component: String,
    operation: String,
  },

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

  #[error(
    "Signature for operation '{id}' ('{component}::{operation}') describes port '{port}' but '{port}' not found in graph"
  )]
  MissingPort {
    port: String,
    id: String,
    component: String,
    operation: String,
  },

  #[error(
    "Input '{port}' on operation '{id}' ('{component}::{operation}') is connected but does not exist on operation."
  )]
  UnknownInput {
    port: String,
    id: String,
    component: String,
    operation: String,
  },

  #[error(
    "Output '{port}' on operation '{id}' ('{component}::{operation}') is connected but does not exist on operation."
  )]
  UnknownOutput {
    port: String,
    id: String,
    component: String,
    operation: String,
  },

  #[error("Unused output port '{port}' on operation '{id}' ('{component}::{operation}')")]
  UnusedOutput {
    port: String,
    id: 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,
      "Flow '{}' could not be validated: {}",
      self.schematic,
      self.errors.iter().map(|e| e.to_string()).collect::<Vec<_>>().join(", ")
    )
  }
}