Vec Filter

Vec Filter is a Rust library to filter a vector of structs based on a query string. It allows you to specify filter conditions on struct fields with a simple query syntax. The library provides a custom derive macro, Filterable, to make your structs filterable with ease.

Table of Contents

Installation

Add the following to your Cargo.toml:

toml [dependencies] vec_filter = "0.1.0"

Usage

Deriving Filterable

To make your struct filterable, derive the Filterable trait:

```rust use vec_filter::Filterable;

[derive(Debug, Clone, Filterable)]

struct Person { name: String, age: u32, } ```

Query Syntax

This section describes the string query format that can be parsed by the library. The string queries are composed of a series of expressions that represent operations, values, and logical connectors. Expressions can be combined using parentheses to create more complex queries.

Operations

Values

Values can be of the following types:

Logical Connectors

Examples

Parentheses

To set the precedence of the operations, use parentheses. Expressions enclosed in parentheses will be evaluated first. If you want to group conditions, you can use parentheses to create more complex queries.

For example:

By using parentheses, you can build complex queries that combine multiple conditions with different levels of precedence to achieve precise filtering.

Notes

Example

Let's see how to filter a vector of Person structs using Vec Filter.

```rust use vecfilter::{Filterable, AST}; use vecfilter::Value;

[derive(Debug, Clone, PartialEq, Filterable)]

struct Person { name: String, age: u32, }

fn main() { let people = vec![ Person { name: "Alice".tostring(), age: 30 }, Person { name: "Bob".tostring(), age: 40 }, Person { name: "Charlie".to_string(), age: 50 }, ];

let ast = AST::And(
    Box::new(AST::Equals {
        field: "name",
        value: Value::String("Alice".to_string()),
    }),
    Box::new(AST::NotEquals {
        field: "age",
        value: Value::Int(30),
    }),
);

let filtered_people = ast.apply(&people);
println!("{:?}", filtered_people); // []

} ```

In this example, we want to find all people with the name "Alice" and age not equal to 30. The query is represented using an AST value. The apply method is then called with the people vector, and the filtered result is printed.

License

This project is licensed under the Apache 2.0 License.