grapl_graph_descriptions_py/
ip_port_node.rs

1    use grapl_graph_descriptions::graph_description::IpPort as InnerIpPort;
2use grapl_graph_descriptions::graph_description::IpPortBuilder;
3use pyo3::create_exception;
4use pyo3::prelude::*;
5
6create_exception!(ip_port_node, IpPortBuilderError, pyo3::exceptions::ValueError);
7
8#[pyclass]
9#[derive(Clone)]
10pub struct IpPortNode {
11    pub(crate) inner_node: InnerIpPort,
12}
13
14impl<'source> pyo3::FromPyObject<'source> for IpPortNode {
15    fn extract(ob: &'source pyo3::types::PyAny) -> pyo3::PyResult<Self> {
16        Ok(
17            pyo3::PyTryFrom::try_from(ob).map(|x: &Self| x.clone())?
18        )
19    }
20}
21
22#[pyclass]
23#[derive(Clone, Default)]
24pub struct IpPortNodeBuilder {
25    builder: IpPortBuilder,
26}
27
28#[pymethods]
29impl IpPortNodeBuilder {
30    #[new]
31    fn new(
32        obj: &PyRawObject,
33    ) {
34        obj.init(
35            Self::default()
36        )
37    }
38
39    pub fn with_ip_address(&mut self, ip_address: String) -> Self {
40        self.builder.ip_address(ip_address);
41        self.clone()
42    }
43
44    pub fn with_port(&mut self, port: u16) -> Self {
45        self.builder.port(port);
46        self.clone()
47    }
48
49    pub fn with_protocol(&mut self, protocol: String) -> Self {
50        self.builder.protocol(protocol);
51        self.clone()
52    }
53
54    pub fn build(&self) -> PyResult<IpPortNode> {
55
56        let built_node = match self.builder.build() {
57            Ok(built_node) => built_node,
58            Err(e) => {
59                return Err(
60                    PyErr::new::<IpPortBuilderError, _>(format!("{}", e))
61                )
62            }
63        };
64
65        Ok(
66            IpPortNode {
67                inner_node: built_node
68            }
69        )
70    }
71}