1use crate::{
2 path::Path, story::Story, story_error::StoryError, story_state::StoryState,
3 value_type::ValueType,
4};
5
6impl Story {
9 #[inline]
10 pub(crate) fn get_state(&self) -> &StoryState {
11 &self.state
12 }
13
14 #[inline]
15 pub(crate) fn get_state_mut(&mut self) -> &mut StoryState {
16 &mut self.state
17 }
18
19 pub(crate) fn reset_globals(&mut self) -> Result<(), StoryError> {
20 if self
21 .main_content_container
22 .named_content
23 .contains_key("global decl")
24 {
25 let original_pointer = self.get_state().get_current_pointer().clone();
26
27 self.choose_path(
28 &Path::new_with_components_string(Some("global decl")),
29 false,
30 )?;
31
32 self.continue_internal(0.0)?;
35
36 self.get_state().set_current_pointer(original_pointer);
37 }
38
39 self.get_state_mut()
40 .variables_state
41 .snapshot_default_globals();
42
43 Ok(())
44 }
45
46 pub fn set_variable(
49 &mut self,
50 variable_name: &str,
51 value_type: &ValueType,
52 ) -> Result<(), StoryError> {
53 let notify_observers = self
54 .get_state_mut()
55 .variables_state
56 .set(variable_name, value_type.clone())?;
57
58 if notify_observers {
59 self.notify_variable_changed(variable_name, value_type);
60 }
61
62 Ok(())
63 }
64
65 pub fn get_variable(&self, variable_name: &str) -> Option<ValueType> {
68 self.get_state().variables_state.get(variable_name)
69 }
70
71 pub(crate) fn restore_state_snapshot(&mut self) {
72 self.state_snapshot_at_last_new_line
78 .as_mut()
79 .unwrap()
80 .restore_after_patch(); self.state = self.state_snapshot_at_last_new_line.take().unwrap();
83
84 if !self.async_saving {
88 self.get_state_mut().apply_any_patch();
89 }
90 }
91
92 pub(crate) fn state_snapshot(&mut self) {
93 let mut tmp_state = self.state.copy_and_start_patching(false);
95 std::mem::swap(&mut tmp_state, &mut self.state);
96 self.state_snapshot_at_last_new_line = Some(tmp_state);
97 }
98
99 pub(crate) fn discard_snapshot(&mut self) {
100 if !self.async_saving {
107 self.get_state_mut().apply_any_patch();
108 }
109
110 self.state_snapshot_at_last_new_line = None;
112 }
113
114 pub fn save_state(&self) -> Result<String, StoryError> {
116 self.get_state().to_json()
117 }
118
119 pub fn load_state(&mut self, json_state: &str) -> Result<(), StoryError> {
121 self.get_state_mut().load_json(json_state)
122 }
123
124 pub fn reset_state(&mut self) -> Result<(), StoryError> {
126 self.if_async_we_cant("ResetState")?;
127
128 self.state = StoryState::new(
129 self.main_content_container.clone(),
130 self.list_definitions.clone(),
131 );
132
133 self.reset_globals()?;
134
135 Ok(())
136 }
137}