项目作者: dtolnay

项目描述 :
Indented document literals for Rust
高级语言: Rust
项目地址: git://github.com/dtolnay/indoc.git
创建时间: 2016-03-19T07:01:28Z
项目社区:https://github.com/dtolnay/indoc

开源协议:Other

下载


Indented Documents (indoc)

github
crates.io
docs.rs
build status

This crate provides a procedural macro for indented string literals. The
indoc!() macro takes a multiline string literal and un-indents it at compile
time so the leftmost non-space character is in the first column.

  1. [dependencies]
  2. indoc = "2"

Compiler requirement: rustc 1.56 or greater.


Using indoc

  1. use indoc::indoc;
  2. fn main() {
  3. let testing = indoc! {"
  4. def hello():
  5. print('Hello, world!')
  6. hello()
  7. "};
  8. let expected = "def hello():\n print('Hello, world!')\n\nhello()\n";
  9. assert_eq!(testing, expected);
  10. }

Indoc also works with raw string literals:

  1. use indoc::indoc;
  2. fn main() {
  3. let testing = indoc! {r#"
  4. def hello():
  5. print("Hello, world!")
  6. hello()
  7. "#};
  8. let expected = "def hello():\n print(\"Hello, world!\")\n\nhello()\n";
  9. assert_eq!(testing, expected);
  10. }

And byte string literals:

  1. use indoc::indoc;
  2. fn main() {
  3. let testing = indoc! {b"
  4. def hello():
  5. print('Hello, world!')
  6. hello()
  7. "};
  8. let expected = b"def hello():\n print('Hello, world!')\n\nhello()\n";
  9. assert_eq!(testing[..], expected[..]);
  10. }


Formatting macros

The indoc crate exports five additional macros to substitute conveniently for
the standard library’s formatting macros:

  • formatdoc!($fmt, ...) — equivalent to format!(indoc!($fmt), ...)
  • printdoc!($fmt, ...) — equivalent to print!(indoc!($fmt), ...)
  • eprintdoc!($fmt, ...) — equivalent to eprint!(indoc!($fmt), ...)
  • writedoc!($dest, $fmt, ...) — equivalent to write!($dest, indoc!($fmt), ...)
  • concatdoc!(...) — equivalent to concat!(...) with each string literal wrapped in indoc!
  1. use indoc::{concatdoc, printdoc};
  2. const HELP: &str = concatdoc! {"
  3. Usage: ", env!("CARGO_BIN_NAME"), " [options]
  4. Options:
  5. -h, --help
  6. "};
  7. fn main() {
  8. printdoc! {"
  9. GET {url}
  10. Accept: {mime}
  11. ",
  12. url = "http://localhost:8080",
  13. mime = "application/json",
  14. }
  15. }


Explanation

The following rules characterize the behavior of the indoc!() macro:

  1. Count the leading spaces of each line, ignoring the first line and any lines
    that are empty or contain spaces only.
  2. Take the minimum.
  3. If the first line is empty i.e. the string begins with a newline, remove the
    first line.
  4. Remove the computed number of spaces from the beginning of each line.


Unindent

Indoc’s indentation logic is available in the unindent crate. This may be
useful for processing strings that are not statically known at compile time.

The crate exposes two functions:

  • unindent(&str) -> String
  • unindent_bytes(&[u8]) -> Vec<u8>
  1. use unindent::unindent;
  2. fn main() {
  3. let indented = "
  4. line one
  5. line two";
  6. assert_eq!("line one\nline two", unindent(indented));
  7. }


License


Licensed under either of Apache License, Version
2.0
or MIT license at your option.



Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in this crate by you, as defined in the Apache-2.0 license, shall
be dual licensed as above, without any additional terms or conditions.