archimedes_kernel/primitives/
mod.rs1use serde::{Deserialize, Serialize};
2
3use crate::movement::{Event, MovementMemory};
4
5#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
6pub struct Identity(pub String);
7
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct Boundary {
10 pub allowed_values: Vec<String>,
11}
12
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14pub struct Law {
15 pub allowed_transitions: Vec<(String, String)>,
16}
17
18impl Law {
19 pub fn check(&self, current_state: &State, event: &Event) -> bool {
20 self.allowed_transitions
21 .iter()
22 .any(|(from, to)| from == ¤t_state.field && to == &event.proposed_field)
23 }
24}
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27pub struct State {
28 pub field: String,
29}
30
31#[derive(Debug, Clone, Serialize, Deserialize)]
32pub struct Reality {
33 pub(crate) identity: Identity,
34 pub(crate) boundary: Boundary,
35 pub(crate) law: Law,
36 pub(crate) state: State,
37 pub(crate) initial_state: State,
38 pub(crate) memory: MovementMemory,
39 pub(crate) birth_boundary: Boundary,
40 pub(crate) birth_law: Law,
41 #[cfg(test)]
42 #[serde(skip)]
43 pub(crate) test_hook: Option<fn(&mut Reality)>,
44}
45
46impl Reality {
47 pub fn new(identity: Identity, boundary: Boundary, law: Law, state: State) -> Self {
48 Self {
49 identity,
50 boundary: boundary.clone(),
51 law: law.clone(),
52 initial_state: state.clone(),
53 state,
54 memory: MovementMemory::default(),
55 birth_boundary: boundary,
56 birth_law: law,
57 #[cfg(test)]
58 test_hook: None,
59 }
60 }
61
62 pub fn identity(&self) -> &Identity {
63 &self.identity
64 }
65 pub fn boundary(&self) -> &Boundary {
66 &self.boundary
67 }
68 pub fn law(&self) -> &Law {
69 &self.law
70 }
71 pub fn state(&self) -> &State {
72 &self.state
73 }
74 pub fn initial_state(&self) -> &State {
75 &self.initial_state
76 }
77 pub fn memory(&self) -> &MovementMemory {
78 &self.memory
79 }
80}
81
82impl PartialEq for Reality {
83 fn eq(&self, other: &Self) -> bool {
84 self.identity == other.identity
85 && self.boundary == other.boundary
86 && self.law == other.law
87 && self.state == other.state
88 && self.initial_state == other.initial_state
89 && self.memory == other.memory
90 && self.birth_boundary == other.birth_boundary
91 && self.birth_law == other.birth_law
92 }
94}
95
96impl Eq for Reality {}