Skip to content

Commit

Permalink
feat: no bail on first unexpected repl char
Browse files Browse the repository at this point in the history
Co-authored-by: kurogeek <[email protected]>
Co-authored-by: furioncycle <[email protected]>
  • Loading branch information
3 people committed Jul 27, 2024
1 parent f319570 commit d20994d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
59 changes: 49 additions & 10 deletions src/app/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,20 +130,26 @@ impl State {
let output = match &mut session_live.expecting {
ReplSessionExpecting::ClearlineBeforeInitialPrompt { cl_progress } => {
use ClearLineProgressStatus::*;
match cl_progress.clone().character(ch)? {
match cl_progress.clone().character(ch) {
InProgress(progress) => {
*cl_progress = progress;
vec![]
}
ReachedEnd => self.next_query(&id)?,
UnexpectedCharacter => {
let mut line = cl_progress.progress().to_owned();
line.push(ch);
session_live.expecting = ReplSessionExpecting::UnexpectedLine { line };
vec![]
}
}
}
ReplSessionExpecting::ClearLineBeforeResult {
cl_progress,
expected_result,
} => {
use ClearLineProgressStatus::*;
match cl_progress.clone().character(ch)? {
match cl_progress.clone().character(ch) {
InProgress(progress) => {
*cl_progress = progress;
}
Expand All @@ -154,6 +160,11 @@ impl State {
expected_result: expected_result.clone(),
};
}
UnexpectedCharacter => {
let mut line = cl_progress.progress().to_owned();
line.push(ch);
session_live.expecting = ReplSessionExpecting::UnexpectedLine { line };
}
};
vec![]
}
Expand Down Expand Up @@ -190,6 +201,15 @@ impl State {

self.next_query(&id)?
}
ReplSessionExpecting::UnexpectedLine { line } => {
line.push(ch);

if ch == '\n' {
bail!("{id}: unexepcted line: {line}");
}

vec![]
}
};

Ok(output)
Expand Down Expand Up @@ -342,28 +362,47 @@ pub(crate) enum ExampleState {
const CLEAR_LINE: &str = "\r\u{1b}[K";

#[derive(Debug, Clone)]
pub struct ClearLineProgress(std::iter::Peekable<std::str::Chars<'static>>);
pub struct ClearLineProgress(std::iter::Peekable<std::iter::Enumerate<std::str::Chars<'static>>>);

impl ClearLineProgress {
fn character(mut self, ch: char) -> anyhow::Result<ClearLineProgressStatus> {
let expected = self.0.next().unwrap();
fn character(mut self, ch: char) -> ClearLineProgressStatus {
let (_i, expected) = self.0.next().unwrap();
if ch != expected {
bail!("expected {expected:?}, got {ch:?}")
}
Ok(if self.0.peek().is_none() {
ClearLineProgressStatus::UnexpectedCharacter
} else if self.0.peek().is_none() {
ClearLineProgressStatus::ReachedEnd
} else {
ClearLineProgressStatus::InProgress(self)
})
}
}

fn new() -> Self {
Self(CLEAR_LINE.chars().peekable())
Self(CLEAR_LINE.chars().enumerate().peekable())
}

fn progress(&mut self) -> &'static str {
let &(i, _) = self.0.peek().unwrap();
&CLEAR_LINE[..i]
}
}

#[derive(Debug, Clone)]
enum ClearLineProgressStatus {
InProgress(ClearLineProgress),
ReachedEnd,
UnexpectedCharacter,
}

#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;

use super::ClearLineProgress;

#[test]
fn clear_line_progress() {
let mut cl_progress = ClearLineProgress::new();
let progress = cl_progress.progress();
assert_eq!(progress, "");
}
}
3 changes: 3 additions & 0 deletions src/app/state/repl_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ pub(crate) enum ReplSessionExpecting {
acc: String,
expected_result: ExpectedResult,
},
UnexpectedLine {
line: String,
},
}

impl ReplSessionLive {
Expand Down

0 comments on commit d20994d

Please sign in to comment.