golem_rib_repl/
rib_context.rs

1use crate::repl_state::ReplState;
2use crate::rib_edit::RibEdit;
3use crate::{RawRibScript, ReplPrinter};
4use rib::RibCompiler;
5use rustyline::history::DefaultHistory;
6use rustyline::Editor;
7use std::sync::RwLockReadGuard;
8
9// A projection of internal repl_state that could be useful
10// for advanced customisation of REPL commands.
11pub struct ReplContext<'a> {
12    printer: &'a dyn ReplPrinter,
13    repl_state: &'a ReplState,
14    editor: &'a mut Editor<RibEdit, DefaultHistory>,
15}
16
17impl<'a> ReplContext<'a> {
18    pub(crate) fn new(
19        printer: &'a dyn ReplPrinter,
20        repl_state: &'a ReplState,
21        editor: &'a mut Editor<RibEdit, DefaultHistory>,
22    ) -> Self {
23        Self {
24            printer,
25            repl_state,
26            editor,
27        }
28    }
29
30    pub fn get_printer(&self) -> &dyn ReplPrinter {
31        self.printer
32    }
33
34    pub fn clear_state(&self) {
35        self.repl_state.clear()
36    }
37
38    pub fn clear_history(&mut self) {
39        self.editor.clear_history().unwrap();
40    }
41
42    pub fn get_new_rib_script(&self, rib: &str) -> RawRibScript {
43        let rib_script = self.repl_state.rib_script();
44        let result = &*rib_script;
45        let mut result = result.clone();
46        result.push(rib);
47        result
48    }
49
50    pub fn get_rib_compiler(&self) -> RwLockReadGuard<RibCompiler> {
51        self.repl_state.rib_compiler()
52    }
53}