#![cfg_attr(not(feature = "std"), no_std)]
#![doc(html_root_url = "https://docs.rs/indenter/0.3.3")]
#![warn(
missing_debug_implementations,
missing_docs,
missing_doc_code_examples,
rust_2018_idioms,
unreachable_pub,
bad_style,
const_err,
dead_code,
improper_ctypes,
non_shorthand_field_patterns,
no_mangle_generic_items,
overflowing_literals,
path_statements,
patterns_in_fns_without_body,
private_in_public,
unconditional_recursion,
unused,
unused_allocation,
unused_comparisons,
unused_parens,
while_true
)]
use core::fmt;
#[allow(missing_debug_implementations)]
pub enum Format<'a> {
Uniform {
indentation: &'static str,
},
Numbered {
ind: usize,
},
Custom {
inserter: &'a mut Inserter,
},
}
#[allow(missing_debug_implementations)]
pub struct Indented<'a, D: ?Sized> {
inner: &'a mut D,
needs_indent: bool,
format: Format<'a>,
}
pub type Inserter = dyn FnMut(usize, &mut dyn fmt::Write) -> fmt::Result;
impl Format<'_> {
fn insert_indentation(&mut self, line: usize, f: &mut dyn fmt::Write) -> fmt::Result {
match self {
Format::Uniform { indentation } => write!(f, "{}", indentation),
Format::Numbered { ind } => {
if line == 0 {
write!(f, "{: >4}: ", ind)
} else {
write!(f, " ")
}
}
Format::Custom { inserter } => inserter(line, f),
}
}
}
impl<'a, D> Indented<'a, D> {
pub fn ind(self, ind: usize) -> Self {
self.with_format(Format::Numbered { ind })
}
pub fn with_str(self, indentation: &'static str) -> Self {
self.with_format(Format::Uniform { indentation })
}
pub fn with_format(mut self, format: Format<'a>) -> Self {
self.format = format;
self
}
}
impl<T> fmt::Write for Indented<'_, T>
where
T: fmt::Write + ?Sized,
{
fn write_str(&mut self, s: &str) -> fmt::Result {
for (ind, line) in s.split('\n').enumerate() {
if ind > 0 {
self.inner.write_char('\n')?;
self.needs_indent = true;
}
if self.needs_indent {
if line.is_empty() {
continue;
}
self.format.insert_indentation(ind, &mut self.inner)?;
self.needs_indent = false;
}
self.inner.write_fmt(format_args!("{}", line))?;
}
Ok(())
}
}
pub fn indented<D: ?Sized>(f: &mut D) -> Indented<'_, D> {
Indented {
inner: f,
needs_indent: true,
format: Format::Uniform {
indentation: " ",
},
}
}
#[cfg(feature = "std")]
#[allow(missing_debug_implementations)]
pub struct CodeFormatter<'a, T> {
f: &'a mut T,
level: u32,
indentation: String,
}
#[cfg(feature = "std")]
impl<'a, T: fmt::Write> fmt::Write for CodeFormatter<'a, T> {
fn write_str(&mut self, input: &str) -> fmt::Result {
let input = match input.chars().next() {
Some('\n') => &input[1..],
_ => return self.f.write_str(input),
};
let min = input
.split('\n')
.map(|line| line.chars().take_while(char::is_ascii_whitespace).count())
.filter(|count| *count > 0)
.min()
.unwrap_or_default();
let input = input.trim_end_matches(|c| char::is_ascii_whitespace(&c));
for line in input.split('\n') {
if line.len().saturating_sub(min) > 0 {
for _ in 0..self.level {
self.f.write_str(&self.indentation)?;
}
}
if line.len() >= min {
self.f.write_str(&line[min..])?;
} else {
self.f.write_str(&line)?;
}
self.f.write_char('\n')?;
}
Ok(())
}
fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result {
self.write_str(&args.to_string())
}
}
#[cfg(feature = "std")]
impl<'a, T: fmt::Write> CodeFormatter<'a, T> {
pub fn new<S: Into<String>>(f: &'a mut T, indentation: S) -> Self {
Self {
f,
level: 0,
indentation: indentation.into(),
}
}
pub fn set_level(&mut self, level: u32) {
self.level = level;
}
pub fn indent(&mut self, inc: u32) {
self.level = self.level.saturating_add(inc);
}
pub fn dedent(&mut self, inc: u32) {
self.level = self.level.saturating_sub(inc);
}
}
#[cfg(test)]
mod tests {
extern crate alloc;
use super::*;
use alloc::string::String;
use core::fmt::Write as _;
#[test]
fn one_digit() {
let input = "verify\nthis";
let expected = " 2: verify\n this";
let mut output = String::new();
indented(&mut output).ind(2).write_str(input).unwrap();
assert_eq!(expected, output);
}
#[test]
fn two_digits() {
let input = "verify\nthis";
let expected = " 12: verify\n this";
let mut output = String::new();
indented(&mut output).ind(12).write_str(input).unwrap();
assert_eq!(expected, output);
}
#[test]
fn no_digits() {
let input = "verify\nthis";
let expected = " verify\n this";
let mut output = String::new();
indented(&mut output).write_str(input).unwrap();
assert_eq!(expected, output);
}
#[test]
fn with_str() {
let input = "verify\nthis";
let expected = "...verify\n...this";
let mut output = String::new();
indented(&mut output)
.with_str("...")
.write_str(input)
.unwrap();
assert_eq!(expected, output);
}
#[test]
fn dyn_write() {
let input = "verify\nthis";
let expected = " verify\n this";
let mut output = String::new();
let writer: &mut dyn core::fmt::Write = &mut output;
indented(writer).write_str(input).unwrap();
assert_eq!(expected, output);
}
#[test]
fn nice_api() {
let input = "verify\nthis";
let expected = " 1: verify\n this";
let output = &mut String::new();
let n = 1;
write!(
indented(output).with_format(Format::Custom {
inserter: &mut move |line_no, f| {
if line_no == 0 {
write!(f, "{: >4}: ", n)
} else {
write!(f, " ")
}
}
}),
"{}",
input
)
.unwrap();
assert_eq!(expected, output);
}
#[test]
fn nice_api_2() {
let input = "verify\nthis";
let expected = " verify\n this";
let output = &mut String::new();
write!(
indented(output).with_format(Format::Uniform { indentation: " " }),
"{}",
input
)
.unwrap();
assert_eq!(expected, output);
}
#[test]
fn trailing_newlines() {
let input = "verify\nthis\n";
let expected = " verify\n this\n";
let output = &mut String::new();
write!(indented(output).with_str(" "), "{}", input).unwrap();
assert_eq!(expected, output);
}
#[test]
fn several_interpolations() {
let input = "verify\nthis\n";
let expected = " verify\n this\n and verify\n this\n";
let output = &mut String::new();
write!(indented(output).with_str(" "), "{} and {}", input, input).unwrap();
assert_eq!(expected, output);
}
}
#[cfg(all(test, feature = "std"))]
mod tests_std {
use super::*;
use core::fmt::Write as _;
#[test]
fn dedent() {
let mut s = String::new();
let mut f = CodeFormatter::new(&mut s, " ");
write!(
f,
r#"
struct Foo;
impl Foo {{
fn foo() {{
todo!()
}}
}}
"#,
)
.unwrap();
assert_eq!(
s,
"struct Foo;\n\nimpl Foo {\n fn foo() {\n todo!()\n }\n}\n"
);
let mut s = String::new();
let mut f = CodeFormatter::new(&mut s, " ");
write!(
f,
r#"
struct Foo;
impl Foo {{
fn foo() {{
todo!()
}}
}}"#,
)
.unwrap();
assert_eq!(
s,
"struct Foo;\n\nimpl Foo {\n fn foo() {\n todo!()\n }\n}\n"
);
}
#[test]
fn indent() {
let mut s = String::new();
let mut f = CodeFormatter::new(&mut s, " ");
f.indent(1);
write!(
f,
r#"
struct Foo;
impl Foo {{
fn foo() {{
todo!()
}}
}}
"#,
)
.unwrap();
assert_eq!(s, " struct Foo;\n\n impl Foo {\n fn foo() {\n todo!()\n }\n }\n");
}
#[test]
fn inline() {
let mut s = String::new();
let mut f = CodeFormatter::new(&mut s, " ");
write!(
f,
r#"struct Foo;
fn foo() {{
}}"#,
)
.unwrap();
assert_eq!(s, "struct Foo;\n fn foo() {\n }");
}
#[test]
fn split_prefix() {
let mut s = String::new();
let mut f = CodeFormatter::new(&mut s, " ");
writeln!(f).unwrap();
assert_eq!(s, "\n");
}
}