]> git.lizzy.rs Git - rust.git/commitdiff
consolidate error handling
authorAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 26 Jan 2019 12:19:24 +0000 (15:19 +0300)
committerAleksey Kladov <aleksey.kladov@gmail.com>
Sat, 26 Jan 2019 13:28:04 +0000 (16:28 +0300)
crates/ra_vfs/src/io.rs

index 669240488ad2820f3b61b067149f8b82a802f7f1..84ccdb394df5ebf2236e985aa913f8a1f9ca7dc7 100644 (file)
@@ -123,9 +123,7 @@ fn watch_root(
         .into_iter()
         .filter_map(|path| {
             let abs_path = path.to_path(&config.root);
-            let text = fs::read_to_string(abs_path)
-                .map_err(|e| log::warn!("watcher error: {}", e))
-                .ok()?;
+            let text = read_to_string(&abs_path)?;
             Some((path, text))
         })
         .collect();
@@ -194,9 +192,7 @@ fn handle_change(&self, path: PathBuf, kind: ChangeKind) -> Result<(), SendError
                     .into_iter()
                     .filter_map(|rel_path| {
                         let abs_path = rel_path.to_path(&config.root);
-                        let text = fs::read_to_string(&abs_path)
-                            .map_err(|e| log::warn!("watcher failed {}", e))
-                            .ok()?;
+                        let text = read_to_string(&abs_path)?;
                         Some((rel_path, text))
                     })
                     .try_for_each(|(path, text)| {
@@ -204,14 +200,15 @@ fn handle_change(&self, path: PathBuf, kind: ChangeKind) -> Result<(), SendError
                             .send(TaskResult::AddSingleFile { root, path, text })
                     })?
             }
-            ChangeKind::Write => match fs::read_to_string(&path) {
-                Err(e) => log::warn!("watcher failed {}", e),
-                Ok(text) => self.sender.send(TaskResult::ChangeSingleFile {
-                    root,
-                    path: rel_path,
-                    text,
-                })?,
-            },
+            ChangeKind::Write => {
+                if let Some(text) = read_to_string(&path) {
+                    self.sender.send(TaskResult::ChangeSingleFile {
+                        root,
+                        path: rel_path,
+                        text,
+                    })?;
+                }
+            }
             ChangeKind::Remove => self.sender.send(TaskResult::RemoveSingleFile {
                 root,
                 path: rel_path,
@@ -250,3 +247,9 @@ fn watch_one(watcher: &mut RecommendedWatcher, dir: &Path) {
         Err(e) => log::warn!("could not watch \"{}\": {}", dir.display(), e),
     }
 }
+
+fn read_to_string(path: &Path) -> Option<String> {
+    fs::read_to_string(&path)
+        .map_err(|e| log::warn!("failed to read file {}", e))
+        .ok()
+}