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
#[cfg(test)]
extern crate byteorder;
#[macro_use]
extern crate error_chain;
#[macro_use]
extern crate nom;

mod parser;

error_chain! {
    errors {
        Nom(err: nom::ErrorKind) {
            description("Invalid STL content")
            display("Invalid STL content: {}", err)
        }
        TooLong {
            description("The STL contained extra content")
        }
        TooShort {
            description("The STL ended early")
        }
    }
}

/// An STL file.
#[derive(Clone, Debug, PartialEq)]
pub struct Stl {
    pub triangles: Vec<Triangle>
}

impl Stl {
    pub fn parse(bytes: &[u8]) -> Result<Stl> {
        use nom::IResult::*;
        match parser::parse(bytes) {
            Done(r, stl) => {
                assert_eq!(r.len(), 0);
                Ok(stl)
            },
            Incomplete(..) => Err(ErrorKind::TooShort.into()),
            Error(err) => Err(ErrorKind::Nom(err).into()),
        }
    }
}

#[derive(Clone, Debug, PartialEq)]
pub struct Triangle {
    pub norm: Vertex,
    pub vertices: [Vertex; 3],
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Vertex(f32, f32, f32);