Skip to main content

rok_utils/
lib.rs

1//! # rok-utils
2//!
3//! A Laravel/AdonisJS-inspired utility crate for Rust with zero-bloat, ergonomic helpers.
4//!
5//! ## Features
6//!
7//! - **String utilities** — case conversion, truncation, pluralization, fluent builder
8//! - **Array utilities** — map, filter, reduce, chunk, unique, group_by
9//! - **Error handling** — AdonisJS-style error codes with HTTP status
10//! - **Functional patterns** — pipe, compose, tap, retry, lazy, memoize
11//! - **Data utilities** — numbers, dates (feature: dates), hashing (feature: crypto), ids (feature: ids)
12//! - **Type guards** — JSON type guards (feature: json)
13//! - **File system helpers** — ensure_dir, find_files, copy_dir_all
14//! - **Path helpers** — normalize, stem_ext, with_extension
15//!
16//! ## Quick Start
17//!
18//! ```rust
19//! use rok_utils::{to_snake_case, Str};
20//!
21//! let snake = to_snake_case("HelloWorld");
22//! assert_eq!(snake, "hello_world");
23//!
24//! let result = Str::of("  hello world  ")
25//!     .trim()
26//!     .lower()
27//!     .replace(" ", "_")
28//!     .value();
29//! assert_eq!(result, "hello_world");
30//! ```
31//!
32//! ## Feature Flags
33//!
34//! | Feature | Description |
35//! |---------|-------------|
36//! | `full` | Enable all features |
37//! | `dates` | Enable date/time utilities |
38//! | `crypto` | Enable hashing and token generation |
39//! | `ids` | Enable UUID/ULID generation |
40//! | `json` | Enable JSON type guards and path access |
41//! | `random` | Enable random string generation |
42//!
43//! ## Example
44//!
45//! ```rust
46//! use rok_utils::{RokError, RokResultExt, Str};
47//!
48//! fn find_user(id: u64) -> Result<String, RokError> {
49//!     if id == 42 {
50//!         Ok("Alice".to_string())
51//!     } else {
52//!         Err(RokError::NotFound(format!("User #{id}")))
53//!     }
54//! }
55//!
56//! let result = find_user(42).context("Database query failed");
57//! ```
58
59pub mod arr;
60pub mod data;
61pub mod errors;
62pub mod fp;
63pub mod fs;
64pub mod path;
65pub mod result;
66pub mod str;
67pub mod string;
68pub mod types;
69
70pub use arr::*;
71pub use data::numbers;
72pub use errors::ResultExt as RokResultExt;
73pub use errors::{ResultExt, RokError};
74pub use fp::{apply, compose, memoize, or_default, pipe, retry, tap, Lazy};
75pub use fs::{
76    copy_dir_all, ensure_dir, find_files, is_dir, is_file, read_bytes, read_to_string, write_atomic,
77};
78pub use path::{normalize, stem_ext, with_extension};
79pub use str::{to_base64, Str};
80pub use string::{
81    char_at, contains, contains_all, doesnt_contain, ends_with, ensure_start, finish, invert_case,
82    is_alphanumeric, is_ascii, is_empty, is_url, lcfirst, length, mask, pad_both, pad_left,
83    pad_right, pluralize, position, pretty_duration, repeat, replace_first, replace_last, reverse,
84    slug, squish, starts_with, substr_count, to_camel_case, to_dot_case, to_headline,
85    to_kebab_case, to_lower, to_no_case, to_pascal_case, to_screaming_snake, to_sentence_case,
86    to_snake_case, to_title_case, to_upper, truncate, ucfirst, unwrap, word_count, wrap,
87};
88
89#[cfg(feature = "dates")]
90pub use data::dates::{
91    add_days, add_hours, diff_days, format_date, human_diff, now, parse_date, today, tomorrow,
92    yesterday,
93};
94
95#[cfg(feature = "crypto")]
96pub use data::hashing::{generate_token, hash_sha256, secure_compare, verify_sha256};
97
98#[cfg(feature = "ids")]
99pub use data::ids::{is_ulid, is_uuid, ulid, uuid_v4, uuid_v7};
100
101#[cfg(feature = "random")]
102pub use string::{password, random};
103
104#[cfg(feature = "json")]
105pub use str::escape_html;
106
107#[cfg(feature = "json")]
108pub use string::is_json;
109
110#[cfg(feature = "json")]
111pub use types::{
112    deep_equal, get_path, is_array, is_bool, is_null, is_number, is_object, is_string, set_path,
113};
114
115#[cfg(feature = "random")]
116pub use fp::shuffle;
117
118#[cfg(test)]
119mod tests {
120    #[test]
121    fn it_works() {
122        assert!(true);
123    }
124}