ntap_db_as/
lib.rs

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