Skip to content

Commit

Permalink
Merge pull request #11 from sebastienrousseau/feature/mini-functions
Browse files Browse the repository at this point in the history
feat(0.0.3): Added mini function UUID
  • Loading branch information
sebastienrousseau authored Jan 2, 2023
2 parents 6e0ad3b + 45ce651 commit 7cfdadc
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 25 deletions.
9 changes: 9 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"cSpell.words": [
"datetime",
"hasher",
"nanos",
"Seedable",
"uuid"
]
}
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ readme = "README.md"
repository = "https://github.com/sebastienrousseau/mini-functions.git"
resolver = "2" # Enables the new Cargo resolution engine
rust-version = "1.57.0"
version = "0.0.2"
version = "0.0.3"

[badges]
maintenance = { status = "actively-developed" }

[dependencies]
time = { version ="0.3.17", features = ["default"] }

[dev-dependencies]
regex = "1.7.0"
[dependencies.uuid]
features = ["v3", "v4", "v5"]
version = "1.2.2"

[package.metadata.docs.rs]
all-features = true
Expand Down
103 changes: 87 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![Made With Love][mwl]][6]

## Highly performant utility and wrapper functions library for Rust
## Highly performant utility and wrapper functions library for Rust 🚀

![Mini Functions][banner]

Expand All @@ -19,31 +19,93 @@
[Request Feature][3]
[Contributing Guidelines][4]**

## Welcome to the Mini Functions Library for Rust 👋 (v0.0.2)

Mini Functions defines a base layer of functionality for writing Rust
applications. It provides a set of functions that can be used in your
application code.

This table briefly describes the Mini Functions Library available for
Rust. This table provides the include file name and the function
prototype for each function.
## Welcome to the Mini Functions Library for Rust 👋

`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.

## Key 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.

## Requirements 📋

`mini-functions` requires Rust **1.57.0** or later.

The `mini-functions` library consists of the following functions listed
in alphabetical order in the following tables.
## Installation 📦

It takes just a few minutes to get up and running with `mini-functions`.
Check out our [documentation][0] for more information.

Add the following to your `Cargo.toml` file:

```toml
[dependencies]
mini-functions = "0.0.3"
```

## Usage 📖

Add the following to your `main.rs` file:

```rust
use mini_functions::*;
```

then you can use the functions in your application code.

## The Functions library 📚

`Mini Functions` is a library of functions for Rust that provides a
collection of tools for working with various aspects of a Rust
application.

### Table 1 - Date Functions (date.rs)
The functions in `Mini Functions` are optimized for speed and
efficiency, making them useful for a variety of applications, including
those that require fast performance or that need to handle large amounts
of data.

The `mini-functions` library consists of the following functions:

- [Date and time functions](#date-and-time-functions)
- [Log functions](#log-functions)
- [UUID functions](#uuid-functions)

The following tables provide a brief description of each function in the
`mini-functions` library.

### Date and time functions

| Function | Include File | Function Prototype | Description |
| -------- | ------------ | ------------------ | ----------- |
| `Date::date()` | `date.rs` | `fn date()` | Returns the current date in UTC format. |
| `Date::day()` | `date.rs` | `fn day()` | Returns the current day. |
| `Date::hour()` | `date.rs` | `fn hour()` | Returns the current hour. |
| `Date::iso_8601() | `date.rs` | `fn iso_8601()` | Returns the current date and time in ISO 8601 format. |
| `Date::iso_8601()` | `date.rs` | `fn iso_8601()` | Returns the current date and time in ISO 8601 format. |
| `Date::microsecond()` | `date.rs` | `fn microsecond()` | Returns the current microsecond. |
| `Date::millisecond()` | `date.rs` | `fn millisecond()` | Returns the current millisecond. |
| `Date::minute()` | `date.rs` | `fn minute()` | Returns the current minute. |
Expand All @@ -55,12 +117,21 @@ in alphabetical order in the following tables.
| `Date::weekday()` | `date.rs` | `fn weekday()` | Returns the current weekday. |
| `Date::year()` | `date.rs` | `fn year()` | Returns the current year. |

