10 releases

0.0.10 Dec 29, 2023
0.0.9 Dec 23, 2023
0.0.7 Jan 7, 2023
0.0.1 Dec 30, 2022

#444 in Parser implementations

Apache-2.0 OR MIT

135KB
1K SLoC

Common (CMN) logo

Mini Functions

A Rust library of highly performant utility and wrapper functions

Mini Functions

Made With Love Crates.io Lib.rs Docs.rs License Codecov

divider

WebsiteDocumentationReport BugRequest FeatureContributing Guidelines

Overview

Mini Functions is a highly performant utility and wrapper functions library for Rust that has been carefully designed with optimization and efficiency in mind. By providing convenient wrapper functions, our library aims to provide a high-level interface for common tasks while still leveraging the performance benefits of Rust under the hood. These utility functions serve as an essential toolkit for any Rust developer, and the library's design abstractions allow for easy integration into a variety of projects and applications.

These utility functions serve as an essential toolkit for any Rust developer, and the library's design abstractions allow for easy integration into a variety of projects and applications.

Table of Contents

Features

  • Built with Rust — A modern programming language that is well-suited for building high-performance, reliable, and secure systems.
  • High-level Utility Functions — A collection of high-level, abstracted functions for common tasks, such as string manipulation, file manipulation, and data parsing.
  • Wrapper Functions for Easy Access — Wrapper functions that provide a more convenient interface for accessing and using underlying Rust libraries or APIs.
  • Optimization and Performance Tools — Tools for optimizing and improving the performance of Rust code.
  • Multi-platform Support — Support for a variety of platforms, including desktop, mobile, and web.
  • Comprehensive Documentation and Examples — Documentation and examples to help developers understand and use the library effectively.
  • Regular Maintenance and Updates — Regular updates and maintenance to ensure the library stays up-to-date and reliable.

Functionality

Mini Functions is a library of functions for Rust that provides a collection of tools for working with various aspects of a Rust application. The mini-functions library consists of the following non-exhaustive functions:

Functions Description
Claims ⧉ The Claim functions are used to retrieve and manipulate information about claims. These functions are used to create and manage claims in JSON Web Tokens (JWT) and JSON Web Signatures (JWS).
Common ⧉ The Common functions are used to retrieve and manipulate information about common data types.
Date ⧉ The Date and time functions are used to retrieve and manipulate information about dates and times.
Errors ⧉ The Error functions are used to retrieve and manipulate information about errors.
Hash ⧉ The Hash functions are used to retrieve and manipulate information about hashes.
Logs ⧉ The Log functions are used to retrieve and manipulate information about logs.
JWT ⧉ The Jot functions are used to retrieve and manipulate information about JSON Object Tokens (JOT).
MD5 ⧉ The MD5 functions are used to retrieve and manipulate information about MD5.
QR ⧉ The QR functions are used to retrieve and manipulate information about QR codes.
Random ⧉ The Random functions are used to retrieve and manipulate information about random data.

See Documentation for full API details.

divider

Getting Started

It takes just a few minutes to get up and running with mini-functions.

Requirements

The minimum supported Rust toolchain version is currently Rust 1.71.1 or later (stable).

Installation

To install mini-functions, you need to have the Rust toolchain installed on your machine. You can install the Rust toolchain by following the instructions on the Rust website ⧉.

Once you have the Rust toolchain installed, you can install mini-functions using the following command:

cargo install mini-functions

Divider

Usage

To use the mini-functions library in your project, add the following to your Cargo.toml file:

[dependencies]
mini-functions = "0.0.10"

Add the following to your main.rs file:

extern crate mini_functions;
use mini_functions::*;

then you can use the functions in your application code.

divider

Examples

The mini-functions library comes with a set of examples that demonstrate how to use the library. You can find the examples in the examples directory.

To run the examples, use the following command:

cargo run --example <example-name>

divider

Example 1: Working with JWT Claims

The mini_functions crate provides a Claims struct for working with JWT claims.

It contains the following functions:

  • Setting Claims with set_claim to add claims.
  • Getting Claims with get_claim to retrieve a claim value.
  • Removing Claims with remove_claim to remove a claim.

Here is a full example:

let mut claims = Claims::new();

claims.set_claim("iss", "https://example.com");
claims.set_claim("admin", "true");

let admin = claims.get_claim("admin").unwrap();

claims.remove_claim("admin");

This allows setting, retrieving, and removing JWT claims conveniently.

To run the JWT Claims example, use the following command:

cargo run --example example_claims

