Przeglądaj źródła

Add BlobReader::seekable_from_path convenience function

Johannes Hofmann 7 lat temu
rodzic
commit
38020e8b19
1 zmienionych plików z 24 dodań i 0 usunięć
  1. 24
    0
      src/blob.rs

+ 24
- 0
src/blob.rs Wyświetl plik

@@ -329,6 +329,30 @@ impl<R: Read + Seek> BlobReader<R> {
329 329
     }
330 330
 }
331 331
 
332
+impl BlobReader<BufReader<File>> {
333
+    /// Creates a new `BlobReader` from the given path that is seekable and will be initialized
334
+    /// with a valid offset.
335
+    ///
336
+    /// # Example
337
+    /// ```
338
+    /// use osmpbf::*;
339
+    ///
340
+    /// # fn foo() -> Result<()> {
341
+    /// let mut reader = BlobReader::seekable_from_path("tests/test.osm.pbf")?;
342
+    /// let first_blob = reader.next().unwrap()?;
343
+    ///
344
+    /// assert_eq!(first_blob.offset(), Some(ByteOffset(0)));
345
+    /// # Ok(())
346
+    /// # }
347
+    /// # foo().unwrap();
348
+    /// ```
349
+    pub fn seekable_from_path<P: AsRef<Path>>(path: P) -> Result<BlobReader<BufReader<File>>> {
350
+        let f = File::open(path.as_ref())?;
351
+        let buf_reader = BufReader::new(f);
352
+        Self::new_seekable(buf_reader)
353
+    }
354
+}
355
+
332 356
 #[cfg(feature = "system-libz")]
333 357
 pub(crate) fn decode_blob<T>(blob: &fileformat::Blob) -> Result<T>
334 358
     where T: protobuf::Message {