stl_bin_parser/
lib.rs

1#[cfg(test)]
2extern crate byteorder;
3#[macro_use]
4extern crate error_chain;
5#[macro_use]
6extern crate nom;
7
8mod parser;
9
10error_chain! {
11    errors {
12        Nom(err: nom::ErrorKind) {
13            description("Invalid STL content")
14            display("Invalid STL content: {}", err)
15        }
16        TooLong {
17            description("The STL contained extra content")
18        }
19        TooShort {
20            description("The STL ended early")
21        }
22    }
23}
24
25/// An STL file.
26#[derive(Clone, Debug, PartialEq)]
27pub struct Stl {
28    pub triangles: Vec<Triangle>
29}
30
31impl Stl {
32    pub fn parse(bytes: &[u8]) -> Result<Stl> {
33        use nom::IResult::*;
34        match parser::parse(bytes) {
35            Done(r, stl) => {
36                assert_eq!(r.len(), 0);
37                Ok(stl)
38            },
39            Incomplete(..) => Err(ErrorKind::TooShort.into()),
40            Error(err) => Err(ErrorKind::Nom(err).into()),
41        }
42    }
43}
44
45#[derive(Clone, Debug, PartialEq)]
46pub struct Triangle {
47    pub norm: Vertex,
48    pub vertices: [Vertex; 3],
49}
50
51#[derive(Copy, Clone, Debug, PartialEq)]
52pub struct Vertex(f32, f32, f32);