1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::collections::HashMap;

use crate::err::Error;


#[derive(Debug)]
pub struct BridgedNIC {
  pub adapter: String
}

#[derive(Debug)]
pub struct IntNetNIC {
  pub name: String
}

#[derive(Debug)]
pub enum NICType {
  Bridged(BridgedNIC),
  IntNet(IntNetNIC)
}

#[derive(Debug)]
pub struct NICInfo {
  pub idx: u8,
  pub nictype: NICType,
  pub mac: eui48::MacAddress
}


pub fn get_from_map(
  map: &HashMap<String, String>
) -> Result<Vec<NICInfo>, Error> {
  let mut nics: Vec<NICInfo> = Vec::new();

  for idx in 1..=8 {
    let key = format!("nic{}", idx);
    if let Some(v) = map.get(&key) {
      if v == "none" {
        continue;
      }
      let nictype = match v.as_ref() {
        "none" => continue,
        "bridged" => {
          let key = format!("bridgeadapter{}", idx);
          let adapter = match map.get(&key) {
            Some(adapter) => adapter,
            None => {
              // missing critical information..
              continue;
            }
          };
          NICType::Bridged(BridgedNIC {
            adapter: adapter.to_string()
          })
        }
        "intnet" => {
          let key = format!("intnet{}", idx);
          let name = match map.get(&key) {
            Some(name) => name,
            None => {
              // missing critical information..
              continue;
            }
          };
          NICType::IntNet(IntNetNIC {
            name: name.to_string()
          })
        }
        _ => {
          println!("unrecognized nic type: {}", v);
          continue;
        }
      };

      let key = format!("macaddress{}", idx);
      let mac = match map.get(&key) {
        Some(addr) => eui48::MacAddress::parse_str(addr)?,
        None => continue
      };

      nics.push(NICInfo { idx, nictype, mac });
    }
  }

  Ok(nics)
}

// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :