ntap_db_tcp_service/
lib.rs

1use std::{collections::HashMap, fs, path::PathBuf};
2
3use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "bundle")]
6pub const TCP_SERVICE_BIN: &[u8] = include_bytes!("../resources/tcp-service.bin");
7
8pub const TCP_SERVICE_BIN_NAME: &str = "tcp-service.bin";
9pub const TCP_SERVICE_R2_URL: &str = "https://r2.ntap.io/tcp-service.bin";
10
11#[derive(Serialize, Deserialize, Debug, Clone)]
12pub struct TcpService {
13    pub port: u16,
14    pub service_name: String,
15}
16
17impl TcpService {
18    pub fn file_name() -> String {
19        TCP_SERVICE_BIN_NAME.to_owned()
20    }
21    pub fn r2_url() -> String {
22        TCP_SERVICE_R2_URL.to_owned()
23    }
24}
25
26#[cfg(feature = "bundle")]
27pub fn get_map() -> HashMap<u16, String> {
28    let mut tcp_map: HashMap<u16, String> = HashMap::new();
29    let tcp_services: Vec<TcpService> = bincode::deserialize(TCP_SERVICE_BIN).unwrap();
30    for tcp_service in tcp_services {
31        tcp_map.insert(tcp_service.port, tcp_service.service_name);
32    }
33    tcp_map
34}
35
36pub fn get_map_from_file(file_path: PathBuf) -> HashMap<u16, String> {
37    let mut tcp_map: HashMap<u16, String> = HashMap::new();
38    match fs::read(file_path) {
39        Ok(f) => {
40            let tcp_services: Vec<TcpService> = bincode::deserialize(&f).unwrap();
41            for tcp_service in tcp_services {
42                tcp_map.insert(tcp_service.port, tcp_service.service_name);
43            }
44        }
45        Err(e) => {
46            eprintln!("Error reading file: {}", e);
47        }
48    }
49    tcp_map
50}