Skip to content

Commit 929281a

Browse files
authored
Introduce PmtResult type (#28)
1 parent b2bb41a commit 929281a

File tree

7 files changed

+30
-29
lines changed

7 files changed

+30
-29
lines changed

src/async_reader.rs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::cache::DirCacheResult;
1717
#[cfg(any(feature = "http-async", feature = "mmap-async-tokio"))]
1818
use crate::cache::{DirectoryCache, NoCache};
1919
use crate::directory::{DirEntry, Directory};
20-
use crate::error::PmtError;
20+
use crate::error::{PmtError, PmtResult};
2121
use crate::header::{HEADER_SIZE, MAX_INITIAL_BYTES};
2222
#[cfg(feature = "http-async")]
2323
use crate::http::HttpBackend;
@@ -37,7 +37,7 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B, NoCache> {
3737
/// Creates a new reader from a specified source and validates the provided `PMTiles` archive is valid.
3838
///
3939
/// Note: Prefer using `new_with_*` methods.
40-
pub async fn try_from_source(backend: B) -> Result<Self, PmtError> {
40+
pub async fn try_from_source(backend: B) -> PmtResult<Self> {
4141
Self::try_from_cached_source(backend, NoCache).await
4242
}
4343
}
@@ -46,7 +46,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
4646
/// Creates a new cached reader from a specified source and validates the provided `PMTiles` archive is valid.
4747
///
4848
/// Note: Prefer using `new_with_*` methods.
49-
pub async fn try_from_cached_source(backend: B, cache: C) -> Result<Self, PmtError> {
49+
pub async fn try_from_cached_source(backend: B, cache: C) -> PmtResult<Self> {
5050
// Read the first 127 and up to 16,384 bytes to ensure we can initialize the header and root directory.
5151
let mut initial_bytes = backend.read(0, MAX_INITIAL_BYTES).await?;
5252
if initial_bytes.len() < HEADER_SIZE {
@@ -91,7 +91,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
9191
///
9292
/// Note: by spec, this should be valid JSON. This method currently returns a [String].
9393
/// This may change in the future.
94-
pub async fn get_metadata(&self) -> Result<String, PmtError> {
94+
pub async fn get_metadata(&self) -> PmtResult<String> {
9595
let offset = self.header.metadata_offset as _;
9696
let length = self.header.metadata_length as _;
9797
let metadata = self.backend.read_exact(offset, length).await?;
@@ -103,10 +103,7 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
103103
}
104104

105105
#[cfg(feature = "tilejson")]
106-
pub async fn parse_tilejson(
107-
&self,
108-
sources: Vec<String>,
109-
) -> Result<tilejson::TileJSON, PmtError> {
106+
pub async fn parse_tilejson(&self, sources: Vec<String>) -> PmtResult<tilejson::TileJSON> {
110107
use serde_json::Value;
111108

112109
let meta = self.get_metadata().await?;
@@ -190,20 +187,20 @@ impl<B: AsyncBackend + Sync + Send, C: DirectoryCache + Sync + Send> AsyncPmTile
190187
entry
191188
}
192189

193-
async fn read_directory(&self, offset: usize, length: usize) -> Result<Directory, PmtError> {
190+
async fn read_directory(&self, offset: usize, length: usize) -> PmtResult<Directory> {
194191
let data = self.backend.read_exact(offset, length).await?;
195192
Self::read_compressed_directory(self.header.internal_compression, data).await
196193
}
197194

198195
async fn read_compressed_directory(
199196
compression: Compression,
200197
bytes: Bytes,
201-
) -> Result<Directory, PmtError> {
198+
) -> PmtResult<Directory> {
202199
let decompressed_bytes = Self::decompress(compression, bytes).await?;
203200
Directory::try_from(decompressed_bytes)
204201
}
205202

206-
async fn decompress(compression: Compression, bytes: Bytes) -> Result<Bytes, PmtError> {
203+
async fn decompress(compression: Compression, bytes: Bytes) -> PmtResult<Bytes> {
207204
let mut decompressed_bytes = Vec::with_capacity(bytes.len() * 2);
208205
match compression {
209206
Compression::Gzip => {
@@ -223,7 +220,7 @@ impl AsyncPmTilesReader<HttpBackend, NoCache> {
223220
/// Creates a new `PMTiles` reader from a URL using the Reqwest backend.
224221
///
225222
/// Fails if [url] does not exist or is an invalid archive. (Note: HTTP requests are made to validate it.)
226-
pub async fn new_with_url<U: IntoUrl>(client: Client, url: U) -> Result<Self, PmtError> {
223+
pub async fn new_with_url<U: IntoUrl>(client: Client, url: U) -> PmtResult<Self> {
227224
Self::new_with_cached_url(NoCache, client, url).await
228225
}
229226
}
@@ -237,7 +234,7 @@ impl<C: DirectoryCache + Sync + Send> AsyncPmTilesReader<HttpBackend, C> {
237234
cache: C,
238235
client: Client,
239236
url: U,
240-
) -> Result<Self, PmtError> {
237+
) -> PmtResult<Self> {
241238
let backend = HttpBackend::try_from(client, url)?;
242239

243240
Self::try_from_cached_source(backend, cache).await
@@ -249,7 +246,7 @@ impl AsyncPmTilesReader<MmapBackend, NoCache> {
249246
/// Creates a new `PMTiles` reader from a file path using the async mmap backend.
250247
///
251248
/// Fails if [p] does not exist or is an invalid archive.
252-
pub async fn new_with_path<P: AsRef<Path>>(path: P) -> Result<Self, PmtError> {
249+
pub async fn new_with_path<P: AsRef<Path>>(path: P) -> PmtResult<Self> {
253250
Self::new_with_cached_path(NoCache, path).await
254251
}
255252
}
@@ -259,7 +256,7 @@ impl<C: DirectoryCache + Sync + Send> AsyncPmTilesReader<MmapBackend, C> {
259256
/// Creates a new cached `PMTiles` reader from a file path using the async mmap backend.
260257
///
261258
/// Fails if [p] does not exist or is an invalid archive.
262-
pub async fn new_with_cached_path<P: AsRef<Path>>(cache: C, path: P) -> Result<Self, PmtError> {
259+
pub async fn new_with_cached_path<P: AsRef<Path>>(cache: C, path: P) -> PmtResult<Self> {
263260
let backend = MmapBackend::try_from(path).await?;
264261

265262
Self::try_from_cached_source(backend, cache).await
@@ -269,10 +266,10 @@ impl<C: DirectoryCache + Sync + Send> AsyncPmTilesReader<MmapBackend, C> {
269266
#[async_trait]
270267
pub trait AsyncBackend {
271268
/// Reads exactly `length` bytes starting at `offset`
272-
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, PmtError>;
269+
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes>;
273270

274271
/// Reads up to `length` bytes starting at `offset`.
275-
async fn read(&self, offset: usize, length: usize) -> Result<Bytes, PmtError>;
272+
async fn read(&self, offset: usize, length: usize) -> PmtResult<Bytes>;
276273
}
277274

278275
#[cfg(test)]

src/directory.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Directory {
4242
impl TryFrom<Bytes> for Directory {
4343
type Error = PmtError;
4444

45-
fn try_from(buffer: Bytes) -> Result<Self, PmtError> {
45+
fn try_from(buffer: Bytes) -> Result<Self, Self::Error> {
4646
let mut buffer = buffer.reader();
4747
let n_entries = buffer.read_usize_varint()?;
4848

src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ use std::string::FromUtf8Error;
22

33
use thiserror::Error;
44

5+
/// A specialized [`Result`] type for `PMTiles` operations.
6+
pub type PmtResult<T> = Result<T, PmtError>;
7+
8+
/// Errors that can occur while reading `PMTiles` files.
59
#[derive(Debug, Error)]
610
pub enum PmtError {
711
#[error("Invalid magic number")]

src/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::panic::catch_unwind;
33

44
use bytes::{Buf, Bytes};
55

6-
use crate::error::PmtError;
6+
use crate::error::{PmtError, PmtResult};
77

88
#[cfg(any(feature = "http-async", feature = "mmap-async-tokio"))]
99
pub(crate) const MAX_INITIAL_BYTES: usize = 16_384;
@@ -154,7 +154,7 @@ impl Header {
154154
buf.get_i32_le() as f32 / 10_000_000.
155155
}
156156

157-
pub fn try_from_bytes(mut bytes: Bytes) -> Result<Self, PmtError> {
157+
pub fn try_from_bytes(mut bytes: Bytes) -> PmtResult<Self> {
158158
let magic_bytes = bytes.split_to(V3_MAGIC.len());
159159

160160
// Assert magic

src/http.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use reqwest::header::{HeaderValue, RANGE};
44
use reqwest::{Client, IntoUrl, Method, Request, StatusCode, Url};
55

66
use crate::async_reader::AsyncBackend;
7-
use crate::error::{PmtError, PmtHttpError};
7+
use crate::error::{PmtHttpError, PmtResult};
88

99
pub struct HttpBackend {
1010
client: Client,
1111
pmtiles_url: Url,
1212
}
1313

1414
impl HttpBackend {
15-
pub fn try_from<U: IntoUrl>(client: Client, url: U) -> Result<Self, PmtError> {
15+
pub fn try_from<U: IntoUrl>(client: Client, url: U) -> PmtResult<Self> {
1616
Ok(HttpBackend {
1717
client,
1818
pmtiles_url: url.into_url()?,
@@ -22,7 +22,7 @@ impl HttpBackend {
2222

2323
#[async_trait]
2424
impl AsyncBackend for HttpBackend {
25-
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, PmtError> {
25+
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
2626
let data = self.read(offset, length).await?;
2727

2828
if data.len() == length {
@@ -32,7 +32,7 @@ impl AsyncBackend for HttpBackend {
3232
}
3333
}
3434

35-
async fn read(&self, offset: usize, length: usize) -> Result<Bytes, PmtError> {
35+
async fn read(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
3636
let end = offset + length - 1;
3737
let range = format!("bytes={offset}-{end}");
3838
let range = HeaderValue::try_from(range).map_err(PmtHttpError::from)?;

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ mod directory;
99
pub use directory::{DirEntry, Directory};
1010

1111
mod error;
12-
pub use error::PmtError;
1312
#[cfg(feature = "http-async")]
1413
pub use error::PmtHttpError;
14+
pub use error::{PmtError, PmtResult};
1515

1616
#[cfg(feature = "http-async")]
1717
pub mod http;

src/mmap.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use bytes::{Buf, Bytes};
66
use fmmap::tokio::{AsyncMmapFile, AsyncMmapFileExt as _, AsyncOptions};
77

88
use crate::async_reader::AsyncBackend;
9-
use crate::error::PmtError;
9+
use crate::error::{PmtError, PmtResult};
1010

1111
pub struct MmapBackend {
1212
file: AsyncMmapFile,
1313
}
1414

1515
impl MmapBackend {
16-
pub async fn try_from<P: AsRef<Path>>(p: P) -> Result<Self, PmtError> {
16+
pub async fn try_from<P: AsRef<Path>>(p: P) -> PmtResult<Self> {
1717
Ok(Self {
1818
file: AsyncMmapFile::open_with_options(p, AsyncOptions::new().read(true))
1919
.await
@@ -30,7 +30,7 @@ impl From<fmmap::error::Error> for PmtError {
3030

3131
#[async_trait]
3232
impl AsyncBackend for MmapBackend {
33-
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, PmtError> {
33+
async fn read_exact(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
3434
if self.file.len() >= offset + length {
3535
Ok(self.file.reader(offset)?.copy_to_bytes(length))
3636
} else {
@@ -40,7 +40,7 @@ impl AsyncBackend for MmapBackend {
4040
}
4141
}
4242

43-
async fn read(&self, offset: usize, length: usize) -> Result<Bytes, PmtError> {
43+
async fn read(&self, offset: usize, length: usize) -> PmtResult<Bytes> {
4444
let reader = self.file.reader(offset)?;
4545

4646
let read_length = length.min(reader.len());

0 commit comments

Comments
 (0)