Skip to content

Commit

Permalink
(feat) remove deprecated failure as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Aug 26, 2020
1 parent aefe0db commit e3997b8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ edition = "2018"

[dependencies]
geo-types = "0.6.0"
failure = "0.1.2"

[dev-dependencies]
num-traits = "0.2"
18 changes: 8 additions & 10 deletions src/core.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::neighbors::Direction;
use crate::{Coordinate, GeohashError, Neighbors, Rect};

use failure::Error;

static BASE32_CODES: &[char] = &[
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k',
'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
Expand Down Expand Up @@ -31,7 +29,7 @@ static BASE32_CODES: &[char] = &[
///
/// assert_eq!(geohash_string, "9q60y60rhs");
/// ```
pub fn encode(c: Coordinate<f64>, len: usize) -> Result<String, Error> {
pub fn encode(c: Coordinate<f64>, len: usize) -> Result<String, GeohashError> {
let mut out = String::with_capacity(len);

let mut bits_total: i8 = 0;
Expand All @@ -42,7 +40,7 @@ pub fn encode(c: Coordinate<f64>, len: usize) -> Result<String, Error> {
let mut min_lon = -180f64;

if c.x < min_lon || c.x > max_lon || c.y < min_lat || c.y > max_lat {
bail!(GeohashError::InvalidCoordinateRange { c });
return Err(GeohashError::InvalidCoordinateRange(c));
}

while out.len() < len {
Expand Down Expand Up @@ -87,7 +85,7 @@ pub fn encode(c: Coordinate<f64>, len: usize) -> Result<String, Error> {
/// * max_lat
/// * min_lon
/// * max_lon
pub fn decode_bbox(hash_str: &str) -> Result<Rect<f64>, Error> {
pub fn decode_bbox(hash_str: &str) -> Result<Rect<f64>, GeohashError> {
let mut is_lon = true;
let mut max_lat = 90f64;
let mut min_lat = -90f64;
Expand Down Expand Up @@ -134,7 +132,7 @@ pub fn decode_bbox(hash_str: &str) -> Result<Rect<f64>, Error> {
))
}

fn hash_value_of_char(c: char) -> Result<usize, Error> {
fn hash_value_of_char(c: char) -> Result<usize, GeohashError> {
let ord = c as usize;
if 48 <= ord && ord <= 57 {
return Ok(ord - 48);
Expand All @@ -147,7 +145,7 @@ fn hash_value_of_char(c: char) -> Result<usize, Error> {
} else if 112 <= ord && ord <= 122 {
return Ok(ord - 91);
}
Err(GeohashError::InvalidHashCharacter { character: c }.into())
Err(GeohashError::InvalidHashCharacter(c))
}

/// Decode a geohash into a coordinate with some longitude/latitude error. The
Expand Down Expand Up @@ -194,7 +192,7 @@ fn hash_value_of_char(c: char) -> Result<usize, Error> {
/// ),
/// );
/// ```
pub fn decode(hash_str: &str) -> Result<(Coordinate<f64>, f64, f64), Error> {
pub fn decode(hash_str: &str) -> Result<(Coordinate<f64>, f64, f64), GeohashError> {
let rect = decode_bbox(hash_str)?;
let c0 = rect.min();
let c1 = rect.max();
Expand All @@ -209,7 +207,7 @@ pub fn decode(hash_str: &str) -> Result<(Coordinate<f64>, f64, f64), Error> {
}

/// Find neighboring geohashes for the given geohash and direction.
pub fn neighbor(hash_str: &str, direction: Direction) -> Result<String, Error> {
pub fn neighbor(hash_str: &str, direction: Direction) -> Result<String, GeohashError> {
let (coord, lon_err, lat_err) = decode(hash_str)?;
let (dlat, dlng) = direction.to_tuple();
let neighbor_coord = Coordinate {
Expand Down Expand Up @@ -242,7 +240,7 @@ pub fn neighbor(hash_str: &str, direction: Direction) -> Result<String, Error> {
/// }
/// );
/// ```
pub fn neighbors(hash_str: &str) -> Result<Neighbors, Error> {
pub fn neighbors(hash_str: &str) -> Result<Neighbors, GeohashError> {
Ok(Neighbors {
sw: neighbor(hash_str, Direction::SW)?,
s: neighbor(hash_str, Direction::S)?,
Expand Down
28 changes: 23 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,27 @@
use std::error::Error;
use std::fmt;

use crate::Coordinate;

#[derive(Debug, Fail)]
#[derive(Debug)]
pub enum GeohashError {
#[fail(display = "invalid hash character: {}", character)]
InvalidHashCharacter { character: char },
#[fail(display = "invalid coordinate range: {:?}", c)]
InvalidCoordinateRange { c: Coordinate<f64> },
InvalidHashCharacter(char),
InvalidCoordinateRange(Coordinate<f64>),
}

impl fmt::Display for GeohashError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GeohashError::InvalidHashCharacter(c) => write!(f, "invalid hash character: {}", c),
GeohashError::InvalidCoordinateRange(c) => {
write!(f, "invalid coordinate range: {:?}", c)
}
}
}
}

impl Error for GeohashError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
None
}
}
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
extern crate geo_types;
#[cfg(test)]
extern crate num_traits;
#[macro_use]
extern crate failure;

mod core;
mod error;
Expand Down

0 comments on commit e3997b8

Please sign in to comment.