rust/hg-core/src/vfs.rs
changeset 49485 ffd4b1f1c9cb
parent 48418 abeae090ce67
child 49914 58074252db3c
child 50180 be019ac8c1e4
--- a/rust/hg-core/src/vfs.rs	Tue Jul 19 15:37:09 2022 +0200
+++ b/rust/hg-core/src/vfs.rs	Tue Jul 19 15:37:45 2022 +0200
@@ -40,6 +40,23 @@
         std::fs::read(&path).when_reading_file(&path)
     }
 
+    /// Returns `Ok(None)` if the file does not exist.
+    pub fn try_read(
+        &self,
+        relative_path: impl AsRef<Path>,
+    ) -> Result<Option<Vec<u8>>, HgError> {
+        match self.read(relative_path) {
+            Err(e) => match &e {
+                HgError::IoError { error, .. } => match error.kind() {
+                    ErrorKind::NotFound => return Ok(None),
+                    _ => Err(e),
+                },
+                _ => Err(e),
+            },
+            Ok(v) => Ok(Some(v)),
+        }
+    }
+
     fn mmap_open_gen(
         &self,
         relative_path: impl AsRef<Path>,