8 个版本
| 0.3.3 | 2021年2月22日 | 
|---|---|
| 0.3.2 | 2021年1月4日 | 
| 0.3.1 | 2020年12月21日 | 
| 0.3.0 | 2020年5月17日 | 
| 0.1.1 | 2020年2月28日 | 
30 在 文本处理 中
1,179,020 每月下载量
用于 1,801 个 Crates (27 直接)
21KB
349 行
indenter
几个针对 fmt::Write 对象的包装器,用于在每行之后高效地追加和删除常见的缩进
设置
将此添加到您的 Cargo.toml
[dependencies]
indenter = "0.2"
示例
仅缩进
此类型主要设计用于编写错误报告器,以优雅地格式化跨多行的错误消息。
use std::error::Error;
use std::fmt::{self, Write};
use indenter::indented;
struct ErrorReporter<'a>(&'a dyn Error);
impl fmt::Debug for ErrorReporter<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut source = Some(self.0);
        let mut i = 0;
        while let Some(error) = source {
            writeln!(f)?;
            write!(indented(f).ind(i), "{}", error)?;
            source = error.source();
            i += 1;
        }
        Ok(())
    }
}
“去缩进”(删除常见的首行缩进)
此类型主要设计用于格式化源代码。例如,在生成代码时。
此类型需要 std 功能。
use std::error::Error;
use core::fmt::{self, Write};
use indenter::CodeFormatter;
let mut output = String::new();
let mut f = CodeFormatter::new(&mut output, "    ");
write!(
    f,
    r#"
    Hello
        World
    "#,
);
assert_eq!(output, "Hello\n    World\n");
let mut output = String::new();
let mut f = CodeFormatter::new(&mut output, "    ");
// it can also indent...
f.indent(2);
write!(
    f,
    r#"
    Hello
        World
    "#,
);
assert_eq!(output, "        Hello\n            World\n");
许可
根据您的选择,在 Apache License, Version 2.0 或 MIT 许可证 下许可。除非您明确声明,否则根据 Apache-2.0 许可证定义,您有意提交以包含在此软件包中的任何贡献,都将按上述方式双重许可,而无需任何额外条款或条件。