arr_rs/core/types/tuple/
tuple3.rs

1use std::fmt::Display;
2use std::str::FromStr;
3
4use crate::core::prelude::*;
5
6pub(crate) type TupleH3 <S, T, U> = (Array<S>, Array<T>, Array<U>);
7
8/// Tuple3 type for array
9#[derive(Debug, Copy, Clone, PartialOrd, PartialEq, Eq)]
10pub struct Tuple3<S: ArrayElement, T: ArrayElement, U: ArrayElement>(pub S, pub T, pub U);
11
12impl<S: ArrayElement + FromStr, T: ArrayElement + FromStr, U: ArrayElement + FromStr> FromStr for Tuple3<S, T, U>
13    where <S as FromStr>::Err: std::fmt::Debug,
14          <T as FromStr>::Err: std::fmt::Debug,
15          <U as FromStr>::Err: std::fmt::Debug, {
16    type Err = ParseTupleError;
17
18    fn from_str(s: &str) -> Result<Self, Self::Err> {
19        let s = s
20            .trim_start_matches('(')
21            .trim_end_matches(')')
22            .replace(", ", ",");
23        let mut parts = s.split(',');
24
25        let x = parts.next().ok_or(ParseTupleError::Format)?;
26        let y = parts.next().ok_or(ParseTupleError::Format)?;
27        let z = parts.next().ok_or(ParseTupleError::Format)?;
28
29        let x = S::from_str(x);
30        let y = T::from_str(y);
31        let z = U::from_str(z);
32
33        if x.is_err() || y.is_err() || z.is_err() {
34            return Err(ParseTupleError::Parse("error parsing tuple value"))
35        }
36
37        Ok(Self(x.unwrap(), y.unwrap(), z.unwrap()))
38    }
39}
40
41impl <S: ArrayElement, T: ArrayElement, U: ArrayElement> ArrayElement for Tuple3<S, T, U> {
42
43    fn zero() -> Self {
44        Self(S::zero(), T::zero(), U::zero())
45    }
46
47    fn one() -> Self {
48        Self(S::one(), T::one(), U::one())
49    }
50
51    fn is_nan(&self) -> bool {
52        self.0.is_nan() || self.1.is_nan() || self.2.is_nan()
53    }
54}
55
56impl <S: ArrayElement, T: ArrayElement, U: ArrayElement> Display for Tuple3<S, T, U> {
57
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        write!(f, "({}, {}, {})", self.0, self.1, self.2)
60    }
61}
62
63impl <S: ArrayElement, T: ArrayElement, U: ArrayElement> TupleElement<T> for Tuple3<S, T, U> {
64    type Input = (S, T, U);
65    type Output = Self;
66
67    fn from_tuple(tuple: (S, T, U)) -> Self::Output {
68        Self(tuple.0, tuple.1, tuple.2)
69    }
70}