-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move obtain to examples module
Co-authored-by: Shahar "Dawn" Or <[email protected]> Co-authored-by: warren2k <[email protected]>
- Loading branch information
1 parent
4d9e324
commit e9c5831
Showing
3 changed files
with
45 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters