Procházet zdrojové kódy

Add some error handling to example

Johannes Hofmann před 8 roky
rodič
revize
5f1f1383a3
1 změnil soubory, kde provedl 13 přidání a 6 odebrání
  1. 13
    6
      examples/count.rs

+ 13
- 6
examples/count.rs Zobrazit soubor

1
 // Count the number of nodes, ways and relations in a PBF file given as the
1
 // Count the number of nodes, ways and relations in a PBF file given as the
2
 // first command line argument.
2
 // first command line argument.
3
 
3
 
4
+extern crate error_chain;
4
 extern crate osmpbf;
5
 extern crate osmpbf;
5
 
6
 
6
 use osmpbf::*;
7
 use osmpbf::*;
8
+use error_chain::ChainedError;
7
 
9
 
8
 fn main() {
10
 fn main() {
9
     let arg = std::env::args_os().nth(1).expect("need a *.osm.pbf file as argument");
11
     let arg = std::env::args_os().nth(1).expect("need a *.osm.pbf file as argument");
12
 
14
 
13
     println!("Counting...");
15
     println!("Counting...");
14
 
16
 
15
-    let result = reader.par_map_reduce(
17
+    match reader.par_map_reduce(
16
         |element| {
18
         |element| {
17
             match element {
19
             match element {
18
                 Element::Node(_) | Element::DenseNode(_) => (1, 0, 0),
20
                 Element::Node(_) | Element::DenseNode(_) => (1, 0, 0),
22
         },
24
         },
23
         || (0u64, 0u64, 0u64),
25
         || (0u64, 0u64, 0u64),
24
         |a, b| (a.0 + b.0, a.1 + b.1, a.2 + b.2)
26
         |a, b| (a.0 + b.0, a.1 + b.1, a.2 + b.2)
25
-    ).unwrap();
26
-
27
-    println!("Nodes: {}", result.0);
28
-    println!("Ways: {}", result.1);
29
-    println!("Relations: {}", result.2);
27
+    ) {
28
+        Ok((nodes, ways, relations)) => {
29
+            println!("Nodes: {}", nodes);
30
+            println!("Ways: {}", ways);
31
+            println!("Relations: {}", relations);
32
+        },
33
+        Err(e) => {
34
+            println!("{}", e.display_chain().to_string());
35
+        },
36
+    }
30
 }
37
 }