sequent_repl/commands/
jump.rs1use crate::Context;
4use sequent::SimulationError;
5use revolver::command::{
6 ApplyCommandError, ApplyOutcome, Command, Description, Example, NamedCommandParser,
7 ParseCommandError,
8};
9use revolver::looper::Looper;
10use revolver::terminal::{Terminal};
11use std::borrow::Cow;
12use std::marker::PhantomData;
13
14pub struct Jump<S, C> {
19 location: usize,
20 __phantom_data: PhantomData<(S, C)>
21}
22
23impl<S, C> Jump<S, C> {
24 fn new(location: usize) -> Self {
25 Self {
26 location,
27 __phantom_data: PhantomData::default()
28 }
29 }
30}
31
32impl<S: Clone, C: Context<State = S>, T: Terminal> Command<T> for Jump<S, C> {
33 type Context = C;
34 type Error = SimulationError<S>;
35
36 fn apply(
37 &mut self,
38 looper: &mut Looper<C, SimulationError<S>, T>,
39 ) -> Result<ApplyOutcome, ApplyCommandError<SimulationError<S>>> {
40 let (terminal, _, context) = looper.split();
41 context.sim().jump(self.location).map_err(ApplyCommandError::Application)?;
42 context.print_state(terminal)?;
43 Ok(ApplyOutcome::Applied)
44 }
45}
46
47pub struct Parser<S, C> {
49 __phantom_data: PhantomData<(S, C)>
50}
51
52impl<S, C> Default for Parser<S, C> {
53 fn default() -> Self {
54 Self {
55 __phantom_data: PhantomData::default()
56 }
57 }
58}
59
60impl<S: Clone + 'static, C: Context<State = S> + 'static, T: Terminal> NamedCommandParser<T> for Parser<S, C> {
61 type Context = C;
62 type Error = SimulationError<S>;
63
64 fn parse(
65 &self,
66 s: &str,
67 ) -> Result<Box<dyn Command<T, Context = C, Error = SimulationError<S>>>, ParseCommandError> {
68 if s.is_empty() {
69 return Err(ParseCommandError(
70 "empty arguments to 'jump'".into(),
71 ));
72 }
73 let location = s.parse().map_err(ParseCommandError::convert)?;
74 Ok(Box::new(Jump::new(location)))
75 }
76
77 fn shorthand(&self) -> Option<Cow<'static, str>> {
78 Some("j".into())
79 }
80
81 fn name(&self) -> Cow<'static, str> {
82 "jump".into()
83 }
84
85 fn description(&self) -> Description {
86 Description {
87 purpose: "Jumps to a specified cursor location in the timeline.".into(),
88 usage: "<location>".into(),
89 examples: vec![Example {
90 scenario: "jump to location 2".into(),
91 command: "2".into(),
92 }],
93 }
94 }
95}
96
97#[cfg(test)]
98mod tests;