rustica/lib.rs
1//! # Rustica
2//!
3//! Rustica is a comprehensive Rust library that provides functional programming abstractions
4//! and utilities, enabling clean, composable, and type-safe code.
5//!
6//! ## Overview
7//!
8//! Functional programming emphasizes immutable data, first-class functions, and composable abstractions.
9//! Rustica brings these concepts to Rust with a focus on pragmatism and performance, providing:
10//!
11//! - Type-safe functional abstractions like `Functor`, `Applicative`, and `Monad`
12//! - Practical data types such as `Maybe`, `Either`, and `Validated`
13//! - Optics for data manipulation via `Lens` and `Prism`
14//! - Composable operations for error handling and data transformation
15//! - Advanced monad transformers: `StateT`, `ReaderT`, `ContT`
16//!
17//! ## Getting Started
18//!
19//! Add Rustica to your `Cargo.toml`:
20//!
21//! ```toml
22//! [dependencies]
23//! rustica = "0.9"
24//! ```
25//!
26//! Import common traits and types through the prelude:
27//!
28//! ```rust
29//! use rustica::prelude::*;
30//! ```
31//!
32//! ## Examples
33//!
34//! ### Basic Functor Usage
35//!
36//! ```rust
37//! use rustica::prelude::*;
38//!
39//! // Using fmap with Option
40//! let value: Option<i32> = Some(42);
41//! let doubled: Option<i32> = value.fmap(|x| x * 2);
42//! assert_eq!(doubled, Some(84));
43//!
44//! // With Either for error handling
45//! let success: Either<String, i32> = Either::right(42);
46//! let mapped: Either<String, String> = success.fmap(|n| n.to_string());
47//! assert_eq!(mapped.unwrap_right(), "42");
48//! ```
49//!
50//! ### Error Handling with Validated
51//!
52//! ```rust
53//! use rustica::datatypes::validated::Validated;
54//! use rustica::traits::applicative::Applicative;
55//! use rustica::traits::functor::Functor;
56//!
57//! fn validate_name(name: &str) -> Validated<Vec<String>, String> {
58//! if name.len() >= 2 {
59//! Validated::valid(name.to_string())
60//! } else {
61//! Validated::invalid(vec!["Name too short".to_string()])
62//! }
63//! }
64//!
65//! fn validate_email(email: &str) -> Validated<Vec<String>, String> {
66//! if email.contains('@') {
67//! Validated::valid(email.to_string())
68//! } else {
69//! Validated::invalid(vec!["Invalid email format".to_string()])
70//! }
71//! }
72//!
73//! // Collect all validation errors
74//! let name = validate_name("A");
75//! let email = validate_email("invalid-email");
76//!
77//! // Combine validations and format the result only when both are valid
78//! let format_user = |n: &String, e: &String| format!("User: {n}, Email: {e}");
79//! let combined = Validated::<Vec<String>, String>::lift2(format_user, &name, &email);
80//! assert!(combined.is_invalid());
81//! assert_eq!(combined.unwrap_invalid().len(), 2); // Both errors are collected
82//! ```
83//!
84//! ## Feature Flags
85//!
86//! Rustica provides several feature flags to customize the library for your needs:
87//!
88//! - `full`: Enables all features below
89//! - `pvec`: Includes persistent vector implementation
90//! - `async`: Includes async monad implementation
91//!
92//! ## Structure
93//!
94//! The library is organized into the following main components:
95//!
96//! - `traits`: Fundamental traits for functional programming concepts
97//! - `datatypes`: Implementations of various functional data types
98//! - `transformers`: Monad transformers and related utilities
99//! - `utils`: Utility functions and helpers for common operations
100//! - `prelude`: A convenient module that re-exports commonly used items
101//! - `pvec`: Persistent vector implementation (feature-gated)
102
103/// Core traits for functional programming abstractions.
104///
105/// This module contains the fundamental type classes and concepts from
106/// functional programming, implemented as Rust traits. Key traits include:
107///
108/// - `Functor`: Types that can be mapped over
109/// - `Applicative`: Functors with application capabilities
110/// - `Monad`: Monadic types with binding operations
111/// - Monoid: Types that can be combined with an identity element
112pub mod traits;
113
114/// Utility functions and helpers for common operations.
115///
116/// This module provides helper functions for error handling, function
117/// composition, and other common functional programming patterns.
118pub mod utils;
119
120/// Persistent vector implementation with structural sharing.
121///
122/// A high-performance, immutable vector implementation that preserves
123/// previous versions through structural sharing.
124pub mod pvec;
125
126/// Implementations of functional data types.
127///
128/// This module contains concrete implementations of common functional
129/// programming data types and containers, each with appropriate trait
130/// implementations.
131pub mod datatypes {
132 #[cfg(feature = "async")]
133 pub mod async_monad;
134 pub mod choice;
135 pub mod cont;
136 pub mod either;
137 pub mod id;
138 pub mod io;
139 pub mod iso_lens;
140 pub mod iso_prism;
141 pub mod lens;
142 pub mod maybe;
143 pub mod prism;
144 pub mod reader;
145 pub mod state;
146 pub mod validated;
147 pub mod wrapper;
148 pub mod writer;
149}
150
151/// Monad transformers and related utilities.
152///
153/// Monad transformers allow combining the effects of multiple monads,
154/// such as adding error handling to stateful computations or adding
155/// state to asynchronous operations.
156pub mod transformers;
157
158pub mod category;
159
160/// Convenient re-exports of commonly used items.
161pub mod prelude;