use super::value_types::ValueTypes;
use crate::error::{FliError, Result};
#[derive(Debug, Clone)]
pub enum ParseState {
Start,
InCommand,
InOption,
AcceptingValue(String, ValueTypes),
Breaking,
InArgument,
End,
}
impl ParseState {
pub fn set_next_mode(&mut self, next: ParseState) -> Result<&mut Self> {
if self.can_go_to_next(&next) {
*self = next;
Ok(self)
} else {
Err(FliError::InvalidStateTransition {
from: format!("{:?}", self),
to: format!("{:?}", next),
})
}
}
pub fn can_go_to_next(&self, next: &ParseState) -> bool {
match next {
ParseState::Start => false,
ParseState::InCommand => matches!(self, ParseState::Start | ParseState::InCommand),
ParseState::InOption => matches!(
self,
ParseState::Start
| ParseState::InCommand
| ParseState::InArgument
| ParseState::InOption
| ParseState::AcceptingValue(_, _)
),
ParseState::AcceptingValue(_, _) => matches!(self, ParseState::InOption),
ParseState::InArgument => matches!(
self,
ParseState::Start
| ParseState::InCommand
| ParseState::Breaking
| ParseState::InArgument
),
ParseState::Breaking => matches!(
self,
ParseState::InOption | ParseState::AcceptingValue(_, _)
),
ParseState::End => !matches!(self, ParseState::Start),
}
}
}