Skip to content

Commit

Permalink
refactor: move obtain to examples module
Browse files Browse the repository at this point in the history
Co-authored-by: Shahar "Dawn" Or <[email protected]>
Co-authored-by: warren2k <[email protected]>
  • Loading branch information
3 people committed Dec 31, 2023
1 parent 4d9e324 commit e9c5831
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 40 deletions.
42 changes: 42 additions & 0 deletions crates/eelco/src/examples.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use crate::repl::example::ReplExample;
use itertools::Itertools;
use crate::repl::example::NIX_REPL_LANG_TAG;

pub(crate) fn obtain(glob: &str) -> anyhow::Result<Vec<ReplExample>> {
glob::glob(glob)?
.map(|path| {
let path = camino::Utf8PathBuf::try_from(path?)?;
let contents = std::fs::read_to_string(path.clone())?;
anyhow::Ok((path, contents))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flat_map(|(path, contents)| {
let arena = comrak::Arena::new();
let ast =
comrak::parse_document(&arena, &contents, &comrak::ComrakOptions::default());
ast.traverse()
.filter_map(move |node_edge| match node_edge {
comrak::arena_tree::NodeEdge::Start(node) => {
let ast = node.data.borrow().clone();
Some((path.clone(), ast))
}
comrak::arena_tree::NodeEdge::End(_) => None,
})
.collect::<Vec<_>>()
})
.filter_map(|(path, ast)| {
if let comrak::nodes::NodeValue::CodeBlock(code_block) = ast.value {
let comrak::nodes::NodeCodeBlock { info, literal, .. } = code_block;
if let Some(NIX_REPL_LANG_TAG) = info.split_ascii_whitespace().next() {
Some((path, ast.sourcepos.start.line, literal.clone()))
} else {
None
}
} else {
None
}
})
.map(|(path, line, contents)| ReplExample::try_new(path, line, contents))
.try_collect()
}
3 changes: 2 additions & 1 deletion crates/eelco/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

pub(crate) mod app;
pub(crate) mod example_id;
mod examples;
pub(crate) mod repl;

use clap::Parser;
Expand All @@ -26,7 +27,7 @@ struct Cli {
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let cli = Cli::parse();
let repl_examples = repl::example::obtain(&cli.sources)?;
let repl_examples = examples::obtain(&cli.sources)?;
if repl_examples.is_empty() {
anyhow::bail!("could not find any REPL examples");
}
Expand Down
40 changes: 1 addition & 39 deletions crates/eelco/src/repl/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,42 +61,4 @@ impl TryFrom<(LFLine, LFLine)> for ReplEntry {
}
}

const NIX_REPL_LANG_TAG: &str = "nix-repl";

pub(crate) fn obtain(glob: &str) -> anyhow::Result<Vec<ReplExample>> {
glob::glob(glob)?
.map(|path| {
let path = camino::Utf8PathBuf::try_from(path?)?;
let contents = std::fs::read_to_string(path.clone())?;
anyhow::Ok((path, contents))
})
.collect::<Result<Vec<_>, _>>()?
.into_iter()
.flat_map(|(path, contents)| {
let arena = comrak::Arena::new();
let ast = comrak::parse_document(&arena, &contents, &comrak::ComrakOptions::default());
ast.traverse()
.filter_map(move |node_edge| match node_edge {
comrak::arena_tree::NodeEdge::Start(node) => {
let ast = node.data.borrow().clone();
Some((path.clone(), ast))
}
comrak::arena_tree::NodeEdge::End(_) => None,
})
.collect::<Vec<_>>()
})
.filter_map(|(path, ast)| {
if let comrak::nodes::NodeValue::CodeBlock(code_block) = ast.value {
let comrak::nodes::NodeCodeBlock { info, literal, .. } = code_block;
if let Some(NIX_REPL_LANG_TAG) = info.split_ascii_whitespace().next() {
Some((path, ast.sourcepos.start.line, literal.clone()))
} else {
None
}
} else {
None
}
})
.map(|(path, line, contents)| ReplExample::try_new(path, line, contents))
.try_collect()
}
pub(crate) const NIX_REPL_LANG_TAG: &str = "nix-repl";

0 comments on commit e9c5831

Please sign in to comment.