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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
extern crate nalgebra as na;

use std::vec::Vec;
use std::string::String;

///Trait for converting from rust types to strings compatible with openscad
pub trait ScadType 
{
    fn get_code(&self) -> String;
}

impl ScadType for na::Vector3<f32>
{
    fn get_code(&self) -> String 
    {
        String::from("[") + &self.x.get_code() + "," + &self.y.get_code() + "," + &self.z.get_code() + "]"
    }
}
impl ScadType for na::Vector2<f32>
{
    fn get_code(&self) -> String
    {
        String::from("[") + &self.x.get_code() + "," + &self.y.get_code() + "]"
    }
}

impl ScadType for f32
{
    fn get_code(&self) -> String 
    {
        self.to_string()
    }
}
impl ScadType for i32
{
    fn get_code(&self) -> String 
    {
        self.to_string()
    }
}
impl ScadType for bool
{
    fn get_code(&self) -> String 
    {
        self.to_string()
    }
}

impl<T: ScadType> ScadType for Vec<T>
{
    fn get_code(&self) -> String 
    {
        let mut result = "[".to_string();

        for elem in self
        {
            result = result + &elem.get_code() + ",";
        }
        
        result = result + "]";

        result
    }
}

impl ScadType for String
{
    fn get_code(&self) -> String
    {
        self.clone()
    }
}

#[cfg(test)]
mod type_tests
{
    extern crate nalgebra as na;
    use scad_type::*;

    #[test]
    fn type_test()
    {
        //No more tests needed for now. I assume the to_string() function works
        //as expected
        assert_eq!(na::Vector3::new(0.0, 0.0, 0.0).get_code(), "[0,0,0]");
        assert_eq!(na::Vector3::new(-5.0, 0.0, 0.0).get_code(), "[-5,0,0]");
        assert_eq!(na::Vector3::new(1.0,2.0,3.0).get_code(), "[1,2,3]");

        assert_eq!(na::Vector2::new(1.0, 3.3).get_code(), "[1,3.3]");

        assert_eq!(vec!(1,2,3,4,5,6).get_code(), "[1,2,3,4,5,6,]");
    }
}