netgauze_bmp_service/
lib.rs

1// Copyright (C) 2022-present The NetGauze Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
12// implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use serde::{Deserialize, Serialize};
17
18use netgauze_bmp_pkt::codec::BmpCodecDecoderError;
19use std::{
20    fmt::{Display, Formatter},
21    net::SocketAddr,
22};
23
24pub mod handle;
25pub mod server;
26pub mod transport;
27
28/// Capture the address of both sides of a socket
29#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug, Serialize, Deserialize)]
30pub struct AddrInfo {
31    local_socket: SocketAddr,
32    remote_socket: SocketAddr,
33}
34
35impl AddrInfo {
36    pub const fn new(local_socket: SocketAddr, remote_socket: SocketAddr) -> Self {
37        Self {
38            local_socket,
39            remote_socket,
40        }
41    }
42
43    pub const fn local_socket(&self) -> SocketAddr {
44        self.local_socket
45    }
46
47    pub const fn remote_socket(&self) -> SocketAddr {
48        self.remote_socket
49    }
50}
51
52/// Associate a value with a tag
53#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
54pub struct TaggedData<T, V> {
55    tag: T,
56    value: V,
57}
58
59impl<T: Copy, V> TaggedData<T, V> {
60    pub const fn new(tag: T, value: V) -> Self {
61        Self { tag, value }
62    }
63
64    pub const fn tag(&self) -> T {
65        self.tag
66    }
67
68    pub const fn value(&self) -> &V {
69        &self.value
70    }
71}
72
73impl Display for TaggedData<AddrInfo, BmpCodecDecoderError> {
74    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
75        write!(f, "{self:?}")
76    }
77}
78
79impl std::error::Error for TaggedData<AddrInfo, BmpCodecDecoderError> {}