thrust/
lib.rs

1#[macro_use]
2extern crate nom;
3
4pub use generator::Generator;
5use nom::{IResult};
6use std::str;
7use std::convert;
8use std::string;
9
10mod parser;
11mod ast;
12mod generator;
13
14pub type ThriftResult<T> = Result<T, ThriftCompilerError>;
15
16#[derive(Debug)]
17pub enum ThriftCompilerError {
18    Parsing,
19    NoNamespace,
20    Nom,
21    Unknown,
22    ToUtf8
23}
24
25impl convert::From<string::FromUtf8Error> for ThriftCompilerError {
26    fn from(err: string::FromUtf8Error) -> ThriftCompilerError {
27        ThriftCompilerError::ToUtf8
28    }
29}
30
31#[derive(Debug)]
32pub struct ThriftCompiler {
33    pub namespace: String,
34    pub buffer: String
35}
36
37impl ThriftCompiler {
38    pub fn run(input: &[u8]) -> ThriftResult<ThriftCompiler> {
39        match parser::parse_thrift(input) {
40            IResult::Done(i, nodes) => {
41                let mut buf = Vec::new();
42                let mut ns = None;
43
44                for node in nodes.iter() {
45                    if node.is_namespace() {
46                        ns = node.namespace();
47                    } else {
48                        node.gen(&mut buf);
49                    }
50                }
51
52                let ns = match ns {
53                    Some(ns) => ns,
54                    None => return Err(ThriftCompilerError::NoNamespace)
55                };
56
57                return Ok(ThriftCompiler {
58                    namespace: ns,
59                    buffer: try!(String::from_utf8(buf))
60                });
61            },
62            IResult::Error(err) => {
63                return Err(ThriftCompilerError::Nom);
64            },
65            IResult::Incomplete(n) => {
66                println!("{:?}", n);
67                return Err(ThriftCompilerError::Unknown);
68            }
69        }
70    }
71}