shelp/repl/
iter.rs

1use super::Repl;
2use crate::LangInterface;
3
4/// A wrapper over [`Repl`] which allows it to be used as a `Iterator`.
5///
6/// Although using an iterator is easier, errors are discarded and [`None`] is returned. For this
7/// reason, it may be beneficial to use the [`Repl`] directly.
8pub struct ReplIter<L: LangInterface> {
9    repl: Repl<L>,
10    color: crate::Color,
11}
12
13impl<L: LangInterface> ReplIter<L> {
14    /// Create a iterator for a [Repl]
15    pub fn new(repl: Repl<L>, color: crate::Color) -> Self {
16        Self { repl, color }
17    }
18
19    /// Sets the exit keyword. If you don't want any exit keyword, set it to an empty string
20    pub fn set_exit_keyword(&mut self, exit_keyword: &'static str) {
21        self.repl.set_exit_keyword(exit_keyword)
22    }
23
24    /// Sets the clear keyword. If you don't want any clear keyword, set it to an empty string
25    pub fn set_clear_keyword(&mut self, clear_keyword: &'static str) {
26        self.repl.set_clear_keyword(clear_keyword)
27    }
28}
29
30impl<L: LangInterface> Repl<L> {
31    /// Shorthand to get iterator from self
32    pub fn iter(self, color: crate::Color) -> ReplIter<L> {
33        ReplIter::new(self, color)
34    }
35}
36
37impl<L: LangInterface> Iterator for ReplIter<L> {
38    type Item = String;
39
40    fn next(&mut self) -> Option<Self::Item> {
41        self.repl.next(self.color).ok()
42    }
43}