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