diff options
author | Lars Wirzenius <liw@liw.fi> | 2019-12-14 13:08:48 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2019-12-14 13:08:48 +0200 |
commit | 413688c93bb069bf0d6a4417f0b15cdd4f32cca8 (patch) | |
tree | c91c9089fdfbadb75a05804899bd5cfff7ad7f1f /src/typeset.rs | |
parent | 06c2f25227e0b78f72bf0186b139f1d9521d6053 (diff) | |
download | subplot-413688c93bb069bf0d6a4417f0b15cdd4f32cca8.tar.gz |
Refactor: move graphs.rs and typeset.rs into ast.rs
Diffstat (limited to 'src/typeset.rs')
-rw-r--r-- | src/typeset.rs | 94 |
1 files changed, 0 insertions, 94 deletions
diff --git a/src/typeset.rs b/src/typeset.rs deleted file mode 100644 index 21cbd30..0000000 --- a/src/typeset.rs +++ /dev/null @@ -1,94 +0,0 @@ -use crate::parser::parse_scenario_snippet; -use crate::Bindings; -use crate::Error; -use crate::PartialStep; -use crate::ScenarioStep; -use crate::StepKind; -use pandoc_ast::{Block, Inline}; - -/// Typeset an error from dot as a Pandoc AST Block element. -pub fn error(err: Error) -> Block { - let msg = format!("ERROR: {}", err.to_string()); - Block::Para(error_msg(&msg)) -} - -// Typeset an error message a vector of inlines. -pub fn error_msg(msg: &str) -> Vec<Inline> { - let msg = Inline::Str(msg.into()); - let msg = Inline::Strong(vec![msg]); - vec![msg] -} - -/// Typeset a scenario snippet as a Pandoc AST Block. -/// -/// Typesetting here means producing the Pandoc abstract syntax tree -/// nodes that result in the desired output, when Pandoc processes -/// them. -/// -/// The snippet is given as a text string, which is parsed. It need -/// not be a complete scenario, but it should consist of complete steps. -pub fn scenario_snippet(bindings: &Bindings, snippet: &str) -> Block { - let steps = parse_scenario_snippet(snippet) - .map(|s| step(bindings, s)) - .collect(); - Block::LineBlock(steps) -} - -// Typeset a single scenario step as a sequence of Pandoc AST Inlines. -fn step(bindings: &Bindings, text: &str) -> Vec<Inline> { - let step = ScenarioStep::from_str(text); - if step.is_none() { - eprintln!("Could not parse step: {}", text); - return error_msg(&format!("Could not parse step: {}", text)); - } - let step = step.unwrap(); - - let m = bindings.find(&step); - if m.is_none() { - eprintln!("Could not findind binding for: {}", text); - return error_msg(&format!("Could not find binding for: {}", text)); - } - let m = m.unwrap(); - - let mut inlines = Vec::new(); - - inlines.push(keyword(&step)); - inlines.push(space()); - - for part in m.parts() { - match part { - PartialStep::UncapturedText(s) => inlines.push(uncaptured(s.text())), - PartialStep::CapturedText(s) => inlines.push(captured(s.text())), - } - } - - inlines -} - -// Typeset first word, which is assumed to be a keyword, of a scenario -// step. -fn keyword(step: &ScenarioStep) -> Inline { - let word = match step.kind() { - StepKind::Given => "given", - StepKind::When => "when", - StepKind::Then => "then", - }; - let word = Inline::Str(word.into()); - Inline::Emph(vec![word]) -} - -// Typeset a space between words. -fn space() -> Inline { - Inline::Space -} - -// Typeset an uncaptured part of a step. -fn uncaptured(s: &str) -> Inline { - Inline::Str(s.into()) -} - -// Typeset a captured part of a step. -fn captured(s: &str) -> Inline { - let s = Inline::Str(s.into()); - Inline::Strong(vec![s]) -} |