behavior_tree_lite/
port.rs

1use crate::{
2    parser::{BlackboardValue, PortMap, PortMapOwned},
3    Symbol,
4};
5
6#[derive(Debug, PartialEq, Eq, Clone, Copy)]
7pub enum PortType {
8    Input,
9    Output,
10    InOut,
11}
12
13#[derive(Debug, PartialEq, Eq, Clone, Copy)]
14pub struct PortSpec {
15    pub ty: PortType,
16    pub key: Symbol,
17}
18
19impl PortSpec {
20    pub fn new_in(key: impl Into<Symbol>) -> Self {
21        Self {
22            ty: PortType::Input,
23            key: key.into(),
24        }
25    }
26
27    pub fn new_out(key: impl Into<Symbol>) -> Self {
28        Self {
29            ty: PortType::Output,
30            key: key.into(),
31        }
32    }
33
34    pub fn new_inout(key: impl Into<Symbol>) -> Self {
35        Self {
36            ty: PortType::InOut,
37            key: key.into(),
38        }
39    }
40}
41
42#[derive(Debug, PartialEq, Eq, Clone)]
43pub enum BlackboardValueOwned {
44    /// Literal value could have been decoded, so it is an owned string.
45    Literal(String),
46    Ref(String),
47}
48
49impl crate::BlackboardValue {
50    pub fn to_owned2(&self) -> BlackboardValueOwned {
51        match self {
52            Self::Literal(s) => BlackboardValueOwned::Literal(s.clone()),
53            Self::Ref(s, _) => BlackboardValueOwned::Ref(s.to_string()),
54        }
55    }
56}
57
58pub trait AbstractPortMap<'src> {
59    fn get_type(&self) -> PortType;
60    fn node_port(&self) -> &str;
61    fn blackboard_value(&self) -> BlackboardValueOwned;
62}
63
64impl<'src> AbstractPortMap<'src> for PortMap<'src> {
65    fn get_type(&self) -> PortType {
66        self.ty
67    }
68
69    fn node_port(&self) -> &'src str {
70        self.node_port
71    }
72
73    fn blackboard_value(&self) -> BlackboardValueOwned {
74        match &self.blackboard_value {
75            BlackboardValue::Literal(s) => BlackboardValueOwned::Literal(s.clone()),
76            BlackboardValue::Ref(s) => BlackboardValueOwned::Ref(s.to_string()),
77        }
78    }
79}
80
81impl<'src> AbstractPortMap<'src> for PortMapOwned {
82    fn get_type(&self) -> PortType {
83        self.ty
84    }
85
86    fn node_port(&self) -> &str {
87        &self.node_port
88    }
89
90    fn blackboard_value(&self) -> BlackboardValueOwned {
91        self.blackboard_value.clone()
92    }
93}