h3ron_polars/algorithm/chunkedarray/valid.rs
1use crate::{IndexChunked, IndexValue};
2use polars_core::prelude::BooleanChunked;
3
4pub trait H3IsValid {
5 ///
6 /// # Example
7 ///
8 /// ```
9 /// use polars::prelude::UInt64Chunked;
10 /// use polars_core::prelude::TakeRandom;
11 /// use h3ron::{H3Cell, Index};
12 /// use h3ron_polars::algorithm::chunkedarray::H3IsValid;
13 /// use h3ron_polars::AsH3CellChunked;
14 ///
15 /// let cell = H3Cell::from_coordinate((4.5, 1.3).into(), 6).unwrap();
16 /// let ca = UInt64Chunked::from_iter([
17 /// Some(cell.h3index()),
18 /// Some(55), // invalid
19 /// None,
20 /// ]);
21 ///
22 /// let is_valid_ca = ca.h3cell().h3_is_valid();
23 /// assert_eq!(is_valid_ca.len(), ca.len());
24 ///
25 /// assert_eq!(is_valid_ca.get(0), Some(true));
26 /// assert_eq!(is_valid_ca.get(1), Some(false));
27 /// assert_eq!(is_valid_ca.get(2), None);
28 /// ```
29 fn h3_is_valid(&self) -> BooleanChunked;
30
31 /// Returns true when all contained h3indexes are valid.
32 fn h3_all_valid(&self) -> bool;
33}
34
35impl<'a, IX: IndexValue> H3IsValid for IndexChunked<'a, IX> {
36 fn h3_is_valid(&self) -> BooleanChunked {
37 BooleanChunked::from_iter(
38 self.iter_indexes_validated()
39 .map(|v| v.map(|index| index.is_ok())),
40 )
41 }
42
43 fn h3_all_valid(&self) -> bool {
44 self.iter_indexes_validated()
45 .all(|v| matches!(v, Some(Ok(_))))
46 }
47}