🦀 TypeSafe Builder 🦀
The Ultimate Builder Pattern Implementation Powered by Rust's Type System
Eliminate bugs at the type level and revolutionize your development experience
✨ Why TypeSafe Builder?
Traditional builder patterns can't detect missing required fields until runtime. TypeSafe Builder leverages Rust's powerful type system to verify all constraints at compile time.
// ❌ Traditional builder - potential runtime errors
let user = new
.name
.build?; // Compiles even with missing required fields
// ✅ TypeSafe Builder - compile-time safety guarantee
let user = new
.with_name
.with_email // Compile error if email is required
.build; // Always guaranteed to succeed
🎯 Key Features
🔒 Type-Level Constraint System
- Required Fields - Completely prevent missing required field configuration
- Optional Fields - Freely configurable fields
- Conditional Requirements - Express dynamic dependencies at the type level
- Complex Logic - Support for AND/OR/NOT operators in complex conditional expressions
⚡ Performance Characteristics
- Zero Runtime Cost - All validation completed at compile time
🛡️ Safety Guarantees
- No Panic - Complete elimination of runtime panics
📦 Quick Start
[]
= "*.*.*" # Replace with the actual version
use *;
// Type-safe builder pattern
let user = new
.with_name
.with_age
.build;
🚀 Advanced Features
1️⃣ Conditional Required Fields
use *;
// ✅ Compiles successfully
let account1 = new.build;
// ✅ Compiles successfully
let account2 = new
.with_email
.with_email_verified
.build;
// ❌ Compile error: email_verified is not set
// let account3 = AccountBuilder::new()
// .with_email("user@example.com".to_string())
// .build();
2️⃣ Conditional Optional Fields
use *;
// ✅ When debug_mode is not set, log_level is required
let config1 = new
.with_log_level
.build;
// ✅ When debug_mode is set, log_level is optional
let config2 = new
.with_debug_mode
.build;
3️⃣ Complex Conditional Logic
use *;
// ✅ All dependencies satisfied (auth + HTTPS)
let client1 = new
.with_use_auth
.with_use_https
.with_api_key
.with_secret
.with_certificate
.build;
// ✅ Insecure configuration with warning
let client2 = new
.with_use_auth
.with_use_https
.with_insecure_warning
.build;
// ✅ Using fallback token when API key is not set
let client3 = new
.with_use_auth
.with_secret
.with_fallback_token
.build;
4️⃣ Negation Operator Support
use *;
// ✅ Warning configuration for non-SSL usage
let db = new
.with_use_ssl
.with_warning_message
.build;
5️⃣ Custom Builder Name
use *;
// Customize the builder name
// Use the customized builder name
let user = new
.with_name
.build;
🔧 Error Handling
Compile-Time Error Examples
// ❌ Compile error
let user = new.build;
// ^^^^^
// error: no method named `build` found for struct `UserBuilder<_TypesafeBuilderEmpty>`
// method `build` is available on `UserBuilder<_TypesafeBuilderFilled>`
Constraint Violation Error Examples
// ❌ Compile error
let config = new
.with_feature
.build;
// ^^^^^
// error: no method named `build` found for struct `ConfigBuilder<_TypesafeBuilderFilled, _TypesafeBuilderEmpty>`
// method `build` is available on `ConfigBuilder<_TypesafeBuilderFilled, _TypesafeBuilderFilled>`
🔍 Real-World Use Cases
Web API Configuration
Database Connection
🤝 Contributing
We welcome contributions to TypeSafe Builder!
Development Environment Setup
Running Tests
# Run all tests
# UI tests (compile error verification)
👥 Contributors
Amazing developers who have contributed to this project:
Want to see your name here? Contribute now and join our amazing community!
📄 License
MIT License - see the LICENSE file for details.
🌟 Give us a star!
If you find this project useful, please consider giving it a ⭐!