Rust文本块处理宏库text-block-macros的使用:简化多行字符串和代码块格式化的宏工具

Rust文本块处理宏库text-block-macros的使用:简化多行字符串和代码块格式化的宏工具

使用示例

创建一个不带末尾换行符的文本块

use text_block_macros::text_block;
let text = text_block! {
  "abc"
  "def"
  "ghi"
};
assert_eq!(text, "abc\ndef\nghi");

创建一个带末尾换行符的文本块

use text_block_macros::text_block_fnl;
let text = text_block_fnl! {
  "abc"
  "def"
  "ghi"
};
assert_eq!(text, "abc\ndef\nghi\n");

完整示例代码

// 引入宏库
use text_block_macros::{text_block, text_block_fnl};

fn main() {
    // 示例1:不带末尾换行符的文本块
    let block1 = text_block! {
        "第一行"
        "第二行"
        "第三行"
    };
    println!("不带末尾换行符的文本块:\n{}", block1);
    assert_eq!(block1, "第一行\n第二行\n第三行");

    // 示例2:带末尾换行符的文本块
    let block2 = text_block_fnl! {
        "第一行"
        "第二行"
        "第三行"
    };
    println!("\n带末尾换行符的文本块:\n{}", block2);
    assert_eq!(block2, "第一行\n第二行\n第三行\n");

    // 示例3:实际应用场景 - 生成SQL查询
    let table_name = "users";
    let query = text_block! {
        "SELECT id, name, email"
        "FROM"
        table_name
        "WHERE active = true"
        "ORDER BY created_at DESC"
    };
    println!("\n生成的SQL查询:\n{}", query);
}

安装方法

在项目目录中运行以下Cargo命令:

cargo add text_block_macros

或者在Cargo.toml中添加:

text-block-macros = "0.2.0"

特性

  • 简化多行字符串的创建
  • 提供两种版本:带末尾换行符和不带末尾换行符
  • 适用于各种文本处理场景
  • 无标准库依赖(no-std)

许可证

MIT © Hoàng Văn Khải


1 回复

Rust文本块处理宏库text-block-macros使用指南

text-block-macros是一个简化Rust中多行字符串和代码块格式化的宏工具库,特别适合处理需要保留特定格式的文本块。

主要功能

  1. 简化多行字符串的创建
  2. 自动处理缩进和空白字符
  3. 提供更清晰的代码块格式化方式

安装

Cargo.toml中添加依赖:

[dependencies]
text-block-macros = "0.1"

基本使用

text!

use text_block_macros::text;

let message = text! {
    Hello,
    This is a multi-line
    text block!
};

assert_eq!(message, "Hello,\nThis is a multi-line\ntext block!");

trim_text!

自动去除每行前导和尾随空白:

use text_block_macros::trim_text;

let code = trim_text! {
    fn main() {
        println!("Hello, world!");
    }
};

assert_eq!(code, "fn main() {\n    println!(\"Hello, world!\");\n}");

高级用法

保留特定缩进

use text_block_macros::indent_text;

let indented = indent_text! {
    First line
        Second line with indent
    Third line
};

assert_eq!(indented, "First line\n    Second line with indent\nThird line");

代码块格式化

use text_block_macros::code_block;

let rust_code = code_block! {
    pub struct Point {
        x: i32,
        y: i32,
    }

    impl Point {
        pub fn new(x: i32, y: i32) -> Self {
            Self { x, y }
        }
    }
};

配置选项

可以通过属性配置宏行为:

use text_block_macros::{text, trim_text};

#[text(preserve_indent = true)]
let preserved = text! {
    Line one
        Line two
    Line three
};

#[trim_text(trim_empty_lines = false)]
let with_empty_lines = trim_text! {
    First line

    Second line after empty line
};

实际应用示例

生成HTML

use text_block_macros::trim_text;

let html = trim_text! {
    <!DOCTYPE html>
    <html>
        <head>
            <title>Example</title>
        </head>
        <body>
            <h1>Hello, world!</h1>
        </body>
    </html>
};

测试用例中的多行字符串

use text_block_macros::text;

#[test]
fn test_multiline() {
    let expected = text! {
        Line 1
        Line 2
        Line 3
    };
    
    let actual = some_function();
    assert_eq!(actual, expected);
}

完整示例代码

// 引入宏库
use text_block_macros::{text, trim_text, indent_text, code_block};

fn main() {
    // 基本text!宏示例
    let greeting = text! {
        Hello there!
        This is a multi-line
        text example.
    };
    println!("{}", greeting);

    // trim_text!宏示例 - 自动去除空白
    let trimmed_code = trim_text! {
        fn add(a: i32, b: i32) -> i32 {
            a + b
        }
    };
    println!("Trimmed code:\n{}", trimmed_code);

    // indent_text!宏示例 - 保留缩进
    let indented = indent_text! {
        Start
            Indented line
        End
    };
    println!("Indented text:\n{}", indented);

    // code_block!宏示例 - 代码块格式化
    let point_impl = code_block! {
        #[derive(Debug)]
        pub struct Point {
            x: f64,
            y: f64,
        }

        impl Point {
            pub fn distance(&self) -> f64 {
                (self.x.powi(2) + self.y.powi(2)).sqrt()
            }
        }
    };
    println!("Code block:\n{}", point_impl);

    // 配置选项示例
    #[text(preserve_indent = true)]
    let configured_text = text! {
        Line 1
            Line 2 with indent
        Line 3
    };
    println!("Configured text:\n{}", configured_text);

    // 生成HTML示例
    let html_page = trim_text! {
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <title>Demo Page</title>
        </head>
        <body>
            <h1>Welcome</h1>
            <p>This page was generated using text-block-macros</p>
        </body>
        </html>
    };
    println!("HTML:\n{}", html_page);
}

#[test]
fn test_text_macros() {
    // 测试用例中的多行字符串
    let expected = text! {
        Expected
        output
        string
    };
    
    let actual = "Expected\noutput\nstring";
    assert_eq!(actual, expected);
}

text-block-macros通过提供这些宏,大大简化了Rust中处理多行文本和代码块的复杂度,使代码更加清晰易读。

回到顶部