From 90aea87a068625c09783c67ad5e879f3ee4deb2a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 30 Oct 2020 20:41:42 +0000 Subject: Update pretty dependency --- abnf_to_pest/Cargo.toml | 2 +- abnf_to_pest/src/lib.rs | 59 +++++++++++++++++++++++-------------------------- 2 files changed, 29 insertions(+), 32 deletions(-) (limited to 'abnf_to_pest') diff --git a/abnf_to_pest/Cargo.toml b/abnf_to_pest/Cargo.toml index 6e4cfe7..6a4abc4 100644 --- a/abnf_to_pest/Cargo.toml +++ b/abnf_to_pest/Cargo.toml @@ -16,4 +16,4 @@ doctest = false abnf = "0.6.0" indexmap = "1.0.2" itertools = "0.9.0" -pretty = "0.5.2" +pretty = "0.10.0" diff --git a/abnf_to_pest/src/lib.rs b/abnf_to_pest/src/lib.rs index a579493..64672cc 100644 --- a/abnf_to_pest/src/lib.rs +++ b/abnf_to_pest/src/lib.rs @@ -24,35 +24,35 @@ use abnf::types::{Node, NumVal, Repeat, Rule}; use indexmap::map::IndexMap; use itertools::Itertools; -use pretty::{BoxDoc, Doc}; +use pretty::BoxDoc; trait Pretty { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>>; + fn pretty(&self) -> BoxDoc<'static>; } impl Pretty for Node { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { use Node::*; match self { - Alternation(nodes) => Doc::intersperse( + Alternation(nodes) => BoxDoc::intersperse( nodes.iter().map(|x| x.pretty().nest(2).group()), - Doc::space().append(Doc::text("| ")), + BoxDoc::space().append(BoxDoc::text("| ")), ), - Concatenation(nodes) => Doc::intersperse( + Concatenation(nodes) => BoxDoc::intersperse( nodes.iter().map(|x| x.pretty()), - Doc::space().append(Doc::text("~ ")), + BoxDoc::space().append(BoxDoc::text("~ ")), ), Repetition(rep) => { rep.get_node().pretty().append(rep.get_repeat().pretty()) } - Rulename(s) => Doc::text(escape_rulename(s)), - Group(n) => Doc::text("(") + Rulename(s) => BoxDoc::text(escape_rulename(s)), + Group(n) => BoxDoc::text("(") .append(n.pretty().nest(4).group()) - .append(Doc::text(")")), - Optional(n) => Doc::text("(") + .append(BoxDoc::text(")")), + Optional(n) => BoxDoc::text("(") .append(n.pretty().nest(4).group()) - .append(Doc::text(")?")), - CharVal(s) => Doc::text(format!( + .append(BoxDoc::text(")?")), + CharVal(s) => BoxDoc::text(format!( "^\"{}\"", s.replace("\"", "\\\"").replace("\\", "\\\\") )), @@ -63,8 +63,8 @@ impl Pretty for Node { } impl Pretty for Repeat { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { - Doc::text(match (self.get_min().unwrap_or(0), self.get_max()) { + fn pretty(&self) -> BoxDoc<'static> { + BoxDoc::text(match (self.get_min().unwrap_or(0), self.get_max()) { (0, None) => "*".into(), (1, None) => "+".into(), (0, Some(1)) => "?".into(), @@ -76,9 +76,9 @@ impl Pretty for Repeat { } impl Pretty for NumVal { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { use NumVal::*; - Doc::text(match self { + BoxDoc::text(match self { Range(x, y) => { format!("'{}'..'{}'", format_char(*x), format_char(*y)) } @@ -135,16 +135,16 @@ pub struct PestyRule { } impl Pretty for (String, PestyRule) { - fn pretty(&self) -> Doc<'static, BoxDoc<'static, ()>> { + fn pretty(&self) -> BoxDoc<'static> { let (name, rule) = self; - Doc::nil() - .append(Doc::text(name.clone())) - .append(Doc::text(" = ")) - .append(Doc::text(if rule.silent { "_" } else { "" })) - .append(Doc::text("{")) - .append(Doc::space().append(rule.node.pretty()).nest(2)) - .append(Doc::space()) - .append(Doc::text("}")) + BoxDoc::nil() + .append(BoxDoc::text(name.clone())) + .append(BoxDoc::text(" = ")) + .append(BoxDoc::text(if rule.silent { "_" } else { "" })) + .append(BoxDoc::text("{")) + .append(BoxDoc::space().append(rule.node.pretty()).nest(2)) + .append(BoxDoc::space()) + .append(BoxDoc::text("}")) .group() } } @@ -170,13 +170,10 @@ pub fn parse_abnf( .collect()) } -pub fn render_rules_to_pest( - rules: I, -) -> Doc<'static, BoxDoc<'static, ()>, ()> +pub fn render_rules_to_pest(rules: I) -> BoxDoc<'static> where I: IntoIterator, { let pretty_rules = rules.into_iter().map(|x| x.pretty()); - let doc: Doc<_> = Doc::intersperse(pretty_rules, Doc::newline()); - doc + BoxDoc::intersperse(pretty_rules, BoxDoc::hardline()) } -- cgit v1.2.3