use std::convert::Into;
pub struct StatementAccumulator {
acc: Vec<String>,
}
impl StatementAccumulator {
pub fn new() -> Self {
Self { acc: Vec::new() }
}
pub fn reset(&mut self) {
self.acc = Vec::new();
}
pub fn next_line(&self) -> usize {
self.acc.len() + 1
}
pub fn last_line(&self) -> Option<&String> {
self.acc.last()
}
pub fn get_statement(&mut self) -> Option<String> {
if let Some(l) = self.acc.last() {
if l.trim_end().ends_with(";") {
let mut stmt = self.acc.drain(0..).fold(String::new(), |mut acc, s| {
acc.push_str(&s);
acc.push_str("\n");
acc
});
stmt.shrink_to_fit();
return Some(stmt);
}
}
None
}
pub fn push<S: Into<String>>(&mut self, line: S) {
self.acc.push(line.into());
}
}
#[cfg(test)]
mod test;