From 5b32c5bdea80a0bdf19240f3cc2b8e2ae251d51a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Wed, 6 Mar 2019 00:51:20 +0100 Subject: rustfmt --- abnf_to_pest/src/lib.rs | 81 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 26 deletions(-) (limited to 'abnf_to_pest/src') diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index 7381113..38347ed 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -1,5 +1,5 @@ -use std::collections::HashMap; use itertools::Itertools; +use std::collections::HashMap; pub struct PestRuleSettings { pub visible: bool, @@ -8,14 +8,23 @@ pub struct PestRuleSettings { impl Default for PestRuleSettings { fn default() -> Self { - PestRuleSettings { visible: true, replace: None } + PestRuleSettings { + visible: true, + replace: None, + } } } -pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap) -> std::io::Result { +pub fn abnf_to_pest( + data: &Vec, + rule_settings: &HashMap, +) -> std::io::Result { use abnf::abnf::*; - use pretty::{Doc, BoxDoc}; - fn format_rule(x: Rule, rule_settings: &HashMap) -> Doc> { + use pretty::{BoxDoc, Doc}; + fn format_rule( + x: Rule, + rule_settings: &HashMap, + ) -> Doc> { let rulename = format_rulename(x.name); let default = Default::default(); let setting = rule_settings.get(&rulename).unwrap_or(&default); @@ -36,7 +45,13 @@ pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap String { let x = x.replace("-", "_"); - if x == "if" || x == "else" || x == "as" || x == "let" || x == "in" || x == "fn" { + if x == "if" + || x == "else" + || x == "as" + || x == "let" + || x == "in" + || x == "fn" + { x + "_" } else { x @@ -44,19 +59,27 @@ pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap Doc<'static, BoxDoc<'static, ()>> { Doc::intersperse( - x.concatenations.into_iter().map(|x| format_concatenation(x).nest(2).group()), - Doc::space().append(Doc::text("| ")) + x.concatenations + .into_iter() + .map(|x| format_concatenation(x).nest(2).group()), + Doc::space().append(Doc::text("| ")), ) } - fn format_concatenation(x: Concatenation) -> Doc<'static, BoxDoc<'static, ()>> { + fn format_concatenation( + x: Concatenation, + ) -> Doc<'static, BoxDoc<'static, ()>> { Doc::intersperse( x.repetitions.into_iter().map(format_repetition), - Doc::space().append(Doc::text("~ ")) + Doc::space().append(Doc::text("~ ")), ) } fn format_repetition(x: Repetition) -> Doc<'static, BoxDoc<'static, ()>> { - format_element(x.element) - .append(x.repeat.map(format_repeat).map(Doc::text).unwrap_or(Doc::nil())) + format_element(x.element).append( + x.repeat + .map(format_repeat) + .map(Doc::text) + .unwrap_or(Doc::nil()), + ) } fn format_repeat(x: Repeat) -> String { match (x.min.unwrap_or(0), x.max) { @@ -72,15 +95,16 @@ pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap Doc::text(format_rulename(s)), - Group(g) => - Doc::text("(") - .append(format_alternation(g.alternation).nest(4).group()) - .append(Doc::text(")")), - Option(o) => - Doc::text("(") - .append(format_alternation(o.alternation).nest(4).group()) - .append(Doc::text(")?")), - CharVal(s) => Doc::text(format!("^\"{}\"", s.replace("\"", "\\\"").replace("\\", "\\\\"))), + Group(g) => Doc::text("(") + .append(format_alternation(g.alternation).nest(4).group()) + .append(Doc::text(")")), + Option(o) => Doc::text("(") + .append(format_alternation(o.alternation).nest(4).group()) + .append(Doc::text(")?")), + CharVal(s) => Doc::text(format!( + "^\"{}\"", + s.replace("\"", "\\\"").replace("\\", "\\\\") + )), NumVal(r) => Doc::text(format_range(r)), ProseVal(_) => unimplemented!(), } @@ -88,8 +112,12 @@ pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap String { use abnf::abnf::Range::*; match x { - Range(x, y) => format!("'{}'..'{}'", format_char(x), format_char(y)), - OneOf(v) => format!("\"{}\"", v.into_iter().map(format_char).join("")), + Range(x, y) => { + format!("'{}'..'{}'", format_char(x), format_char(y)) + } + OneOf(v) => { + format!("\"{}\"", v.into_iter().map(format_char).join("")) + } } } fn format_char(x: u64) -> String { @@ -104,11 +132,12 @@ pub fn abnf_to_pest(data: &Vec, rule_settings: &HashMap = Doc::intersperse(formatted_rules, Doc::newline()); Ok(format!("{}", doc.pretty(80))) } - -- cgit v1.2.3