use std::default::Default;
#[cfg_attr(test, derive(Debug, PartialEq))]
#[derive(Default)]
pub(super) enum InnerState {
Removed,
#[default]
Original,
Replaced(Vec<String>),
}
pub struct ProcessLinesActions {
state: InnerState,
}
impl<'a> ProcessLinesActions {
pub(super) fn new() -> Self {
ProcessLinesActions {
state: InnerState::default(),
}
}
pub(super) fn take_lines(&mut self) -> InnerState {
std::mem::take(&mut self.state)
}
pub fn replace_with_lines(&mut self, new_lines: impl Iterator<Item = &'a str>) {
self.state = InnerState::Replaced(new_lines.map(|str| str.to_string()).collect());
}
pub fn remove_line(&mut self) {
self.state = InnerState::Removed;
}
}
#[cfg(test)]
mod test {
use super::InnerState;
use super::ProcessLinesActions;
#[test]
fn test_replace() {
let mut actions = ProcessLinesActions::new();
actions.replace_with_lines("ipsum".split('\n'));
assert_eq!(
actions.take_lines(),
InnerState::Replaced(vec!["ipsum".to_string()])
);
actions.replace_with_lines("lorem ipsum dolor".split(' '));
assert_eq!(
actions.take_lines(),
InnerState::Replaced(vec![
"lorem".to_string(),
"ipsum".to_string(),
"dolor".to_string()
])
);
assert_eq!(actions.take_lines(), InnerState::Original);
}
#[test]
fn test_remove() {
let mut actions = ProcessLinesActions::new();
actions.remove_line();
assert_eq!(actions.take_lines(), InnerState::Removed);
}
#[test]
fn test_no_actions() {
let mut actions = ProcessLinesActions::new();
assert_eq!(actions.take_lines(), InnerState::Original);
}
}