Data Utilities
Utilities for numbers, dates, IDs, and cryptographic operations.
Numbers
format_number
Format numbers with thousand separators:
#![allow(unused)]
fn main() {
use rok_utils::data::numbers::format_number;
assert_eq!(format_number(1234567.89, 2, ','), "1,234,567.89");
assert_eq!(format_number(1000.00, 0, ','), "1,000");
}
round, ceil, floor
#![allow(unused)]
fn main() {
use rok_utils::data::numbers::{round, ceil, floor};
assert_eq!(round(3.14159, 2), 3.14);
assert_eq!(ceil(3.001, 0), 4.0);
assert_eq!(floor(3.999, 0), 3.0);
}
clamp
Constrain a value within a range:
#![allow(unused)]
fn main() {
use rok_utils::data::numbers::clamp;
assert_eq!(clamp(5.0, 0.0, 10.0), 5.0);
assert_eq!(clamp(-5.0, 0.0, 10.0), 0.0);
assert_eq!(clamp(15.0, 0.0, 10.0), 10.0);
}
Requires the
datesfeature flag.
Dates
today / now
#![allow(unused)]
fn main() {
use rok_utils::{today, now};
let date = today();
let timestamp = now();
}
add_days / add_hours
#![allow(unused)]
fn main() {
use rok_utils::{add_days, today};
let tomorrow = add_days(&today(), 1);
}
format_date
#![allow(unused)]
fn main() {
use rok_utils::{now, format_date};
let formatted = format_date(&now(), "%Y-%m-%d %H:%M");
}
IDs
uuid_v4
Generate a random UUID:
#![allow(unused)]
fn main() {
use rok_utils::uuid_v4;
let uuid = uuid_v4();
// "550e8400-e29b-41d4-a716-446655440000"
}
uuid_v7
Generate a time-ordered UUID:
#![allow(unused)]
fn main() {
use rok_utils::uuid_v7;
let uuid = uuid_v7();
}
is_uuid
Validate UUID strings:
#![allow(unused)]
fn main() {
use rok_utils::is_uuid;
assert!(is_uuid("550e8400-e29b-41d4-a716-446655440000"));
assert!(!is_uuid("invalid"));
}
Requires the
idsfeature flag.
Hashing
hash_sha256
#![allow(unused)]
fn main() {
use rok_utils::hash_sha256;
let hash = hash_sha256("password");
// "5e884898da28047d9169e02e..."
}
verify_sha256
#![allow(unused)]
fn main() {
use rok_utils::{hash_sha256, verify_sha256};
let hash = hash_sha256("password");
assert!(verify_sha256("password", &hash));
assert!(!verify_sha256("wrong", &hash));
}
generate_token
Generate a secure random token:
#![allow(unused)]
fn main() {
use rok_utils::generate_token;
let token = generate_token(32);
// 32 bytes as hex string
}
secure_compare
Constant-time comparison (prevents timing attacks):
#![allow(unused)]
fn main() {
use rok_utils::secure_compare;
assert!(secure_compare("secret", "secret"));
assert!(!secure_compare("secret", "Secret"));
}
Requires the
cryptofeature flag.