Expand description
§Rustica
Rustica is a comprehensive Rust library that provides functional programming abstractions and utilities, enabling clean, composable, and type-safe code.
§Overview
Functional programming emphasizes immutable data, first-class functions, and composable abstractions. Rustica brings these concepts to Rust with a focus on pragmatism and performance, providing:
- Type-safe functional abstractions like
Functor,Applicative, andMonad - Practical data types such as
Maybe,Either, andValidated - Optics for data manipulation via
LensandPrism - Composable operations for error handling and data transformation
- Advanced monad transformers:
StateT,ReaderT,ContT
§Getting Started
Add Rustica to your Cargo.toml:
[dependencies]
rustica = "0.11.1"Import common traits and types through the prelude:
use rustica::prelude::*;§Examples
§Basic Functor Usage
use rustica::prelude::*;
// Using fmap with Option
let value: Option<i32> = Some(42);
let doubled: Option<i32> = value.fmap(|x| x * 2);
assert_eq!(doubled, Some(84));
// With Either for error handling
let success: Either<String, i32> = Either::right(42);
let mapped: Either<String, String> = success.fmap(|n| n.to_string());
assert_eq!(mapped.unwrap_right(), "42");§Error Handling with Validated
use rustica::datatypes::validated::Validated;
use rustica::traits::applicative::Applicative;
use rustica::traits::functor::Functor;
fn validate_name(name: &str) -> Validated<String, String> {
if name.len() >= 2 {
Validated::valid(name.to_string())
} else {
Validated::invalid("Name too short".to_string())
}
}
fn validate_email(email: &str) -> Validated<String, String> {
if email.contains('@') {
Validated::valid(email.to_string())
} else {
Validated::invalid("Invalid email format".to_string())
}
}
// Collect all validation errors
let name = validate_name("A");
let email = validate_email("invalid-email");
// Combine validations and format the result only when both are valid
let format_user = |n: &String, e: &String| format!("User: {n}, Email: {e}");
let combined = Validated::<String, String>::lift2(format_user, &name, &email);
assert!(combined.is_invalid());
assert_eq!(combined.unwrap_invalid().len(), 2); // Both errors are collected§Feature Flags
Rustica provides several feature flags to customize the library for your needs:
full: Enables all optional features (async+serde)async: Enables async monad implementation (AsyncM)serde: Enables serialization/deserialization support
§Structure
The library is organized into the following main components:
traits: Fundamental traits for functional programming conceptsdatatypes: Implementations of various functional data typestransformers: Monad transformers and related utilitiescategory: Category theory abstractionserror: Composable error handling utilitiespvec: Persistent vector implementation with structural sharingutils: Utility functions and helpers for common operationsprelude: A convenient module that re-exports commonly used items
Modules§
- category
- Category theory abstractions.
- datatypes
- Implementations of functional data types.
- error
- Error handling utilities.
- prelude
- Convenient re-exports of commonly used items.
- pvec
- Persistent vector implementation with structural sharing.
- traits
- Core traits for functional programming abstractions.
- transformers
- Monad transformers and related utilities.
- utils
- Utility functions and helpers for common operations.
Macros§
- compose
- Macro for composing multiple functions with type annotations.
- context
- Creates a lazy error context that is only evaluated when an error occurs.
- function
- Macro for creating named function morphisms with type annotations.
- pipe
- Macro for creating function pipelines using comma-separated syntax.
- pvec
- Convenience macro for creating persistent vectors.