-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from sebastienrousseau/feature/mini-functions
feat(0.0.3): Added mini function UUID
- Loading branch information
Showing
8 changed files
with
243 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"cSpell.words": [ | ||
"datetime", | ||
"hasher", | ||
"nanos", | ||
"Seedable", | ||
"uuid" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,4 @@ | |
|
||
pub mod date; | ||
pub mod log; | ||
pub mod uuid; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |