Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: no Option<ExpectedResult> #105

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/app/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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}
Expand Down
7 changes: 6 additions & 1 deletion src/app/state/repl_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ pub(crate) enum ReplSessionExpecting {
Echo {
acc: String,
last_query: ReplQuery,
expected_result: Option<ExpectedResult>,
expected_result: ExpectedResult,
},
ResultAndNextPrompt {
acc: String,
Expand Down Expand Up @@ -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<LFLine> for ExpectedResult {
fn from(expected_result: LFLine) -> Self {
Expand Down
12 changes: 7 additions & 5 deletions src/repl/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ enum Expecting {
#[default]
PromptAndQuery,
ResultOrBlankLine(ReplQuery),
BlankLine(ReplQuery, Option<ExpectedResult>),
BlankLine(ReplQuery, ExpectedResult),
}

impl std::str::FromStr for ReplExampleEntries {
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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<ExpectedResult>,
pub(crate) expected_result: ExpectedResult,
}

impl ReplEntry {
pub(crate) fn new(query: ReplQuery, expected_result: Option<ExpectedResult>) -> Self {
pub(crate) fn new(query: ReplQuery, expected_result: ExpectedResult) -> Self {
Self {
query,
expected_result,
Expand Down