Skip to content

Commit f68a7c3

Browse files
committed
Rename Error and HttpError, and move them to root
Even clippy now suggests not to use `Error` name because it is becoming increasingly conflicting with other libs. Renamed things to `PmtError` and `PmtHttpError`.
1 parent 2de5837 commit f68a7c3

File tree

7 files changed

+57
-49
lines changed

7 files changed

+57
-49
lines changed

src/async_reader.rs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use reqwest::{Client, IntoUrl};
1010
use tokio::io::AsyncReadExt;
1111

1212
use crate::directory::{Directory, Entry};
13-
use crate::error::Error;
13+
use crate::error::PmtError;
1414
use crate::header::{HEADER_SIZE, MAX_INITIAL_BYTES};
1515
#[cfg(feature = "http-async")]
1616
use crate::http::HttpBackend;
@@ -29,11 +29,11 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B> {
2929
/// Creates a new reader from a specified source and validates the provided PMTiles archive is valid.
3030
///
3131
/// Note: Prefer using new_with_* methods.
32-
pub async fn try_from_source(backend: B) -> Result<Self, Error> {
32+
pub async fn try_from_source(backend: B) -> Result<Self, PmtError> {
3333
// Read the first 127 and up to 16,384 bytes to ensure we can initialize the header and root directory.
3434
let mut initial_bytes = backend.read(0, MAX_INITIAL_BYTES).await?;
3535
if initial_bytes.len() < HEADER_SIZE {
36-
return Err(Error::InvalidHeader);
36+
return Err(PmtError::InvalidHeader);
3737
}
3838

3939
let header = Header::try_from_bytes(initial_bytes.split_to(HEADER_SIZE))?;
@@ -73,7 +73,7 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B> {
7373
///
7474
/// Note: by spec, this should be valid JSON. This method currently returns a [String].
7575
/// This may change in the future.
76-
pub async fn get_metadata(&self) -> Result<String, Error> {
76+
pub async fn get_metadata(&self) -> Result<String, PmtError> {
7777
let offset = self.header.metadata_offset as _;
7878
let length = self.header.metadata_length as _;
7979
let metadata = self.backend.read_exact(offset, length).await?;
@@ -85,13 +85,16 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B> {
8585
}
8686

8787
#[cfg(feature = "tilejson")]
88-
pub async fn parse_tilejson(&self, sources: Vec<String>) -> Result<tilejson::TileJSON, Error> {
88+
pub async fn parse_tilejson(
89+
&self,
90+
sources: Vec<String>,
91+
) -> Result<tilejson::TileJSON, PmtError> {
8992
use serde_json::Value;
9093

9194
let meta = self.get_metadata().await?;
92-
let meta: Value = serde_json::from_str(&meta).map_err(|_| Error::InvalidMetadata)?;
95+
let meta: Value = serde_json::from_str(&meta).map_err(|_| PmtError::InvalidMetadata)?;
9396
let Value::Object(meta) = meta else {
94-
return Err(Error::InvalidMetadata);
97+
return Err(PmtError::InvalidMetadata);
9598
};
9699

97100
let mut tj = self.header.get_tilejson(sources);
@@ -117,7 +120,7 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B> {
117120
if let Ok(v) = serde_json::from_value::<Vec<tilejson::VectorLayer>>(value) {
118121
tj.vector_layers = Some(v);
119122
} else {
120-
return Err(Error::InvalidMetadata);
123+
return Err(PmtError::InvalidMetadata);
121124
}
122125
} else {
123126
tj.other.insert(key, value);
@@ -159,20 +162,20 @@ impl<B: AsyncBackend + Sync + Send> AsyncPmTilesReader<B> {
159162
entry.cloned()
160163
}
161164

162-
async fn read_directory(&self, offset: usize, length: usize) -> Result<Directory, Error> {
165+
async fn read_directory(&self, offset: usize, length: usize) -> Result<Directory, PmtError> {
163166
let data = self.backend.read_exact(offset, length).await?;
164167
Self::read_compressed_directory(self.header.internal_compression, data).await
165168
}
166169

167170
async fn read_compressed_directory(
168171
compression: Compression,
169172
bytes: Bytes,
170-
) -> Result<Directory, Error> {
173+
) -> Result<Directory, PmtError> {
171174
let decompressed_bytes = Self::decompress(compression, bytes).await?;
172175
Directory::try_from(decompressed_bytes)
173176
}
174177

175-
async fn decompress(compression: Compression, bytes: Bytes) -> Result<Bytes, Error> {
178+
async fn decompress(compression: Compression, bytes: Bytes) -> Result<Bytes, PmtError> {
176179
let mut decompressed_bytes = Vec::with_capacity(bytes.len() * 2);
177180
match compression {
178181
Compression::Gzip => {
@@ -192,7 +195,7 @@ impl AsyncPmTilesReader<HttpBackend> {
192195
/// Creates a new PMTiles reader from a URL using the Reqwest backend.
193196
///
194197
/// Fails if [url] does not exist or is an invalid archive. (Note: HTTP requests are made to validate it.)
195-
pub async fn new_with_url<U: IntoUrl>(client: Client, url: U) -> Result<Self, Error> {
198+
pub async fn new_with_url<U: IntoUrl>(client: Client, url: U) -> Result<Self, PmtError> {
196199
let backend = HttpBackend::try_from(client, url)?;
197200

198201
Self::try_from_source(backend).await
@@ -204,7 +207,7 @@ impl AsyncPmTilesReader<MmapBackend> {
204207
/// Creates a new PMTiles reader from a file path using the async mmap backend.
205208
///
206209
/// Fails if [p] does not exist or is an invalid archive.
207-
pub async fn new_with_path<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
210+
pub async fn new_with_path<P: AsRef<Path>>(path: P) -> Result<Self, PmtError> {
208211
let backend = MmapBackend::try_from(path).await?;
209212

210213
Self::try_from_source(backend).await
@@ -214,10 +217,10 @@ impl AsyncPmTilesReader<MmapBackend> {
214217
#[async_trait]
215218
pub trait AsyncBackend {
216219
/// Reads exactly `length` bytes starting at `offset`
217-
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, Error>;
220+
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, PmtError>;
218221

219222
/// Reads up to `length` bytes starting at `offset`.
220-
async fn read(&self, offset: usize, length: usize) -> Result<Bytes, Error>;
223+
async fn read(&self, offset: usize, length: usize) -> Result<Bytes, PmtError>;
221224
}
222225

223226
#[cfg(test)]

src/directory.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt::{Debug, Formatter};
33
use bytes::{Buf, Bytes};
44
use varint_rs::VarintReader;
55

6-
use crate::error::Error;
6+
use crate::error::PmtError;
77

88
pub(crate) struct Directory {
99
entries: Vec<Entry>,
@@ -38,9 +38,9 @@ impl Directory {
3838
}
3939

4040
impl TryFrom<Bytes> for Directory {
41-
type Error = Error;
41+
type Error = PmtError;
4242

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

@@ -68,7 +68,7 @@ impl TryFrom<Bytes> for Directory {
6868
for entry in entries.iter_mut() {
6969
let offset = buffer.read_u64_varint()?;
7070
entry.offset = if offset == 0 {
71-
let e = last_entry.ok_or(Error::InvalidEntry)?;
71+
let e = last_entry.ok_or(PmtError::InvalidEntry)?;
7272
e.offset + e.length as u64
7373
} else {
7474
offset - 1

src/error.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::string::FromUtf8Error;
33
use thiserror::Error;
44

55
#[derive(Debug, Error)]
6-
pub enum Error {
6+
pub enum PmtError {
77
#[error("Invalid magic number")]
88
InvalidMagicNumber,
99
#[error("Invalid PMTiles version")]
@@ -27,12 +27,12 @@ pub enum Error {
2727
UnableToOpenMmapFile,
2828
#[cfg(feature = "http-async")]
2929
#[error("{0}")]
30-
Http(#[from] HttpError),
30+
Http(#[from] PmtHttpError),
3131
}
3232

3333
#[cfg(feature = "http-async")]
3434
#[derive(Debug, Error)]
35-
pub enum HttpError {
35+
pub enum PmtHttpError {
3636
#[error("Unexpected number of bytes returned [expected: {0}, received: {1}].")]
3737
UnexpectedNumberOfBytesReturned(usize, usize),
3838
#[error("Range requests unsupported")]
@@ -47,8 +47,8 @@ pub enum HttpError {
4747

4848
// This is required because thiserror #[from] does not support two-level conversion.
4949
#[cfg(feature = "http-async")]
50-
impl From<reqwest::Error> for Error {
50+
impl From<reqwest::Error> for PmtError {
5151
fn from(e: reqwest::Error) -> Self {
52-
Self::Http(HttpError::Http(e))
52+
Self::Http(PmtHttpError::Http(e))
5353
}
5454
}

src/header.rs

Lines changed: 9 additions & 9 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::Error;
6+
use crate::error::PmtError;
77

88
#[cfg(any(feature = "http-async", feature = "mmap-async-tokio"))]
99
pub(crate) const MAX_INITIAL_BYTES: usize = 16_384;
@@ -59,7 +59,7 @@ impl Compression {
5959
}
6060

6161
impl TryInto<Compression> for u8 {
62-
type Error = Error;
62+
type Error = PmtError;
6363

6464
fn try_into(self) -> Result<Compression, Self::Error> {
6565
match self {
@@ -68,7 +68,7 @@ impl TryInto<Compression> for u8 {
6868
2 => Ok(Compression::Gzip),
6969
3 => Ok(Compression::Brotli),
7070
4 => Ok(Compression::Zstd),
71-
_ => Err(Error::InvalidCompression),
71+
_ => Err(PmtError::InvalidCompression),
7272
}
7373
}
7474
}
@@ -125,7 +125,7 @@ impl TileType {
125125
}
126126

127127
impl TryInto<TileType> for u8 {
128-
type Error = Error;
128+
type Error = PmtError;
129129

130130
fn try_into(self) -> Result<TileType, Self::Error> {
131131
match self {
@@ -134,7 +134,7 @@ impl TryInto<TileType> for u8 {
134134
2 => Ok(TileType::Png),
135135
3 => Ok(TileType::Jpeg),
136136
4 => Ok(TileType::Webp),
137-
_ => Err(Error::InvalidTileType),
137+
_ => Err(PmtError::InvalidTileType),
138138
}
139139
}
140140
}
@@ -147,15 +147,15 @@ impl Header {
147147
buf.get_i32_le() as f32 / 10_000_000.
148148
}
149149

150-
pub fn try_from_bytes(mut bytes: Bytes) -> Result<Self, Error> {
150+
pub fn try_from_bytes(mut bytes: Bytes) -> Result<Self, PmtError> {
151151
let magic_bytes = bytes.split_to(V3_MAGIC.len());
152152

153153
// Assert magic
154154
if magic_bytes != V3_MAGIC {
155155
return Err(if magic_bytes.starts_with(V2_MAGIC.as_bytes()) {
156-
Error::UnsupportedPmTilesVersion
156+
PmtError::UnsupportedPmTilesVersion
157157
} else {
158-
Error::InvalidMagicNumber
158+
PmtError::InvalidMagicNumber
159159
});
160160
}
161161

@@ -189,7 +189,7 @@ impl Header {
189189
center_latitude: Self::read_coordinate_part(&mut bytes),
190190
})
191191
})
192-
.map_err(|_| Error::InvalidHeader)?
192+
.map_err(|_| PmtError::InvalidHeader)?
193193
}
194194
}
195195

src/http.rs

Lines changed: 8 additions & 8 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::{Error, HttpError};
7+
use crate::error::{PmtError, PmtHttpError};
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, Error> {
15+
pub fn try_from<U: IntoUrl>(client: Client, url: U) -> Result<Self, PmtError> {
1616
Ok(HttpBackend {
1717
client,
1818
pmtiles_url: url.into_url()?,
@@ -22,32 +22,32 @@ impl HttpBackend {
2222

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

2828
if data.len() == length {
2929
Ok(data)
3030
} else {
31-
Err(HttpError::UnexpectedNumberOfBytesReturned(length, data.len()).into())
31+
Err(PmtHttpError::UnexpectedNumberOfBytesReturned(length, data.len()).into())
3232
}
3333
}
3434

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

4040
let mut req = Request::new(Method::GET, self.pmtiles_url.clone());
4141
req.headers_mut().insert(RANGE, range);
4242

4343
let response = self.client.execute(req).await?.error_for_status()?;
4444
if response.status() != StatusCode::PARTIAL_CONTENT {
45-
return Err(HttpError::RangeRequestsUnsupported.into());
45+
return Err(PmtHttpError::RangeRequestsUnsupported.into());
4646
}
4747

4848
let response_bytes = response.bytes().await?;
4949
if response_bytes.len() > length {
50-
Err(HttpError::ResponseBodyTooLong(response_bytes.len(), length).into())
50+
Err(PmtHttpError::ResponseBodyTooLong(response_bytes.len(), length).into())
5151
} else {
5252
Ok(response_bytes)
5353
}

src/lib.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,12 @@
33
pub use crate::header::{Compression, Header, TileType};
44

55
mod directory;
6-
pub mod error;
6+
7+
mod error;
8+
pub use error::PmtError;
9+
#[cfg(feature = "http-async")]
10+
pub use error::PmtHttpError;
11+
712
mod header;
813

914
#[cfg(feature = "http-async")]

src/mmap.rs

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

88
use crate::async_reader::AsyncBackend;
9-
use crate::error::Error;
9+
use crate::error::PmtError;
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, Error> {
16+
pub async fn try_from<P: AsRef<Path>>(p: P) -> Result<Self, PmtError> {
1717
Ok(Self {
1818
file: AsyncMmapFile::open_with_options(p, AsyncOptions::new().read(true))
1919
.await
20-
.map_err(|_| Error::UnableToOpenMmapFile)?,
20+
.map_err(|_| PmtError::UnableToOpenMmapFile)?,
2121
})
2222
}
2323
}
2424

25-
impl From<fmmap::error::Error> for Error {
25+
impl From<fmmap::error::Error> for PmtError {
2626
fn from(_: fmmap::error::Error) -> Self {
2727
Self::Reading(io::Error::from(io::ErrorKind::UnexpectedEof))
2828
}
2929
}
3030

3131
#[async_trait]
3232
impl AsyncBackend for MmapBackend {
33-
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, Error> {
33+
async fn read_exact(&self, offset: usize, length: usize) -> Result<Bytes, PmtError> {
3434
if self.file.len() >= offset + length {
3535
Ok(self.file.reader(offset)?.copy_to_bytes(length))
3636
} else {
37-
Err(Error::Reading(io::Error::from(
37+
Err(PmtError::Reading(io::Error::from(
3838
io::ErrorKind::UnexpectedEof,
3939
)))
4040
}
4141
}
4242

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

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

0 commit comments

Comments
 (0)