]>
Commit | Line | Data |
---|---|---|
1a4d82fc | 1 | // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT |
223e47cc LB |
2 | // file at the top-level directory of this distribution and at |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
c34b1796 AL |
11 | #![feature(collections)] |
12 | #![feature(rustc_private)] | |
13 | ||
1a4d82fc JJ |
14 | extern crate collections; |
15 | extern crate serialize; | |
970d7e83 | 16 | |
1a4d82fc JJ |
17 | use std::collections::HashMap; |
18 | use serialize::json::{self, Json}; | |
970d7e83 | 19 | use std::option; |
223e47cc LB |
20 | |
21 | enum object { | |
22 | bool_value(bool), | |
23 | int_value(i64), | |
24 | } | |
25 | ||
1a4d82fc | 26 | fn lookup(table: json::Object, key: String, default: String) -> String |
223e47cc | 27 | { |
1a4d82fc JJ |
28 | match table.get(&key) { |
29 | option::Option::Some(&Json::String(ref s)) => { | |
30 | s.to_string() | |
223e47cc | 31 | } |
1a4d82fc JJ |
32 | option::Option::Some(value) => { |
33 | println!("{} was expected to be a string but is a {}", key, value); | |
223e47cc LB |
34 | default |
35 | } | |
1a4d82fc | 36 | option::Option::None => { |
223e47cc LB |
37 | default |
38 | } | |
39 | } | |
40 | } | |
41 | ||
c34b1796 | 42 | fn add_interface(_store: isize, managed_ip: String, data: json::Json) -> (String, object) |
223e47cc | 43 | { |
1a4d82fc JJ |
44 | match &data { |
45 | &Json::Object(ref interface) => { | |
46 | let name = lookup(interface.clone(), | |
47 | "ifDescr".to_string(), | |
48 | "".to_string()); | |
49 | let label = format!("{}-{}", managed_ip, name); | |
223e47cc | 50 | |
1a4d82fc | 51 | (label, object::bool_value(false)) |
223e47cc | 52 | } |
1a4d82fc JJ |
53 | _ => { |
54 | println!("Expected dict for {} interfaces, found {}", managed_ip, data); | |
55 | ("gnos:missing-interface".to_string(), object::bool_value(true)) | |
223e47cc LB |
56 | } |
57 | } | |
58 | } | |
59 | ||
c34b1796 | 60 | fn add_interfaces(store: isize, managed_ip: String, device: HashMap<String, json::Json>) |
1a4d82fc | 61 | -> Vec<(String, object)> { |
c34b1796 | 62 | match device["interfaces"] { |
1a4d82fc | 63 | Json::Array(ref interfaces) => |
223e47cc | 64 | { |
1a4d82fc JJ |
65 | interfaces.iter().map(|interface| { |
66 | add_interface(store, managed_ip.clone(), (*interface).clone()) | |
67 | }).collect() | |
223e47cc LB |
68 | } |
69 | _ => | |
70 | { | |
1a4d82fc | 71 | println!("Expected list for {} interfaces, found {}", managed_ip, |
c34b1796 | 72 | device["interfaces"]); |
1a4d82fc | 73 | Vec::new() |
223e47cc LB |
74 | } |
75 | } | |
76 | } | |
77 | ||
78 | pub fn main() {} |