#![cfg(feature = "alloc")]
#[allow(deprecated)]
mod impl_deprecated;
mod impl_program;
use crate::types::RuleVec;
use rstm_core::Rule;
use rstm_state::{RawState, State};
#[derive(Clone, Debug, Default)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize),
serde(rename_all = "camelCase")
)]
pub struct Program<Q = String, A = char>
where
Q: RawState,
{
pub(crate) initial_state: Option<State<Q>>,
pub(crate) rules: RuleVec<Q, A>,
}
impl<Q, S> Program<Q, S>
where
Q: RawState,
{
pub const fn new() -> Self {
Self {
initial_state: None,
rules: RuleVec::new(),
}
}
pub fn from_rules<I>(iter: I) -> Self
where
I: IntoIterator<Item = Rule<Q, S>>,
{
Self {
initial_state: None,
rules: RuleVec::from_iter(iter),
}
}
pub fn from_state(initial_state: Q) -> Self {
Self {
initial_state: Some(State(initial_state)),
rules: RuleVec::new(),
}
}
pub fn initial_state(&self) -> Option<State<&'_ Q>> {
self.initial_state.as_ref().map(|state| state.view())
}
pub const fn rules(&self) -> &RuleVec<Q, S> {
&self.rules
}
pub const fn rules_mut(&mut self) -> &mut RuleVec<Q, S> {
&mut self.rules
}
pub fn with_default_state(self, state: Q) -> Self {
Self {
initial_state: Some(State(state)),
..self
}
}
pub fn with_rules<I>(self, rules: I) -> Self
where
I: IntoIterator<Item = Rule<Q, S>>,
{
Self {
rules: Vec::from_iter(rules),
..self
}
}
pub fn iter(&self) -> core::slice::Iter<'_, Rule<Q, S>> {
self.rules().iter()
}
pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Rule<Q, S>> {
self.rules_mut().iter_mut()
}
}