divider

Example 2: Working with Mathematical Constants

The mini_functions crate provides access to common mathematical constants through the Constants struct and cmn_constants macro.

It contains the following functions:

  • Constants::new() - Create a new Constants instance
  • constants() - Get the full list of constants
  • constant(name) - Lookup a constant by name
  • cmn_constants! - Import constants into scope

Here is a full example:

use cmn::{constants::{Constant, ConstantValue}, cmn_constants};

let c = Constants::new();
let euler = c.constant("EULER").unwrap();

cmn_constants! {
    PI = cmn::constants::PI,
}

println!("Euler's constant: {euler}"); 
println!("Pi: {PI}");

This allows convenient access to mathematical constants.

To run the constants example:

cargo run --example example_constants

divider

Example 3: Working with Date and Time

The mini_functions crate provides a DateTime struct for working with dates and times.

It contains the following functions:

  • DateTime::now - Get current date/time
  • DateTime::new - Create a DateTime with default (UTC) timezone
  • DateTime::new_with_tz - Create a DateTime with custom timezone
  • is_valid_day - Check if a day value is valid
  • next_day/previous_day - Get next/previous day
  • from_str - Parse a date/time string
  • relative_delta - Apply a delta to the DateTime

Here is an example:

let now = DateTime::now();

let tomorrow = now + chrono::Duration::days(1);
let yesterday = now - chrono::Duration::days(1);

println!("Today: {now}");
println!("Tomorrow: {tomorrow}"); 
println!("Yesterday: {yesterday}");

This allows convenient date/time handling.

To run the date/time example:

cargo run --example example_date

divider

Example 4: Error Handling

The mini_functions crate provides error handling functionality through the ErrorType enum.

It contains the following functions:

  • ErrorType::new - Create new error type
  • new_subtype - Create error subtype

Here is an example:

use mini_functions::errors::common::ErrorType;

let error = ErrorType::new("illegal_argument");
let sub_error = error.new_subtype("invalid_value");

println!("Main error: {error:?}");
println!("Sub-error: {sub_error:?}");

This allows simple error handling with custom types and subtypes.

To run the errors example:

cargo run --example example_errors

divider

Example 5: Password Hashing

The mini_functions crate provides password hashing and verification functions through the Hash struct.

It contains the following functions:

  • Hash::new_{algo} - Generate a hash for a password
  • set_password - Update password for a hash
  • verify - Verify a password against a hash
  • to_string - Convert hash to a string

Here is an example:

use mini_functions::hash::Hash;

let hash = Hash::new_argon2i("mypassword");
let is_valid = hash.verify("mypassword");

let updated_hash = hash.set_password("newpassword");
let new_is_valid = updated_hash.verify("newpassword");

This allows generating and verifying password hashes conveniently.

To run the password hashing example:

cargo run --example example_hash

divider

Example 7: Logging

The mini_functions crate provides application logging functionality through the Log struct.

It contains functions like:

  • Log::new - Create a new log entry
  • LogFormat - Supported log formats

Here is an example of logging events with different formats:

use mini_functions::logs::{Log, LogFormat, LogLevel};

let log_json = Log::new(
    "message-id", 
    "2023-01-01T12:00:00Z",
    LogLevel::Info,
    "AppEvent",
    "User logged in",
    LogFormat::JSON
);

let log_clf = Log::new(
    "message-id",
    "2023-01-01T12:00:00Z",
    LogLevel::Info,
    "AuthEvent",
    "User login successful",
    LogFormat::CLF
);

This allows flexible logging in various text and JSON formats.

To run the logging example:

cargo run --example example_logs

divider

Example 8: MD5 Hashing

The mini_functions crate provides MD5 hash generation functionality through the MD5 struct.

It contains functions like:

  • MD5::hexdigest - Generate MD5 hash for input
  • MD5::new - Create MD5 hasher instance
  • update - Update hasher with new input
  • finalize - Obtain final hash

Here is an example of hashing different input sources:

use mini_functions::md5::MD5;

let digest = MD5::hexdigest("input string");

let mut hasher = MD5::new();
hasher.update(&[1, 2, 3]);
let hash = hasher.finalize();

This allows flexible hashing of strings, byte arrays, files.

To run the MD5 example:

cargo run --example example_md5

divider

Example 9: QR Codes

The mini_functions crate provides QR code generation and manipulation functionality through the QRCode struct.

It contains functions like:

  • QRCode::from_string - Generate QR code from text
  • to_png - Convert to PNG image
  • colorize - Colorize the QR code
  • resize - Resize image