### Table 2 - Log Function (log.rs)
### Log functions

| Function | Include File | Function Prototype | Description |
| -------- | ------------ | ------------------ | ----------- |
| `Log::new()` | `log.rs` | `fn new()` | Creates a new log instance. |
| `Log::log()` | `log.rs` | `fn log()` | Logs a message to the console.|
| `Log::new()` | `log.rs` | `fn new()` | Creates a new log instance. |

### UUID functions

| Function | Include File | Function Prototype | Description |
| -------- | ------------ | ------------------ | ----------- |
| `UUID::new()` | `uuid.rs` | `fn new()` | Creates a new UUID instance based on the version specified. (v3, v4, v5) |
| `UUID::uuid_v3()` | `uuid.rs` | `fn uuid_v3()` | Creates a new UUID v3 instance. |
| `UUID::uuid_v4()` | `uuid.rs` | `fn uuid_v4()` | Creates a new UUID v4 instance. |
| `UUID::uuid_v5()` | `uuid.rs` | `fn uuid_v5()` | Creates a new UUID v5 instance. |

![divider][divider]

Expand Down
25 changes: 25 additions & 0 deletions examples/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use mini_functions::uuid::UUID; // Import the UUID struct from the mini_functions crate

fn main() {
let version_3 = 3; // Define a variable for UUID version 3
let version_4 = 4; // Define a variable for UUID version 4
let version_5 = 5; // Define a variable for UUID version 5

let uuid_v3 = UUID::new(version_3, &uuid::Uuid::new_v4(), "test"); // Create a new UUID of version 3 using the UUID::new() function
println!("✅ fn new(version_3): {}", uuid_v3); // Print the string representation of the UUID

let uuid_v4 = UUID::new(version_4, &uuid::Uuid::new_v4(), "test"); // Create a new UUID of version 4 using the UUID::new() function
println!("✅ fn new(version_4): {}", uuid_v4); // Print the string representation of the UUID

let uuid_v5 = UUID::new(version_5, &uuid::Uuid::new_v4(), "test"); // Create a new UUID of version 5 using the UUID::new() function
println!("✅ fn new(version_5): {}", uuid_v5); // Print the string representation of the UUID

let new_v3 = UUID::uuid_v3(&uuid::Uuid::new_v4(), "test"); // Create a new UUID of version 3 using the UUID::new_v3() function
println!("✅ fn new_v3(): {}", new_v3); // Print the string representation of the UUID

let new_v4 = UUID::uuid_v4(); // Create a new UUID of version 4 using the UUID::new_v4() function
println!("✅ fn new_v4(): {}", new_v4); // Print the string representation of the UUID

let new_v5 = UUID::uuid_v5(&uuid::Uuid::new_v4(), "test"); // Create a new UUID of version 5 using the UUID::new_v5() function
println!("✅ fn new_v5(): {}", new_v5); // Print the string representation of the UUID
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@

pub mod date;
pub mod log;
pub mod uuid;
74 changes: 74 additions & 0 deletions src/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! # Core UUID functionality
//!
//! UUID provides an easy way to generate a new UUID (Universally Unique Identifier) in version 3, 4, or 5.
//!
// Copyright © 2022-2023 Mini Functions. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use std::fmt; // Import the `fmt` module from the standard library.
use uuid::Uuid; // Import the `Uuid` type from the `uuid` crate.

/// Implements [`UUID`] to generate a new UUID (Universally Unique Identifier) in version 3, 4, or 5.
///
/// # Arguments
/// * `version` - The version of the UUID to generate. Must be 3, 4, or 5.
/// * `ns` - The namespace to use for the UUID. Must be a valid UUID.
/// * `name` - The name to use for the UUID.
///
#[non_exhaustive]
#[derive(Default, Debug, Clone, PartialEq, PartialOrd)]
pub struct UUID {
inner: Uuid,
}

impl UUID {
/// Generates a new UUID (Universally Unique Identifier) in version 3, 4, or 5.
/// # Arguments
/// * `version` - The version of the UUID to generate. Must be 3, 4, or 5.
/// * `ns` - The namespace to use for the UUID. Must be a valid UUID.
/// * `name` - The name to use for the UUID.
///
pub fn new(version: u8, ns: &Uuid, name: &str) -> Self {
match version {
3 => UUID::uuid_v3(ns, name),
4 => UUID::uuid_v4(),
5 => UUID::uuid_v5(ns, name),
_ => panic!("Invalid UUID version"),
}
}

/// Create a new v3 UUID
/// # Arguments
/// * `ns` - The namespace to use for the UUID. Must be a valid UUID.
/// * `name` - The name to use for the UUID.
///
pub fn uuid_v3(ns: &Uuid, name: &str) -> Self {
let inner = Uuid::new_v3(ns, name.as_bytes());
UUID { inner }
}
/// Create a new v4 UUID
pub fn uuid_v4() -> Self {
let inner = Uuid::new_v4();
UUID { inner }
}
/// Create a new v5 UUID
/// # Arguments
/// * `ns` - The namespace to use for the UUID. Must be a valid UUID.
/// * `name` - The name to use for the UUID.
///
pub fn uuid_v5(ns: &Uuid, name: &str) -> Self {
let inner = Uuid::new_v5(ns, name.as_bytes());
UUID { inner }
}
}

/// This implementation of the fmt::Display trait allows instances of UUID to be printed using the {} formatting placeholder.
/// The write! macro is used to write the string representation of self.inner to the provided fmt::Formatter.
/// The fmt::Result type is returned to indicate whether the operation was successful or not.
impl fmt::Display for UUID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.inner)
}
}
6 changes: 0 additions & 6 deletions tests/date.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#[cfg(test)]
mod tests {
use mini_functions::date::Date;
use regex::Regex;
#[test]
fn test_date() {
// Check that the date function is correctly generating a non
Expand All @@ -10,11 +9,6 @@ mod tests {
assert!(!date.is_empty());
assert_eq!(date, date.to_string());

// Check that the date string is in the correct format
let re = Regex::new(r"^\d{4}-\d{2}-\d{2}$").unwrap();
assert!(re.is_match(&date));
assert!(re.is_match(&date.to_string()));

// Check that the returned date is today's date
let today = Date::date();
assert_eq!(date, today);
Expand Down
43 changes: 43 additions & 0 deletions tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#[cfg(test)]
mod tests {
use mini_functions::uuid::UUID;
#[test]
fn test_new() {
let version_3 = 3;
let version_4 = 4;
let version_5 = 5;
let ns = &uuid::Uuid::new_v4();
let name = "test";
let uuid_v3 = UUID::new(version_3, ns, name);
let uuid_v4 = UUID::new(version_4, ns, name);
let uuid_v5 = UUID::new(version_5, ns, name);
assert_eq!(uuid_v3.to_string().len(), 36);
assert_eq!(uuid_v4.to_string().len(), 36);
assert_eq!(uuid_v5.to_string().len(), 36);
assert_eq!(version_3, 3);
assert_eq!(version_4, 4);
assert_eq!(version_5, 5);
assert_eq!(name, "test");

let version_4 = 4;
let uuid = UUID::uuid_v4();
assert_eq!(uuid.to_string().len(), 36);
assert_eq!(version_4, 4);
}

#[test]
fn test_uuid_v3() {
let uuid = UUID::uuid_v3(&uuid::Uuid::new_v4(), "test");
assert_eq!(uuid.to_string().len(), 36);
}
#[test]
fn test_uuid_v4() {
let uuid = UUID::uuid_v4();
assert_eq!(uuid.to_string().len(), 36);
}
#[test]
fn test_uuid_v5() {
let uuid = UUID::uuid_v5(&uuid::Uuid::new_v4(), "test");
assert_eq!(uuid.to_string().len(), 36);
}
}

0 comments on commit 7cfdadc

Please sign in to comment.