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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use scad_object::*;
use std::vec::{Vec};
use std::string::{String};
use std::path::Path;
use std::fs::File;
use std::io::prelude::*;


/**
    Object that stores scad objects along with global parameters for
    the objects. Also has methods for writing the  data to files.
*/
pub struct ScadFile 
{
    objects: Vec<ScadObject>,

    detail: i32,
}

impl ScadFile 
{
    pub fn new() -> ScadFile 
    {
        ScadFile {
            objects: Vec::new(),

            detail: 0,
        }
    }

    /**
        Returns the code for the global parameters as well as all the 
        children in the file
    */
    pub fn get_code(&self) -> String 
    {
        let mut result = String::from("");

        if self.detail != 0
        {
            result = result + "$fn=" + &self.detail.to_string() + ";\n";
        }

        for object in &self.objects
        {
            result = result + &object.get_code() + "\n";
        }

        result
    }

    pub fn add_object(&mut self, object: ScadObject) 
    {
        self.objects.push(object);
    }

    /**
      Sets the $fn variable for the whole file. This varibale defines  the detail
      amount for cylindrical objects
     */
    pub fn set_detail(&mut self, detail: i32) 
    {
        self.detail = detail;
    }

    /**
      Writes the resulting code to a file

      ##Arguments
        
        path: The path to the file where we want to write relative to the current
        working directory.

      ##Returns
      The function will return false and print a message to the console if
      writing fails.
     */
    pub fn write_to_file(&self, path: String) -> bool
    {
        //Writing the result to file
        let path = Path::new(&path);

        // Open a file in write-only mode, returns `io::Result<File>`
        let mut file = match File::create(&path) {
            Err(_) => 
                {
                    println!("Couldn't open file for writing");
                    return false;
                },
            Ok(file) => file,
        };

        match file.write(self.get_code().as_bytes()) {
            Err(_) => 
                {
                    println!("Failed to write to output file");
                    return false;
                },
            Ok(_) => {}
        };

        return true;
    }
}



#[cfg(test)]
mod file_tests
{
    use scad_object::*;
    use scad_element::*;

    use scad_file::{ScadFile};


    use std::fs;
    use std::fs::File;
    use std::io::prelude::*;

    #[test]
    fn detail_test()
    {
        let mut sfile = ScadFile::new();

        sfile.detail = 30;

        assert_eq!(sfile.get_code(), "$fn=30;\n");

        let obj = ScadObject::new(ScadElement::Union);
        sfile.add_object(obj);

        let obj = ScadObject::new(ScadElement::Difference);
        sfile.add_object(obj);

        assert_eq!(sfile.get_code(), "$fn=30;\nunion();\ndifference();\n")
    }

    #[test]
    fn file_test()
    {
        let mut sfile = ScadFile::new();

        sfile.detail = 30;

        let write_success = sfile.write_to_file(String::from("test.scad"));
        
        let mut correct_content = false;
        //Read the content of the file
        match File::open("test.scad")
        {
            Ok(mut f) => 
            {
                let mut file_content = String::new();
                match f.read_to_string(&mut file_content)
                {
                    Ok(_) => {
                        if file_content == sfile.get_code()
                        {
                            correct_content = true;
                        }
                        else
                        {
                            println!("Expected {}, Found {}", file_content, sfile.get_code());
                        }
                    },
                    Err(_) => {println!("Failed to read content from output file");}
                };
            },
            Err(_) => {println!("Failed to open file for reading");}
        };

        //Remove the file we created
        match fs::remove_file("test.scad")
        {
            Ok(_) => {},
            Err(_) => {}
        };

        assert!(write_success);
        assert!(correct_content);
    }
}