And macros like:

  • qr_code_to! - QR code generation macro

Here is an example:

use mini_functions::qr;

let qr_code = qr::QRCode::from_string("https://example.com");
let img = qr_code.to_png(512);

qr::save_png(&img, "qr.png");

This allows convenient QR code creation, manipulation and saving.

To run the QR code example:

cargo run --example example_qr

divider

Example 10: Random Number Generation

The mini_functions crate provides random number generation functionality through the Random struct and associated functions.

It contains functions like:

  • Random::new - Create random number generator
  • bool - Random boolean
  • int - Random integer
  • float - Random float
  • bytes - Random byte vector

And macros like:

  • rand_int! - Random integer in range
  • rand_bool! - Random boolean with probability

Here is an example:

use mini_functions::random::{Random, rand_int};

let mut rng = Random::new();

let rand_num = rand_int!(rng, 0, 10);
let rand_bool = rand_bool!(rng, 0.5);

This allows convenient random number generation.

To run the random example:

cargo run --example example_random

divider

Platform support

mini-functions is supported and tested on the following platforms:

Tier 1 platforms

Operating System Target Description
Linux aarch64-unknown-linux-gnu 64-bit Linux systems on ARM architecture
Windows i686-pc-windows-gnu 32-bit Windows systems using the GNU toolchain
Windows i686-pc-windows-msvc 32-bit Windows systems using the Microsoft Visual C toolchain
Linux i686-unknown-linux-gnu 32-bit Linux systems (kernel 3.2+, glibc 2.17+)
macOS x86_64-apple-darwin 64-bit macOS systems (10.7 Lion or later)
Windows x86_64-pc-windows-gnu 64-bit Windows systems using the GNU toolchain
Windows x86_64-pc-windows-msvc 64-bit Windows systems using the Microsoft Visual C toolchain
Linux x86_64-unknown-linux-gnu 64-bit Linux systems (kernel 2.6.32+, glibc 2.11+)

Tier 2 platforms

Operating System Target Description
Linux aarch64-apple-darwin 64-bit macOS on Apple Silicon
Windows aarch64-pc-windows-msvc 64-bit Windows on ARM architecture using the Microsoft Visual C toolchain
Linux aarch64-unknown-linux-musl 64-bit Linux on ARM architecture with musl libc
Linux arm-unknown-linux-gnueabi ARMv6 Linux systems (kernel 3.2, glibc 2.17)
Linux arm-unknown-linux-gnueabihf ARMv7 Linux systems, hardfloat (kernel 3.2, glibc 2.17)
Linux armv7-unknown-linux-gnueabihf ARMv7 Linux systems, hardfloat (kernel 3.2, glibc 2.17)
Linux powerpc-unknown-linux-gnu PowerPC Linux systems (kernel 3.2, glibc 2.17)
Linux powerpc64-unknown-linux-gnu PowerPC64 Linux systems (kernel 3.2, glibc 2.17)
Linux powerpc64le-unknown-linux-gnu PowerPC64le Linux systems (kernel 3.2, glibc 2.17)
Linux riscv64gc-unknown-linux-gnu RISC-V Linux systems (kernel 3.2, glibc 2.17)
Linux s390x-unknown-linux-gnu s390x Linux systems (kernel 3.2, glibc 2.17)
Linux x86_64-unknown-freebsd 64-bit FreeBSD systems on x86-64 architecture
Linux x86_64-unknown-linux-musl 64-bit Linux systems (kernel 2.6.32+, musl libc)

The GitHub Actions ⧉ shows the platforms in which the mini-functions library tests are run.

divider

Documentation

Info: Please check out our website ⧉ for more information. You can find our documentation on docs.rs ⧉, lib.rs ⧉ and crates.io ⧉.

Semantic Versioning Policy

For transparency into our release cycle and in striving to maintain backward compatibility, mini-functions follows semantic versioning ⧉.

divider

License

The project is licensed under the terms of Apache License, Version 2.0 and the MIT license.

divider

Contribution

We welcome all people who want to contribute. Please see the contributing instructions ⧉ for more information.

Contributions in any form (issues, pull requests, etc.) to this project must adhere to the Rust's Code of Conduct ⧉.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

divider

Acknowledgements

A big thank you to all the awesome contributors of mini-functions ⧉ for their help and support.

A special thank you goes to the Rust Reddit ⧉ community for providing a lot of useful suggestions on how to improve this project.

Dependencies

~26–38MB
~472K SLoC