1#![cfg(feature = "alloc")]
7
8#[allow(deprecated)]
9mod impl_deprecated;
10mod impl_program;
11
12use crate::types::RuleVec;
13use rstm_core::Rule;
14use rstm_state::{RawState, State};
15
16#[derive(Clone, Debug, Default)]
21#[cfg_attr(
22 feature = "serde",
23 derive(serde::Serialize),
24 serde(rename_all = "camelCase")
25)]
26pub struct Program<Q = String, A = char>
27where
28 Q: RawState,
29{
30 pub(crate) initial_state: Option<State<Q>>,
31 pub(crate) rules: RuleVec<Q, A>,
32}
33
34impl<Q, S> Program<Q, S>
35where
36 Q: RawState,
37{
38 pub const fn new() -> Self {
40 Self {
41 initial_state: None,
42 rules: RuleVec::new(),
43 }
44 }
45 pub fn from_rules<I>(iter: I) -> Self
47 where
48 I: IntoIterator<Item = Rule<Q, S>>,
49 {
50 Self {
51 initial_state: None,
52 rules: RuleVec::from_iter(iter),
53 }
54 }
55 pub fn from_state(initial_state: Q) -> Self {
57 Self {
58 initial_state: Some(State(initial_state)),
59 rules: RuleVec::new(),
60 }
61 }
62 pub fn initial_state(&self) -> Option<State<&'_ Q>> {
64 self.initial_state.as_ref().map(|state| state.view())
65 }
66 pub const fn rules(&self) -> &RuleVec<Q, S> {
68 &self.rules
69 }
70 pub const fn rules_mut(&mut self) -> &mut RuleVec<Q, S> {
72 &mut self.rules
73 }
74 pub fn with_default_state(self, state: Q) -> Self {
76 Self {
77 initial_state: Some(State(state)),
78 ..self
79 }
80 }
81 pub fn with_rules<I>(self, rules: I) -> Self
83 where
84 I: IntoIterator<Item = Rule<Q, S>>,
85 {
86 Self {
87 rules: Vec::from_iter(rules),
88 ..self
89 }
90 }
91 pub fn iter(&self) -> core::slice::Iter<'_, Rule<Q, S>> {
93 self.rules().iter()
94 }
95 pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Rule<Q, S>> {
97 self.rules_mut().iter_mut()
98 }
99}