ntap_db_oui/
lib.rs

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