]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/issue-2804.rs
Doc says to avoid mixing allocator instead of forbiding it
[rust.git] / src / test / run-pass / issue-2804.rs
1
2 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
3 // file at the top-level directory of this distribution and at
4 // http://rust-lang.org/COPYRIGHT.
5 //
6 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9 // option. This file may not be copied, modified, or distributed
10 // except according to those terms.
11
12 extern crate collections;
13 extern crate serialize;
14 extern crate debug;
15
16 use std::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: String, default: String) -> String
26 {
27     match table.find(&key.to_string()) {
28         option::Some(&json::String(ref s)) => {
29             s.to_string()
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: String, data: json::Json) -> (String, object)
42 {
43     match &data {
44         &json::Object(ref interface) => {
45             let name = lookup(interface.clone(),
46                               "ifDescr".to_string(),
47                               "".to_string());
48             let label = format!("{}-{}", managed_ip, name);
49
50             (label, bool_value(false))
51         }
52         _ => {
53             println!("Expected dict for {} interfaces, found {:?}", managed_ip, data);
54             ("gnos:missing-interface".to_string(), bool_value(true))
55         }
56     }
57 }
58
59 fn add_interfaces(store: int, managed_ip: String, device: HashMap<String, json::Json>)
60 -> Vec<(String, object)> {
61     match device.get(&"interfaces".to_string())
62     {
63         &json::List(ref interfaces) =>
64         {
65           interfaces.iter().map(|interface| {
66                 add_interface(store, managed_ip.clone(), (*interface).clone())
67           }).collect()
68         }
69         _ =>
70         {
71             println!("Expected list for {} interfaces, found {:?}", managed_ip,
72                    device.get(&"interfaces".to_string()));
73             Vec::new()
74         }
75     }
76 }
77
78 pub fn main() {}