Skip to main content

utiles_core/
traits.rs

1//! Utiles traits
2
3use std::hash::Hash;
4
5use crate::TileLike;
6use crate::parent::Parents;
7
8/// `IsOk` trait for checking if a value is Ok and returns a result
9/// of self or an error
10pub trait IsOk: Sized {
11    /// Returns `Ok` if the value is `Ok` or an error
12    ///
13    /// # Errors
14    ///
15    /// Returns an error if the value is not `Ok`
16    fn ok(&self) -> Result<Self, crate::UtilesCoreError>;
17}
18
19/// `Coord2d` like trait for 2D coordinates has x/y getters
20pub trait Coord2dLike {
21    /// Returns the x value
22    fn x(&self) -> f64;
23
24    /// Returns the y value
25    fn y(&self) -> f64;
26}
27
28/// `LngLat` like trait
29pub trait LngLatLike: Coord2dLike {
30    /// Returns the longitude value
31    fn lng(&self) -> f64;
32
33    /// Returns the longitude value
34    fn lon(&self) -> f64 {
35        self.lng()
36    }
37
38    /// Returns the longitude value
39    fn longitude(&self) -> f64 {
40        self.lat()
41    }
42
43    /// Returns the latitude value
44    fn lat(&self) -> f64;
45
46    /// Returns the latitude value
47    fn latitude(&self) -> f64 {
48        self.lat()
49    }
50}
51
52pub trait TileParent: Eq + Hash + Copy + TileLike {
53    fn parent(&self, zoom: Option<u8>) -> Option<Self>
54    where
55        Self: Sized;
56
57    fn iter_parents(&self) -> Parents<Self> {
58        Parents {
59            current: self.parent(None),
60        }
61    }
62
63    #[must_use]
64    fn root() -> Self
65    where
66        Self: Sized;
67}
68
69pub trait TileChildren1: Eq + Hash + Copy + TileLike {
70    /// Returns direct children in Z order:
71    ///     1) top-left
72    ///     2) top-right
73    ///     3) bottom-left
74    ///     4) bottom-right
75    #[must_use]
76    fn children1(&self) -> [Self; 4];
77}