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
use std::{cell::RefCell, rc::Rc};

use support::constants::{BORROWABLE, THIS_IS_A_BUG};

use super::{
    errors::ValidationError,
    types::{Port, ValidationContext},
};

pub fn merge_errors(errors: Vec<anyhow::Error>, new_error: anyhow::Error) -> Vec<anyhow::Error> {
    let mut errors = errors;
    errors.push(new_error);

    errors
}

pub fn merge_errors_vecs(
    errors: Vec<anyhow::Error>,
    new_errors: Vec<anyhow::Error>,
) -> Vec<anyhow::Error> {
    let mut errors = errors;

    for new_error in new_errors.into_iter() {
        errors.push(new_error);
    }

    errors
}

pub fn ensure_node_name_unique(
    node_name: impl Into<String>,
    validation_context: Rc<RefCell<ValidationContext>>,
) -> Result<(), anyhow::Error> {
    let mut context = validation_context
        .try_borrow_mut()
        .expect(&format!("{}, {}", BORROWABLE, THIS_IS_A_BUG));

    let node_name = node_name.into();
    if !context.used_nodes_names.contains(&node_name) {
        context.used_nodes_names.push(node_name);
        return Ok(());
    }

    Err(ValidationError::NodeNameAlreadyUsed(node_name).into())
}

pub fn ensure_value_is_not_empty(value: &str) -> Result<(), anyhow::Error> {
    if value.is_empty() {
        Err(ValidationError::CantBeEmpty().into())
    } else {
        Ok(())
    }
}

pub fn ensure_port_unique(
    port: Port,
    validation_context: Rc<RefCell<ValidationContext>>,
) -> Result<(), anyhow::Error> {
    let mut context = validation_context
        .try_borrow_mut()
        .expect(&format!("{}, {}", BORROWABLE, THIS_IS_A_BUG));

    if !context.used_ports.contains(&port) {
        context.used_ports.push(port);
        return Ok(());
    }

    Err(ValidationError::PortAlreadyUsed(port).into())
}