ucglib/io/
mod.rs

1// Copyright 2019 Jeremy Wall
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use std::convert::Into;
15
16/// Accumulates lines and tells you when a ucg Statement has been read.
17pub struct StatementAccumulator {
18    acc: Vec<String>,
19}
20
21impl StatementAccumulator {
22    /// Constructs a StatementAccumulator ready for use.
23    pub fn new() -> Self {
24        Self { acc: Vec::new() }
25    }
26
27    pub fn reset(&mut self) {
28        self.acc = Vec::new();
29    }
30
31    pub fn next_line(&self) -> usize {
32        self.acc.len() + 1
33    }
34
35    pub fn last_line(&self) -> Option<&String> {
36        self.acc.last()
37    }
38
39    /// Tells you if the latest line ends in the statement terminator.
40    ///
41    /// Returns None if it wasn't a terminated statement and leaves the
42    /// accumulated lines alone.
43    ///
44    /// Returns Some(String) with the terminated statement if it was a
45    /// terminated statement and drains the accumulated statements out.
46    pub fn get_statement(&mut self) -> Option<String> {
47        if let Some(l) = self.acc.last() {
48            if l.trim_end().ends_with(";") {
49                let mut stmt = self.acc.drain(0..).fold(String::new(), |mut acc, s| {
50                    acc.push_str(&s);
51                    acc.push_str("\n");
52                    acc
53                });
54                stmt.shrink_to_fit();
55                return Some(stmt);
56            }
57        }
58        None
59    }
60
61    /// Pushes a line into the Statement Accumulator. Assumes that the
62    /// new line has already been trimmed. get_statement will reintroduce
63    /// the new lines.
64    pub fn push<S: Into<String>>(&mut self, line: S) {
65        self.acc.push(line.into());
66    }
67}
68
69#[cfg(test)]
70mod test;