Skip to content

Commit

Permalink
chore: fix badges and update rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Mar 21, 2024
1 parent e2c3b54 commit 9c27e98
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 39 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
10 changes: 5 additions & 5 deletions formatting/src/prettier/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
}
},
}
}

Expand All @@ -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)
}
},
}
}

Expand All @@ -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)
}
},
}
}

Expand Down Expand Up @@ -330,7 +330,7 @@ impl fmt::Display for Document {
doc => {
let width = f.width().unwrap_or(80);
super::print::pretty_print(doc, width, f)
}
},
}
}
}
4 changes: 2 additions & 2 deletions formatting/src/prettier/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
36 changes: 12 additions & 24 deletions formatting/src/prettier/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(())
Expand All @@ -127,7 +115,7 @@ impl<'a> PrettyPrinter<'a> {
Some((chunk, more_chunks)) => {
chunks = more_chunks;
*chunk
}
},
},
};

Expand All @@ -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));
Expand All @@ -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));
}
}
},
}
}
}
Expand Down
1 change: 0 additions & 1 deletion formatting/src/prettier/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use alloc::{boxed::Box, rc::Rc, string::ToString, vec::Vec};
use core::fmt;

use pretty_assertions::assert_str_eq;

Expand Down
9 changes: 6 additions & 3 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 9c27e98

Please sign in to comment.