use bytelines::*;
use std::io::{self, BufReader};
use crate::context::Context;
pub trait Lifecycle {
fn on_start(&mut self, _ctx: &mut Context) {}
fn on_entry(&mut self, _input: &[u8], _ctx: &mut Context) {}
fn on_end(&mut self, _ctx: &mut Context) {}
}
pub fn run_lifecycle<L>(mut lifecycle: L)
where
L: Lifecycle,
{
let stdin = io::stdin();
let stdin_lock = stdin.lock();
let mut ctx = Context::new();
lifecycle.on_start(&mut ctx);
let mut lines = BufReader::new(stdin_lock).byte_lines();
while let Some(Ok(input)) = lines.next() {
lifecycle.on_entry(input, &mut ctx);
}
lifecycle.on_end(&mut ctx);
}