2019-07-26 16:54:25 -05:00
|
|
|
// run-pass
|
|
|
|
|
2018-09-17 04:18:35 -05:00
|
|
|
#![allow(non_camel_case_types)]
|
|
|
|
#![allow(dead_code)]
|
2015-03-05 20:33:58 -06:00
|
|
|
|
2022-05-05 11:34:13 -05:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2013-05-24 21:35:29 -05:00
|
|
|
use std::option;
|
2012-07-11 16:31:35 -05:00
|
|
|
|
2022-05-05 11:34:13 -05:00
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
enum Json {
|
|
|
|
I64(i64),
|
|
|
|
U64(u64),
|
|
|
|
F64(f64),
|
|
|
|
String(String),
|
|
|
|
Boolean(bool),
|
|
|
|
Array(Array),
|
|
|
|
Object(Object),
|
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
|
|
|
type Array = Vec<Json>;
|
|
|
|
type Object = BTreeMap<String, Json>;
|
|
|
|
|
2013-03-01 12:44:43 -06:00
|
|
|
enum object {
|
2012-07-11 16:31:35 -05:00
|
|
|
bool_value(bool),
|
|
|
|
int_value(i64),
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:34:13 -05:00
|
|
|
fn lookup(table: Object, key: String, default: String) -> String
|
2012-07-11 16:31:35 -05:00
|
|
|
{
|
2015-01-02 01:53:35 -06:00
|
|
|
match table.get(&key) {
|
2014-11-28 10:57:41 -06:00
|
|
|
option::Option::Some(&Json::String(ref s)) => {
|
2014-06-28 10:27:29 -05:00
|
|
|
s.to_string()
|
2012-07-11 16:31:35 -05:00
|
|
|
}
|
2014-11-28 10:57:41 -06:00
|
|
|
option::Option::Some(value) => {
|
2022-05-05 11:34:13 -05:00
|
|
|
println!("{} was expected to be a string but is a {:?}", key, value);
|
2012-07-11 16:31:35 -05:00
|
|
|
default
|
|
|
|
}
|
2014-11-28 10:57:41 -06:00
|
|
|
option::Option::None => {
|
2012-07-11 16:31:35 -05:00
|
|
|
default
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:34:13 -05:00
|
|
|
fn add_interface(_store: isize, managed_ip: String, data: Json) -> (String, object)
|
2012-07-11 16:31:35 -05:00
|
|
|
{
|
2013-07-02 14:47:32 -05:00
|
|
|
match &data {
|
2014-11-26 12:21:45 -06:00
|
|
|
&Json::Object(ref interface) => {
|
2014-06-28 10:27:29 -05:00
|
|
|
let name = lookup(interface.clone(),
|
2014-05-25 05:17:19 -05:00
|
|
|
"ifDescr".to_string(),
|
|
|
|
"".to_string());
|
2014-05-27 22:44:58 -05:00
|
|
|
let label = format!("{}-{}", managed_ip, name);
|
2012-07-11 16:31:35 -05:00
|
|
|
|
2014-11-06 02:05:53 -06:00
|
|
|
(label, object::bool_value(false))
|
2012-07-11 16:31:35 -05:00
|
|
|
}
|
2013-07-02 14:47:32 -05:00
|
|
|
_ => {
|
2022-05-05 11:34:13 -05:00
|
|
|
println!("Expected dict for {} interfaces, found {:?}", managed_ip, data);
|
2014-11-06 02:05:53 -06:00
|
|
|
("gnos:missing-interface".to_string(), object::bool_value(true))
|
2012-07-11 16:31:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 11:34:13 -05:00
|
|
|
fn add_interfaces(store: isize, managed_ip: String, device: HashMap<String, Json>)
|
2014-05-22 18:57:53 -05:00
|
|
|
-> Vec<(String, object)> {
|
2015-03-21 20:15:47 -05:00
|
|
|
match device["interfaces"] {
|
2014-11-26 12:21:45 -06:00
|
|
|
Json::Array(ref interfaces) =>
|
2012-07-11 16:31:35 -05:00
|
|
|
{
|
2014-03-05 17:28:08 -06:00
|
|
|
interfaces.iter().map(|interface| {
|
2013-07-02 14:47:32 -05:00
|
|
|
add_interface(store, managed_ip.clone(), (*interface).clone())
|
2014-03-05 17:28:08 -06:00
|
|
|
}).collect()
|
2012-07-11 16:31:35 -05:00
|
|
|
}
|
2012-08-03 21:59:04 -05:00
|
|
|
_ =>
|
2012-07-11 16:31:35 -05:00
|
|
|
{
|
2022-05-05 11:34:13 -05:00
|
|
|
println!("Expected list for {} interfaces, found {:?}", managed_ip,
|
2015-03-21 20:15:47 -05:00
|
|
|
device["interfaces"]);
|
2014-03-05 16:02:44 -06:00
|
|
|
Vec::new()
|
2012-07-11 16:31:35 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-01 21:43:17 -06:00
|
|
|
pub fn main() {}
|