# mitoo

## Introduction

mitoo is a Rust toolkit library that encapsulates methods such as configuration reading, file operations, encryption and decryption, transcoding, regular expressions, threading, collections, trees, sqlite, rabbitMQ, etc., and customizes or integrates various Util tool classes.

## Module Description

- cmd_util: Command-line execution tool
- conf_util: Configuration file reading tool
- date_util: Date and time tool
- file_util: File operation tool
- ffmpeg_util: Tool encapsulated based on ffmpeg
- log_util: Logging tool
- re_util: Regular expression tool
- secure_util: General encryption and decryption tool, including digest algorithm processing, etc.
- thread_util: Thread tool
- sqlite_util: SQLite database operation tool
- rabbitmq_util: rabbitmq encapsulation for more convenient use

For the specific usage of each module's Util, please refer to the description of each module or the unit test examples within the module (most modules contain unit test examples).

### Example 1: Instructions for using conf_util configuration

It supports json and yaml as configurations. Users are assumed to know the content and format of the configurations. For multi-level configurations, data can be directly accessed through the x.y.z.w format. The specific usage is as follows:

Special note: Both objects and arrays are operated directly through dots. For example, obj.children.name where "obj" is an object, "children" is an array, and "name" is an attribute of the objects in "children". When using obj.children.name, it will get the "name" values of all objects in the "children" array.

```rust
use mitoo::YamlWrapper;

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

    // Note: Here, "children" is an array. Whether it is an array or an object, they are operated on via dots.
    let x = wrapper.get("address.children.name");
    // address.children.name = [String("r"), String("s")]
    println!("address.children.name = {:?}", x);
    println!("=============================================================");

    // When used as a configuration, it is normally assumed that the user knows what is needed.
    let x = wrapper.get_one("address.x.y").as_str().unwrap();
    // address.x.y = hello, json!
    println!("address.x.y = {}", x);
}
```

### Example 2: SQLite database reading

```rust
// Note: Two FromSqliteRow are imported here: one is used for annotations, and the other is used in the expanded code.
use mitoo::{FromSqliteRow};
use mitoo::SqliteClient;
 
// Assume there is a users table; here is the definition of the corresponding struct:
#[derive(Debug)]
struct User {
    id: i64,
    name: String,
}

impl FromSqliteRow for User {
    fn from_sqlite_row(row: &sqlite::Row) -> Self {
        User {
            id: row.read::<i64, _>("id"),
            name: row.read::<&str, _>("name").to_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);
    }
}
```

### Example 3: RabbitMQ Tool

It is recommended that various configurations be set via yaml, and then read using ConfUtil to pass as parameters.

```rust
use mitoo::RabbitMQUtil;

// Produce data
#[tokio::test]
async fn publish() {
    // Initialize connection information
    let util = RabbitMQUtil::new("127.0.0.1", 5672, "guest", "guest").await;
    // Define a queue
    util.declare_queue("hello").await;
    // binding exchange-queue-routing_key
    util.queue_bind("amq.topic", "hello", "hello").await;
    // Send multiple messages in a loop
    for _i in 0..10 {
        let _x = util.publish("amq.topic", "hello", "Hello World, good!").await;
    }
    // During testing, it is necessary to ensure that the process remains alive for a certain period of time after publishing to allow messages to be delivered to RabbitMQ.
    tokio::time::sleep(Duration::from_secs(2)).await;
}

// Consume data
#[tokio::test]
async fn consume() {
    // Initialize connection information
    let util = RabbitMQUtil::new("127.0.0.1", 5672, "guest", "guest").await;

    // Consume the specified queue and specify a callback consumption function
    util.consume("hello", |basic_properties, content| async move {
        println!("===============>{}", String::from_utf8(content).unwrap());
        println!("===============>{:?}", basic_properties);
    }).await;
    
    // Wait for the consumption to end
    tokio::time::sleep(Duration::from_secs(5)).await;
}
```

