From 9c27e986dcd14c19e934be21b9963352c61dac9b Mon Sep 17 00:00:00 2001 From: Bobbin Threadbare Date: Thu, 21 Mar 2024 15:50:49 -0700 Subject: [PATCH] chore: fix badges and update rustfmt --- README.md | 8 +++---- formatting/src/prettier/document.rs | 10 ++++---- formatting/src/prettier/mod.rs | 4 ++-- formatting/src/prettier/print.rs | 36 ++++++++++------------------- formatting/src/prettier/tests.rs | 1 - rustfmt.toml | 9 +++++--- 6 files changed, 29 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index b4e8258..e618eb2 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,9 @@ # `miden-formatting` -[![LICENSE](https://github.com/0xPolygonMiden/miden-vm/blob/main/LICENSE)](https://img.shields.io/badge/license-MIT-blue.svg) -[![RUST_VERSION]()](https://img.shields.io/badge/rustc-1.76+-lightgray.svg) -[![CRATE](https://crates.io/crates/miden-formatting)](https://img.shields.io/crates/v/miden-formatting) -[![CI](https://github.com/0xPolygonMiden/miden-formatting/workflows/CI/badge.svg?branch=main)](https://github.com/0xPolygonMiden/miden-formatting/actions?query=workflow%3A%22CI%22+branch%3Amain) +[![LICENSE](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/0xPolygonMiden/miden-formatting/blob/main/LICENSE) +[![RUST_VERSION](https://img.shields.io/badge/rustc-1.76+-lightgray.svg)]() +[![CRATE](https://img.shields.io/crates/v/miden-formatting)](https://crates.io/crates/miden-formatting) +[![CI](https://github.com/0xPolygonMiden/miden-formatting/actions/workflows/ci.yml/badge.svg)](https://github.com/0xPolygonMiden/miden-formatting/actions/workflows/ci.yml) This crate provides some general infrastructure for pretty-printers and value foramtting that is needed by various Miden crates. Rather than implement this stuff in every place where it is needed, we've extracted the most important and general bits and put them in this crate. diff --git a/formatting/src/prettier/document.rs b/formatting/src/prettier/document.rs index 19c263e..84faecd 100644 --- a/formatting/src/prettier/document.rs +++ b/formatting/src/prettier/document.rs @@ -44,7 +44,7 @@ impl Document { Self::Empty => false, Self::Newline => true, Self::Char('\n' | '\r', _) => true, - Self::Char(_, _) => false, + Self::Char(..) => false, Self::Text(ref text, _) => text.starts_with(['\n', '\r']), Self::Flatten(doc) => doc.has_leading_newline(), Self::Indent(_, doc) => doc.has_leading_newline(), @@ -96,7 +96,7 @@ pub fn character(c: char) -> Document { c => { let width = unicode_width::UnicodeWidthChar::width(c).unwrap_or(0) as u32; Document::Char(c, width) - } + }, } } @@ -114,7 +114,7 @@ pub fn text(s: impl ToString) -> Document { drop(chars); let width = unicode_width::UnicodeWidthStr::width(string.as_ref()) as u32; Document::Text(string, width) - } + }, } } @@ -129,7 +129,7 @@ pub fn const_text(s: &'static str) -> Document { let string = Cow::Borrowed(s); let width = unicode_width::UnicodeWidthStr::width(string.as_ref()) as u32; Document::Text(string, width) - } + }, } } @@ -330,7 +330,7 @@ impl fmt::Display for Document { doc => { let width = f.width().unwrap_or(80); super::print::pretty_print(doc, width, f) - } + }, } } } diff --git a/formatting/src/prettier/mod.rs b/formatting/src/prettier/mod.rs index b5a7d2e..11780e4 100644 --- a/formatting/src/prettier/mod.rs +++ b/formatting/src/prettier/mod.rs @@ -12,11 +12,11 @@ mod print; #[cfg(test)] mod tests; -pub use self::document::{concat, const_text, display, flatten, indent, nl, split, text, Document}; - use alloc::string::String; use core::fmt; +pub use self::document::{concat, const_text, display, flatten, indent, nl, split, text, Document}; + /// The [PrettyPrint] trait is used as a building block for pretty printing data or syntax trees, /// as commonly seen in tools like Prettier. /// diff --git a/formatting/src/prettier/print.rs b/formatting/src/prettier/print.rs index 9032534..18f24fe 100644 --- a/formatting/src/prettier/print.rs +++ b/formatting/src/prettier/print.rs @@ -39,26 +39,14 @@ impl<'a> Chunk<'a> { } fn flat(self, doc: &'a Document) -> Self { - Self { - doc, - indent: self.indent, - flat: true, - } + Self { doc, indent: self.indent, flat: true } } } impl<'a> PrettyPrinter<'a> { fn new(doc: &'a Document, width: usize) -> Self { - let chunk = Chunk { - doc, - indent: 0, - flat: false, - }; - Self { - width, - col: 0, - chunks: vec![chunk], - } + let chunk = Chunk { doc, indent: 0, flat: false }; + Self { width, col: 0, chunks: vec![chunk] } } fn print(&mut self, f: &mut fmt::Formatter) -> fmt::Result { @@ -81,28 +69,28 @@ impl<'a> PrettyPrinter<'a> { write!(f, "{1:0$}", chunk.indent as usize, "")?; self.col = chunk.indent; } - } + }, Document::Char(c, width) => { f.write_char(*c)?; self.col += width; - } + }, Document::Text(text, width) => { f.write_str(text)?; self.col += width; - } + }, Document::Flatten(x) => self.chunks.push(chunk.flat(x)), Document::Indent(i, x) => self.chunks.push(chunk.indented(*i, x)), Document::Concat(x, y) => { self.chunks.push(chunk.with_doc(y)); self.chunks.push(chunk.with_doc(x)); - } + }, Document::Choice(x, y) => { if chunk.flat || self.fits(chunk.with_doc(x)) { self.chunks.push(chunk.with_doc(x)); } else { self.chunks.push(chunk.with_doc(y)); } - } + }, } } Ok(()) @@ -127,7 +115,7 @@ impl<'a> PrettyPrinter<'a> { Some((chunk, more_chunks)) => { chunks = more_chunks; *chunk - } + }, }, }; @@ -139,13 +127,13 @@ impl<'a> PrettyPrinter<'a> { } else { return false; } - } + }, Document::Flatten(x) => stack.push(chunk.flat(x)), Document::Indent(i, x) => stack.push(chunk.indented(*i, x)), Document::Concat(x, y) => { stack.push(chunk.with_doc(y)); stack.push(chunk.with_doc(x)); - } + }, Document::Choice(x, y) => { if chunk.flat { stack.push(chunk.with_doc(x)); @@ -154,7 +142,7 @@ impl<'a> PrettyPrinter<'a> { // the first line of `y` is no longer than the first line of `x`. stack.push(chunk.with_doc(y)); } - } + }, } } } diff --git a/formatting/src/prettier/tests.rs b/formatting/src/prettier/tests.rs index 202ec26..f8cfeaf 100644 --- a/formatting/src/prettier/tests.rs +++ b/formatting/src/prettier/tests.rs @@ -1,5 +1,4 @@ use alloc::{boxed::Box, rc::Rc, string::ToString, vec::Vec}; -use core::fmt; use pretty_assertions::assert_str_eq; diff --git a/rustfmt.toml b/rustfmt.toml index 81e2f10..ed3610c 100644 --- a/rustfmt.toml +++ b/rustfmt.toml @@ -2,12 +2,15 @@ edition = "2021" array_width = 80 attr_fn_like_width = 80 chain_width = 80 -comment_width = 100 +condense_wildcard_suffixes = true fn_call_width = 80 group_imports = "StdExternalCrate" imports_granularity = "Crate" newline_style = "Unix" -reorder_impl_items = true +match_block_trailing_comma = true +single_line_if_else_max_width = 60 +single_line_let_else_max_width = 60 +struct_lit_width = 40 +struct_variant_width = 40 use_field_init_shorthand = true use_try_shorthand = true -wrap_comments = true