1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use crate::shard::GeoShard;
use serde::{Deserialize, Serialize};

/// Object indentifier
pub type ObjectIdentifier = u64;

/// Latitude/longitude coordinate pair
pub type LatLngCoord = [f64; 2];

/// Nearby object
#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
#[derive(Debug, Serialize, Clone, PartialEq)]
pub struct Neighbor {
    /// Distance in kilometers
    pub distance: f64,
    /// Object key
    pub key: String,
}

#[derive(Debug)]
pub enum GeoShardError {
    IndexAlreadyExists(String),
    IndexNotFound(String),
    GeohashError(geohash::GeohashError),
}

impl std::fmt::Display for GeoShardError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            GeoShardError::IndexAlreadyExists(index) => {
                write!(f, "Index '{}' already exists.", index)
            }
            GeoShardError::IndexNotFound(index) => write!(f, "Index '{}' not found.", index),
            GeoShardError::GeohashError(err) => write!(f, "Geohash error: {}", err),
        }
    }
}

impl std::error::Error for GeoShardError {}

/// Configuration settings for geoshard parameters
#[derive(Clone, Debug, Deserialize)]
pub struct GeoShardConfig {
    /// Determines the default geohash length for inserts
    pub insert_depth: Option<usize>,
    /// Determines the default geohash length for searches
    pub search_depth: Option<usize>,
    /// Specifies the default number of results returned in range queries
    pub default_count: Option<usize>,
    /// Toggles the default sorting behavior for query results
    pub default_sorted: Option<bool>,
}

impl Default for GeoShardConfig {
    fn default() -> Self {
        GeoShardConfig {
            insert_depth: Some(GeoShard::DEFAULT_DEPTH),
            search_depth: Some(GeoShard::DEFAULT_DEPTH),
            default_count: Some(GeoShard::DEFAULT_COUNT),
            default_sorted: Some(GeoShard::DEFAULT_SORTED),
        }
    }
}

pub type BatchOutput<A, E> = (Vec<(String, A)>, Vec<(String, E)>);