Skip to content

Commit

Permalink
Merge pull request #89 from mobusoperandi/repl-example-parse-fail-fil…
Browse files Browse the repository at this point in the history
…e-line

repl example parse fail file line
  • Loading branch information
mightyiam authored Feb 21, 2024
2 parents 7b9ef00 + 202e8ef commit 8faf08a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 19 deletions.
17 changes: 11 additions & 6 deletions src/examples.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::example_id::ExampleId;
use crate::expression::ExpressionExample;
use crate::repl::example::ReplExample;
use crate::repl::example::NIX_REPL_LANG_TAG;
use anyhow::Context;
use itertools::Itertools;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -34,21 +36,24 @@ pub(crate) fn obtain(glob: &str) -> anyhow::Result<Vec<Example>> {
.filter_map(|(path, ast)| {
if let comrak::nodes::NodeValue::CodeBlock(code_block) = ast.value {
let comrak::nodes::NodeCodeBlock { info, literal, .. } = code_block;
match info.split_ascii_whitespace().next() {
let line = ast.sourcepos.start.line;
let id = ExampleId::new(path, line);

let maybe_result = match info.split_ascii_whitespace().next() {
Some(NIX_REPL_LANG_TAG) => {
let line = ast.sourcepos.start.line;
let repl_example =
ReplExample::try_new(path, line, literal.clone()).map(Example::Repl);
ReplExample::try_new(id.clone(), literal.clone()).map(Example::Repl);
Some(repl_example)
}
Some("nix") => {
let line = ast.sourcepos.start.line;
let expression_example =
ExpressionExample::new(path, line, literal.clone());
ExpressionExample::new(id.clone(), literal.clone());
Some(Ok(Example::Expression(expression_example)))
}
_ => None,
}
};

maybe_result.map(|result| result.context(format!("{id}")))
} else {
None
}
Expand Down
3 changes: 1 addition & 2 deletions src/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ pub(crate) struct ExpressionExample {
}

impl ExpressionExample {
pub(crate) fn new(path: camino::Utf8PathBuf, line: usize, expression: String) -> Self {
let id = ExampleId::new(path, line);
pub(crate) fn new(id: ExampleId, expression: String) -> Self {
Self { id, expression }
}
}
8 changes: 1 addition & 7 deletions src/repl/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ pub(crate) struct ReplExample {
}

impl ReplExample {
pub(crate) fn try_new(
source_path: camino::Utf8PathBuf,
line: usize,
contents: String,
) -> anyhow::Result<Self> {
let id = ExampleId::new(source_path, line);

pub(crate) fn try_new(id: ExampleId, contents: String) -> anyhow::Result<Self> {
Ok(Self {
id,
entries: contents.parse()?,
Expand Down
12 changes: 8 additions & 4 deletions tests/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod util;

use assert_fs::prelude::FileWriteStr;
use indoc::{formatdoc, indoc};
use predicates::boolean::PredicateBooleanExt;
use util::with_eelco;

#[test]
Expand All @@ -15,10 +16,13 @@ fn fails_to_parse() {
"})
.unwrap();

eelco
.assert()
.failure()
.stderr("Error: expected prompt, found LFLine(\"nix-shnepl> nope\\n\")\n");
let file_path = file.path().to_str().unwrap();

eelco.assert().failure().stderr(
predicates::str::starts_with(format!("Error: {file_path}:1")).and(
predicates::str::contains("expected prompt, found LFLine(\"nix-shnepl> nope\\n\")"),
),
);
});
}

Expand Down

0 comments on commit 8faf08a

Please sign in to comment.