Skip to content

Commit

Permalink
Display impls use token formatting (#183)
Browse files Browse the repository at this point in the history
Deduplicates a bunch of code by having Display impls use the TokenFmt
implementations. There were some bugs with the TokenFmt formatting that
I had to fix, and some slight differences in their output that required
adjusting tests. This also fixes a longstanding bug where there were 2
spaces in the output, like `42 (dimensionless)`.

I also got rid of that two-step pattern parsing stuff, it's simpler to
iterate the chars of the pattern string instead.
  • Loading branch information
tiffany352 authored Jun 2, 2024
1 parent 09c5163 commit 5859e92
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 426 deletions.
33 changes: 33 additions & 0 deletions core/src/output/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,14 @@ impl<'a> Span<'a> {
pub fn link(text: impl Into<Cow<'a, str>>) -> Span<'a> {
Span::new(text, FmtToken::Link)
}

pub fn is_ws(&self) -> bool {
if let Span::Content { text, .. } = self {
text.ends_with(" ")
} else {
false
}
}
}

impl<'a> fmt::Debug for Span<'a> {
Expand Down Expand Up @@ -178,9 +186,34 @@ pub enum FmtToken {
Link,
}

pub(crate) fn write_spans_string(out: &mut String, spans: &[Span]) {
for span in spans {
match span {
Span::Content { text, .. } => out.push_str(text),
Span::Child(child) => write_spans_string(out, &child.to_spans()),
}
}
}

/// Allows an object to be converted into a token tree.
pub trait TokenFmt<'a> {
fn to_spans(&'a self) -> Vec<Span<'a>>;

fn spans_to_string(&'a self) -> String {
let mut string = String::new();
write_spans_string(&mut string, &self.to_spans());
string
}

fn display(&'a self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
for span in self.to_spans() {
match span {
Span::Content { text, .. } => write!(fmt, "{}", text)?,
Span::Child(child) => child.display(fmt)?,
}
}
Ok(())
}
}

pub(crate) struct JoinIter<'a, I>
Expand Down
Loading

0 comments on commit 5859e92

Please sign in to comment.