mitoo

Crates.iomitoo
lib.rsmitoo
version0.2.2
created_at2025-09-17 01:40:03.180424+00
updated_at2025-09-20 14:22:00.550455+00
descriptionmitoo 是一个Rust工具包类库,对配置读取、文件操作、加解密、转码、正则、线程、sqlite、rabbitMQ等方法进行封装,自定义或集成各种Util工具类。
homepage
repositoryhttps://gitee.com/ranfusheng/mitoo.git
max_upload_size
id1842664
size199,127
(TheoryDance)

documentation

https://crates.io/crates/mitoo

README

mitoo

介绍

mitoo 是一个Rust工具包类库,对配置读取、文件操作、加解密、转码、正则、线程、sqlite、rabbitMQ等方法进行封装,自定义或集成各种Util工具类。

模块说明

  • cmd_util 命令行执行工具
  • conf_util 读取配置文件
  • date_util 日期及时间工具
  • file_util 文件操作工具
  • ffmpeg_util ffmpeg封装的工具
  • log_util 日志工具
  • re_util工具 正则工具
  • secure_util 常规加解密工具,含摘要算法等处理
  • thread_util 线程工具
  • sqlite_util SQLite数据库操作工具
  • rabbitmq_util rabbitmq封装,让使用更便捷

示例一:conf_util 配置使用说明

实现了json与yaml,作为配置使用,用户是知道配置的内容及格式的,当多层配置时,可以直接通过x.y.z.w方式直接对应的数据,具体使用如下:

特别注意:不管是对象,还是数组,都是直接通过点操作的,如 obj.children.name 其中obj是对象,children是一个数组,name是children中对象的一个属性,当obj.children.name获取的是children数组中所有对象的name值。

use mitoo::YamlWrapper;

#[test]
fn test_config_util() {
    let wrapper = JsonWrapper::new("docs/config.yaml").unwrap(); // yaml配置
    // let warpper = YamlWrapper::from_string("......").unwrap();

    // 注意: 这里面的children是一个数组,不管是数组还是对象,都是通过点来操作
    let x = wrapper.get("address.children.name");
    // address.children.name = [String("r"), String("s")]
    println!("address.children.name = {:?}", x);
    println!("=============================================================");

    // 作为配置使用,正常是知晓需要
    let x = wrapper.get_one("address.x.y").as_str().unwrap();
    // address.x.y = hello, json!
    println!("address.x.y = {}", x);
}

示例二:sqlite 数据库读取

// 注意:这里需引入了两个FromSqliteRow,一个用于注解,一个用于扩写后的代码使用
use mitoo::sqlite_util::{FromSqliteRow, SqliteClient};
use from_sqlite_row_macro::FromSqliteRow;
 
// 假设存在一个 users 表,定义对应的结构体
#[derive(Debug, FromSqliteRow)]
struct User {
    id: i64, // 不要使用i32(其没有实现对应的trait)
    name: String,
}
 
#[test]
fn it_works() {
    let client = SqliteClient::new("test.db").expect("Failed to create SqliteUtil");
    let user_results = client.query::<User>("SELECT id, name FROM users");
    println!("User results: {:?}", user_results);
    for item in user_results {
        println!("id = {}, name = {}", item.id, item.name);
    }
}

示例三:rabbitmq 工具

建议各种配置,通过yaml进行配置,然后使用ConfUtil进行读取配置作为参数传递

use mitoo::RabbitMQUtil;

// 生产数据
#[tokio::test]
async fn publish() {
    // 初始化连接信息
    let util = RabbitMQUtil::new("127.0.0.1", 5672, "guest", "guest").await;
    // 定义一个队列
    util.declare_queue("hello").await;
    // 绑定exchange-queue-routing_key
    util.queue_bind("amq.topic", "hello", "hello").await;
    // 循环发送多个消息
    for _i in 0..10 {
        let _x = util.publish("amq.topic", "hello", "Hello World, good!").await;
    }
    // 测试时,需要保证发布之后,进程存活一定时间,让消息传递到rabbitmq
    tokio::time::sleep(Duration::from_secs(2)).await;
}

// 消费数据
#[tokio::test]
async fn consume() {
    // 初始化连接信息
    let util = RabbitMQUtil::new("127.0.0.1", 5672, "guest", "guest").await;

    // 消费指定队列,并指定回调消费函数
    util.consume("hello", |basic_properties, content| async move {
        println!("===============>{}", String::from_utf8(content).unwrap());
        println!("===============>{:?}", basic_properties);
    }).await;
    
    // 等待消费结束
    tokio::time::sleep(Duration::from_secs(5)).await;
}

安装教程

cargo add mitoo

参与贡献

  1. Fork 本仓库

  2. 新建 Feat_xxx 分支

  3. 提交代码

  4. 新建 Pull Request

特技

  1. 使用 Readme_XXX.md 来支持不同的语言,例如 Readme_en.md, Readme_zh.md
  2. Gitee 官方博客 blog.gitee.com
  3. 你可以 https://gitee.com/explore 这个地址来了解 Gitee 上的优秀开源项目
  4. GVP 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目
  5. Gitee 官方提供的使用手册 https://gitee.com/help
  6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 https://gitee.com/gitee-stars/
Commit count: 0

cargo fmt