Browse Source

search_pbf: Update closure can change control flow

Johannes Hofmann 7 years ago
parent
commit
3ffbe5011c
2 changed files with 28 additions and 7 deletions
  1. 4
    2
      src/main.rs
  2. 24
    5
      src/search.rs

+ 4
- 2
src/main.rs View File

250
             path,
250
             path,
251
             pattern,
251
             pattern,
252
             move |lat, lon| {
252
             move |lat, lon| {
253
-                marker_tx.send((lat, lon)).unwrap();
254
-                proxy.wakeup().unwrap();
253
+                if marker_tx.send((lat, lon)).is_err() {
254
+                    return search::ControlFlow::Break;
255
+                }
256
+                proxy.wakeup().into()
255
             },
257
             },
256
         )?;
258
         )?;
257
     }
259
     }

+ 24
- 5
src/search.rs View File

1
+use osmpbf::{Element, ElementReader};
1
 use regex::Regex;
2
 use regex::Regex;
2
 use std::path::{Path, PathBuf};
3
 use std::path::{Path, PathBuf};
3
 use std::thread;
4
 use std::thread;
4
-use osmpbf::{Element, ElementReader};
5
 
5
 
6
+#[derive(Debug, Eq, PartialEq)]
7
+pub enum ControlFlow {
8
+    Continue,
9
+    Break,
10
+}
11
+
12
+impl<T, E> From<Result<T, E>> for ControlFlow
13
+{
14
+    fn from(result: Result<T, E>) -> Self {
15
+        match result {
16
+            Ok(_) => ControlFlow::Continue,
17
+            Err(_) => ControlFlow::Break,
18
+        }
19
+    }
20
+}
6
 
21
 
22
+//TODO Add callbacks for other events: search finished, on error, ...
7
 pub fn search_pbf<P, F>(
23
 pub fn search_pbf<P, F>(
8
     pbf_path: P,
24
     pbf_path: P,
9
     search_pattern: &str,
25
     search_pattern: &str,
10
     update_func: F,
26
     update_func: F,
11
 ) -> Result<thread::JoinHandle<()>, String>
27
 ) -> Result<thread::JoinHandle<()>, String>
12
 where P: AsRef<Path>,
28
 where P: AsRef<Path>,
13
-      F: Fn(f64, f64) + Send + 'static,
29
+      F: Fn(f64, f64) -> ControlFlow + Send + 'static,
14
 {
30
 {
15
     let pathbuf = PathBuf::from(pbf_path.as_ref());
31
     let pathbuf = PathBuf::from(pbf_path.as_ref());
16
     let re = Regex::new(search_pattern)
32
     let re = Regex::new(search_pattern)
19
         .map_err(|e| format!("Failed to read PBF file {:?}: {}", pbf_path.as_ref(), e))?;
35
         .map_err(|e| format!("Failed to read PBF file {:?}: {}", pbf_path.as_ref(), e))?;
20
 
36
 
21
     let handle = thread::spawn(move|| {
37
     let handle = thread::spawn(move|| {
22
-        //TODO do something about the unwrap()
23
         reader.for_each(|element| {
38
         reader.for_each(|element| {
24
             match element {
39
             match element {
25
                 Element::Node(node) => {
40
                 Element::Node(node) => {
26
                     for (_key, val) in node.tags() {
41
                     for (_key, val) in node.tags() {
27
                         if re.is_match(val) {
42
                         if re.is_match(val) {
28
-                            update_func(node.lat(), node.lon());
43
+                            if update_func(node.lat(), node.lon()) == ControlFlow::Break {
44
+                                return;
45
+                            }
29
                             break;
46
                             break;
30
                         }
47
                         }
31
                     }
48
                     }
33
                 Element::DenseNode(dnode) => {
50
                 Element::DenseNode(dnode) => {
34
                     for (_key, val) in dnode.tags() {
51
                     for (_key, val) in dnode.tags() {
35
                         if re.is_match(val) {
52
                         if re.is_match(val) {
36
-                            update_func(dnode.lat(), dnode.lon());
53
+                            if update_func(dnode.lat(), dnode.lon()) == ControlFlow::Break {
54
+                                return;
55
+                            }
37
                             break;
56
                             break;
38
                         }
57
                         }
39
                     }
58
                     }