From 328a8d9c45115cfdeb60bb260a9d87c8e747af7e Mon Sep 17 00:00:00 2001 From: "Shahar \"Dawn\" Or" Date: Sun, 19 May 2024 20:35:31 +0700 Subject: [PATCH] refactor: no Option Co-authored-by: ruben beck --- src/app/state.rs | 14 ++++++-------- src/app/state/repl_state.rs | 7 ++++++- src/repl/example.rs | 12 +++++++----- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/app/state.rs b/src/app/state.rs index 06c7c0f..7fb53f2 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -152,13 +152,9 @@ impl State { if !acc.ends_with('\n') { vec![] } else if Self::sanitize(acc)? == expected.as_str() { - session_live.expecting = if let Some(expected_result) = expected_result { - ReplSessionExpecting::ResultAndNextPrompt { - acc: String::new(), - expected_result: expected_result.clone(), - } - } else { - ReplSessionExpecting::Prompt(String::new()) + session_live.expecting = ReplSessionExpecting::ResultAndNextPrompt { + acc: String::new(), + expected_result: expected_result.clone(), }; vec![] } else { @@ -173,10 +169,12 @@ impl State { let sanitized = Self::sanitize(acc)?; - let Some(result) = sanitized.strip_suffix("\n\nnix-repl> ") else { + let Some(result) = sanitized.strip_suffix("\nnix-repl> ") else { break 'arm vec![]; }; + let result = result.trim_end_matches('\n'); + if result != expected_result.as_str() { anyhow::bail!(indoc::formatdoc! {" {id} diff --git a/src/app/state/repl_state.rs b/src/app/state/repl_state.rs index e416188..81d382c 100644 --- a/src/app/state/repl_state.rs +++ b/src/app/state/repl_state.rs @@ -49,7 +49,7 @@ pub(crate) enum ReplSessionExpecting { Echo { acc: String, last_query: ReplQuery, - expected_result: Option, + expected_result: ExpectedResult, }, ResultAndNextPrompt { acc: String, @@ -77,6 +77,11 @@ impl Iterator for ReplSessionLive { #[derive(Debug, Clone, PartialEq, Eq, derive_more::Deref, derive_more::Display)] pub(crate) struct ExpectedResult(pub(crate) String); +impl ExpectedResult { + pub(crate) fn empty() -> ExpectedResult { + Self(String::new()) + } +} impl From for ExpectedResult { fn from(expected_result: LFLine) -> Self { diff --git a/src/repl/example.rs b/src/repl/example.rs index 0699325..a218cfb 100644 --- a/src/repl/example.rs +++ b/src/repl/example.rs @@ -31,7 +31,7 @@ enum Expecting { #[default] PromptAndQuery, ResultOrBlankLine(ReplQuery), - BlankLine(ReplQuery, Option), + BlankLine(ReplQuery, ExpectedResult), } impl std::str::FromStr for ReplExampleEntries { @@ -53,10 +53,12 @@ impl std::str::FromStr for ReplExampleEntries { } Expecting::ResultOrBlankLine(query) => { if line.as_str() == "\n" { - state.entries.push(ReplEntry::new(query, None)); + state + .entries + .push(ReplEntry::new(query, ExpectedResult::empty())); state.expecting = Expecting::PromptAndQuery; } else { - let expected = Some(ExpectedResult::from(line)); + let expected = ExpectedResult::from(line); state.expecting = Expecting::BlankLine(query, expected); } } @@ -84,11 +86,11 @@ impl std::str::FromStr for ReplExampleEntries { #[derive(Debug, Clone)] pub(crate) struct ReplEntry { pub(crate) query: ReplQuery, - pub(crate) expected_result: Option, + pub(crate) expected_result: ExpectedResult, } impl ReplEntry { - pub(crate) fn new(query: ReplQuery, expected_result: Option) -> Self { + pub(crate) fn new(query: ReplQuery, expected_result: ExpectedResult) -> Self { Self { query, expected_result,