syslog_ng_common/proxies/parser/
proxy.rs

1// Copyright (c) 2016 Tibor Benke <ihrwein@gmail.com>
2//
3// Licensed under the Apache License, Version 2.0
4// <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0> or the MIT
5// license <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. All files in the project carrying such notice may not be copied,
7// modified, or distributed except according to those terms.
8
9use LogMessage;
10use LogParser;
11
12pub use proxies::parser::{OptionError, Parser, ParserBuilder};
13
14#[repr(C)]
15pub struct ParserProxy<B>
16    where B: ParserBuilder<LogParser>
17{
18    pub parser: Option<B::Parser>,
19    pub builder: Option<B>,
20}
21
22impl<B> ParserProxy<B> where B: ParserBuilder<LogParser>
23{
24    pub fn new() -> ParserProxy<B> {
25        ParserProxy {
26            parser: None,
27            builder: Some(B::new()),
28        }
29    }
30
31    pub fn init(&mut self) -> bool {
32        let builder = self.builder.take().expect("Called init when builder was not set");
33        match builder.build() {
34            Ok(parser) => {
35                self.parser = Some(parser);
36                true
37            }
38            Err(error) => {
39                error!("Error: {:?}", error);
40                false
41            }
42        }
43    }
44
45    pub fn set_option(&mut self, name: String, value: String) {
46        if self.builder.is_none() {
47            self.builder = Some(B::new());
48        }
49
50        let builder = self.builder.as_mut().expect("Failed to get builder on a ParserProxy");
51        builder.option(name, value);
52    }
53
54    pub fn process(&mut self, parent: &mut LogParser, msg: &mut LogMessage, input: &str) -> bool {
55        self.parser
56            .as_mut()
57            .expect("Called process on a non-existing Rust parser")
58            .parse(parent, msg, input)
59    }
60}
61
62impl<B> Clone for ParserProxy<B> where B: ParserBuilder<LogParser> {
63    fn clone(&self) -> ParserProxy<B> {
64        // it makes no sense to clone() the builder
65        ParserProxy {parser: self.parser.clone(), builder: None}
66    }
67}