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
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::{Error, FromH3Index, H3Direction};
use h3ron_h3_sys::H3Index;
use std::ffi::CString;
pub trait Index: Sized + PartialEq + FromH3Index {
fn h3index(&self) -> H3Index;
fn new(h3index: H3Index) -> Self;
fn validate(&self) -> Result<(), Error>;
fn resolution(&self) -> u8 {
(unsafe { h3ron_h3_sys::getResolution(self.h3index()) }) as u8
}
fn is_valid(&self) -> bool {
self.validate().is_ok()
}
fn direction(&self) -> H3Direction {
H3Direction::direction(self).unwrap()
}
fn direction_checked(&self) -> Result<H3Direction, Error> {
H3Direction::direction(self)
}
fn direction_to_parent_resolution(&self, target_resolution: u8) -> Result<H3Direction, Error> {
H3Direction::direction_to_parent_resolution(self, target_resolution)
}
}
pub trait HasH3Resolution {
fn h3_resolution(&self) -> u8;
}
impl<IX> HasH3Resolution for IX
where
IX: Index,
{
fn h3_resolution(&self) -> u8 {
self.resolution()
}
}
pub(crate) fn index_from_str<IX: Index>(s: &str) -> Result<IX, Error> {
let cs = CString::new(s).map_err(|_| Error::Failed)?;
let mut h3index: H3Index = 0;
Error::check_returncode(unsafe { h3ron_h3_sys::stringToH3(cs.as_ptr(), &mut h3index) })?;
let index = IX::new(h3index);
index.validate()?;
Ok(index)
}