rstm_programs/
program.rs

1/*
2    Appellation: program <module>
3    Created At: 2025.09.05:04:59:21
4    Contrib: @FL03
5*/
6#![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/// The [`Program`] implementation is designed to provide a structured representation of a set
17/// of rules and an optional initial state for a Turing machine or similar computational model.
18/// It encapsulates the rules that dictate the machine's behavior and the starting point for
19/// its execution.
20#[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    /// returns a new, empty instance of the [`Program`]
39    pub const fn new() -> Self {
40        Self {
41            initial_state: None,
42            rules: RuleVec::new(),
43        }
44    }
45    /// returns a new instance of the [`Program`] using the given rules
46    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    /// Create a new instance of the [Program] using the given initial state.
56    pub fn from_state(initial_state: Q) -> Self {
57        Self {
58            initial_state: Some(State(initial_state)),
59            rules: RuleVec::new(),
60        }
61    }
62    /// Returns an owned reference to the initial state of the program.
63    pub fn initial_state(&self) -> Option<State<&'_ Q>> {
64        self.initial_state.as_ref().map(|state| state.view())
65    }
66    /// Returns a reference to the instructions.
67    pub const fn rules(&self) -> &RuleVec<Q, S> {
68        &self.rules
69    }
70    /// Returns a mutable reference to the instructions.
71    pub const fn rules_mut(&mut self) -> &mut RuleVec<Q, S> {
72        &mut self.rules
73    }
74    /// consumes the current instance to create another with the given default state
75    pub fn with_default_state(self, state: Q) -> Self {
76        Self {
77            initial_state: Some(State(state)),
78            ..self
79        }
80    }
81    /// consumes the current instance to create another with the given rules
82    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    /// Returns an iterator over the elements.
92    pub fn iter(&self) -> core::slice::Iter<'_, Rule<Q, S>> {
93        self.rules().iter()
94    }
95    /// Returns a mutable iterator over the elements.
96    pub fn iter_mut(&mut self) -> core::slice::IterMut<'_, Rule<Q, S>> {
97        self.rules_mut().iter_mut()
98    }
99}