ntap_db_udp_service/
lib.rs

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