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
#![deny(unsafe_code, warnings, clippy::all)]

use crate::{Vector3, Vector3Coordinate};
use thiserror::Error;

impl<T: Vector3Coordinate> From<(T, T, T)> for Vector3<T> {
    fn from(value: (T, T, T)) -> Self {
        Vector3 {
            x: value.0,
            y: value.1,
            z: value.2,
        }
    }
}

impl<T: Vector3Coordinate> From<Vector3<T>> for (T, T, T) {
    fn from(value: Vector3<T>) -> Self {
        (value.x, value.y, value.z)
    }
}

impl<T: Vector3Coordinate> From<[T; 3]> for Vector3<T> {
    fn from(value: [T; 3]) -> Self {
        Vector3 {
            x: value[0],
            y: value[1],
            z: value[2],
        }
    }
}

impl<T: Vector3Coordinate> From<Vector3<T>> for [T; 3] {
    fn from(value: Vector3<T>) -> Self {
        [value.x, value.y, value.z]
    }
}

#[derive(Error, Debug)]
pub enum ParseVector3Error {
    #[error("failed to parse numbers")]
    ParseNumberError(#[from] std::num::ParseFloatError),
    #[error("invalid format")]
    InvalidFormat,
    #[error("invalid Vec<Number>")]
    InvalidVec,
}

impl TryFrom<&str> for Vector3<f64> {
    type Error = ParseVector3Error;
    fn try_from(value: &str) -> Result<Self, Self::Error> {
        if value.len() < 14 {
            return Err(ParseVector3Error::InvalidFormat);
        }

        if &value[..8] != "Vector3(" || !value.ends_with(')') {
            return Err(ParseVector3Error::InvalidFormat);
        }

        let data = &value[8..value.len() - 1];
        let mut new_vector: [f64; 3] = [0.0, 0.0, 0.0];
        for (index, coord) in data.split(',').enumerate() {
            new_vector[index] = coord.trim().parse::<f64>()?;
        }

        Ok(Vector3::from(new_vector))
    }
}

impl<T: Vector3Coordinate> TryFrom<Vec<T>> for Vector3<T> {
    type Error = ParseVector3Error;
    fn try_from(value: Vec<T>) -> Result<Self, Self::Error> {
        let x = value.first().ok_or(ParseVector3Error::InvalidVec)?;
        let y = value.get(1).ok_or(ParseVector3Error::InvalidVec)?;
        let z = value.get(2).ok_or(ParseVector3Error::InvalidVec)?;

        Ok(Vector3::new(*x, *y, *z))
    }
}