Crate criterium

Crate criterium 

Source
Expand description

Criterium is a mini-framework to make implementing dynamic conditions and database queries easier.

It does so by supplying the needed datastructures that can be compiled to SQL and a list of values or directly matched against a given value.

Multiple Criteria can also be combined using Boolean logic (see CriteriumChain).

Dynamic also means configurable, Criterium integrates itself with Serde.

You can find the souce code and more documentation on codeberg.org/unobtanium/criterium.

§Criterium Concepts

§Criterium

A Criterium is a datastructure that describes a condition that matches against one or more values, the comparison either results in a match or in a not-match.

The comparison can either be one directly by the Criterium itself using the DirectMatch trait its criterium_match() method or by compiling the Criterium to SQL and using it to query a database.

The Criterium crate provides pre-made Implementations of for numbers, strings and boolean values, you can use these to compose more complex Criteria for your datastructures. The supported features depend on which interfaces you implement for those.

§Null/None Values

Criterium uses null, undefined and None and “unset” interchangably, all of them mean that the value is not defined and therefore not equals to any set value. Other Comparisons that imply a presence of a value on the other side of the operator must fail. (Similar to how SQL works)

The primitives have an IsNone matcher to detect the absence or undefinedness of a value.

A null equals null situaion should be avaided. Converting options of values to criteria converts them to an Equals for Some(…) and an IsNone for None inputs.

Whenever possible (without breaking semantics of course) criterium tries to convert None and Unknown results into boolean ones, i.e. when … and true and … or false situations arise. Information not being present is different from an error.

§Chain

A CriteriumChain is a way to connect multiple criteria with each other using boolean operators.

Usually complex queries take in a CriteriumChain with the containing type set to a custom enum that represents all the possible Criteria that are available.

While chains can be complex, they don’t have to be. They could be as simple as being a container for a single citerium.

Chains follow an intentionally open data-structure, this means one can add custom functionality by implementing own traits.

§Inverting

Inverting is used throughout Criterium to describe the process of negating the match-result, either by adding or removing a Not statement.

(in most progamming languages the boolean operator for this is not or !.)

Types that support inverting directly have invert() and invert_if(bool) methods.

Please note that inverting a comparison against a nullable value result in a match (as in true) when comparing against null.

§Traits to implement on your criteria

By default, the CriteriumChain accepts any data type as the criterium it contains.

However, Criterium is only really useful the criteria implement one or more of the following traits:

  • DirectMatch - If you want to compare against a rust data structure directly. (With known or unknown result)
  • Also see the rusqlite module for traits that are needed to interoperate with rusqlite.

§Feature flags

  • rusqlite - Enable compiling to SQLite SQL and rusqlite values.
  • chrono - Allows using DateTime in place of numbers, they will be interpreted as a unix utc timestamp.
  • serde - Enables serializing and deserialzing. (See the documentation_serde module)

Re-exports§

pub use boolean::BooleanCriterium;
pub use chain::builder::CriteriumChainBuilder;
pub use chain::ChainInto;
pub use chain::CriteriumChain;
pub use chain::CriteriumChainList;
pub use number::NumberCriterium;
pub use string::StringCriterium;
pub use direct_match::DirectMatch;
pub use tag::TagCriterium;

Modules§

boolean
A Boolean type for Criterium
chain
Combines multiple criteria.
direct_match
Matching without a database.
documentation_serde
number
Matches against numeric values.
rusqlite
Rusqlite (SQLite) integration for criterium.
search
Full Text Search Criterium
sql
General SQL helpers.
string
Matches against textual values.
tag
For matching one to many relationships

Structs§

LogicPath
Encodes information on how the syntax tree below a given query is structured.

Enums§

BooleanJoiner
A way to describe how nultiple elements are joined together in a chain of multiple.