Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "typesafe_builder"
version = "1.4.0"
version = "1.5.0"
edition = "2024"
authors = ["tomoikey"]
readme = "README.md"
Expand All @@ -10,4 +10,4 @@ license = "MIT"
description = "A procedural macro to generate type-safe builder patterns for Rust structs"

[dependencies]
typesafe_builder_derive = "1.4.0"
typesafe_builder_derive = "1.5.0"
99 changes: 68 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">

# 🦀 TypeSafe Builder 🦀
# TypeSafe Builder

<div>
<img src="https://img.shields.io/crates/v/typesafe_builder.svg" alt="crates.io"/>
Expand All @@ -18,7 +18,7 @@
</a>
</div>

<h3>🚀 Compile-Time Type Safety • 🧬 Zero Runtime Cost • Blazing Fast Builds</h3>
<h3>Compile-Time Type Safety • Zero Runtime Cost • Blazing Fast Builds</h3>

**The Ultimate Builder Pattern Implementation Powered by Rust's Type System**

Expand All @@ -29,7 +29,7 @@
---
</div>

## Why TypeSafe Builder?
## 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**.
Expand All @@ -47,22 +47,23 @@ let user = UserBuilder::new()
.build(); // Always guaranteed to succeed
```

## 🎯 Key Features
## Key Features

### 🔒 **Type-Level Constraint System**
### Type-Level Constraint System
- **Required Fields** - Completely prevent missing required field configuration
- **Optional Fields** - Freely configurable fields
- **Default Values** - Fields with intelligent default values using any Rust expression
- **Conditional Requirements** - Express dynamic dependencies at the type level
- **Complex Logic** - Support for AND/OR/NOT operators in complex conditional expressions
- **Into Conversion** - Ergonomic setters with automatic type conversion via `Into<T>`

### ⚡ **Performance Characteristics**
### Performance Characteristics
- **Zero Runtime Cost** - All validation completed at compile time

### 🛡️ **Safety Guarantees**
### Safety Guarantees
- **No Panic** - Complete elimination of runtime panics

## 📦 Quick Start
## Quick Start

```toml
[dependencies]
Expand All @@ -89,9 +90,9 @@ let user = UserBuilder::new()
.build(); // email will be "[email protected]"
```

## 🚀 Advanced Features
## Advanced Features

### 1️⃣ **Conditional Required Fields**
### 1. Conditional Required Fields

```rust
use typesafe_builder::*;
Expand Down Expand Up @@ -119,7 +120,7 @@ let account2 = AccountBuilder::new()
// .build();
```

### 2️⃣ **Conditional Optional Fields**
### 2. Conditional Optional Fields

```rust
use typesafe_builder::*;
Expand All @@ -143,7 +144,7 @@ let config2 = ConfigBuilder::new()
.build();
```

### 3️⃣ **Complex Conditional Logic**
### 3. Complex Conditional Logic

```rust
use typesafe_builder::*;
Expand Down Expand Up @@ -198,7 +199,7 @@ let client3 = ApiClientBuilder::new()
.build();
```

### 4️⃣ **Default Values**
### 4. Default Values

```rust
use typesafe_builder::*;
Expand Down Expand Up @@ -252,14 +253,14 @@ struct AppConfig {
}
```

**Key features of default values:**
- **Flexible expressions**: Use any valid Rust expression as default value
- **No type restrictions**: Works with primitives, collections, function calls, etc.
- **Environment variables**: Access environment variables at build time
- **Function calls**: Call any function or method as default value
- **Standalone attribute**: Cannot be combined with `required`, `optional`, etc.
Key features of default values:
- Flexible expressions: Use any valid Rust expression as default value
- No type restrictions: Works with primitives, collections, function calls, etc.
- Environment variables: Access environment variables at build time
- Function calls: Call any function or method as default value
- Standalone attribute: Cannot be combined with `required`, `optional`, etc.

### 5️⃣ **Negation Operator Support**
### 5. Negation Operator Support

```rust
use typesafe_builder::*;
Expand All @@ -281,7 +282,43 @@ let db = DatabaseBuilder::new()
.build();
```

### 6️⃣ **Custom Builder Name**
### 6. Into Conversion Support

The `#[builder(into)]` attribute allows setter methods to accept any type that implements `Into<T>` for the field type `T`, providing more ergonomic APIs:

```rust
use typesafe_builder::*;

#[derive(Builder)]
struct User {
#[builder(required)]
#[builder(into)]
name: String,

#[builder(optional)]
#[builder(into)]
email: Option<String>,
}

// ✅ Accept &str directly (converts to String via Into)
let user1 = UserBuilder::new()
.with_name("Alice") // &str -> String
.with_email("[email protected]") // &str -> String
.build();

// ✅ Still works with String directly
let user2 = UserBuilder::new()
.with_name("Bob".to_string())
.build();
```

Key benefits:
- Ergonomic APIs: Accept `&str` for `String` fields without manual conversion
- Type flexibility: Any `Into<T>` implementation works automatically
- Zero overhead: Conversion happens at the call site
- Backward compatible: Works alongside existing setter patterns

### 7. Custom Builder Name

```rust
use typesafe_builder::*;
Expand All @@ -299,7 +336,7 @@ let user = MyCustomBuilder::new()
.build();
```

## 🔧 Error Handling
## Error Handling

### Compile-Time Error Examples

Expand Down Expand Up @@ -337,7 +374,7 @@ let config = ConfigBuilder::new()
// method `build` is available on `ConfigBuilder<_TypesafeBuilderFilled, _TypesafeBuilderFilled>`
```

## 🔍 Real-World Use Cases
## Real-World Use Cases

### Web API Configuration

Expand Down Expand Up @@ -392,7 +429,7 @@ struct DatabaseConfig {
}
```

## 🤝 Contributing
## Contributing

We welcome contributions to TypeSafe Builder!

Expand All @@ -414,7 +451,7 @@ cargo test
cargo test --package typesafe_builder_derive --test ui
```

## 👥 Contributors
## Contributors

Amazing developers who have contributed to this project:

Expand All @@ -428,7 +465,7 @@ Amazing developers who have contributed to this project:
<br />
<sub><b>tomoikey</b></sub>
<br />
<sub>🧠 Creator & Maintainer</sub>
<sub>Creator & Maintainer</sub>
</a>
</td>
<td align="center">
Expand All @@ -437,7 +474,7 @@ Amazing developers who have contributed to this project:
<br />
<sub><b>ramsyana</b></sub>
<br />
<sub>🔧 Contributor</sub>
<sub>Contributor</sub>
</a>
</td>
<td align="center">
Expand All @@ -446,7 +483,7 @@ Amazing developers who have contributed to this project:
<br />
<sub><b>Your Name Here</b></sub>
<br />
<sub>🚀 Next Contributor</sub>
<sub>Next Contributor</sub>
</a>
</td>
</tr>
Expand All @@ -458,13 +495,13 @@ Amazing developers who have contributed to this project:

</div>

## 📄 License
## License

MIT License - see the [LICENSE](LICENSE) file for details.

## 🌟 Give us a star!
## Give us a star!

If you find this project useful, please consider giving it a !
If you find this project useful, please consider giving it a star!

---

Expand Down
59 changes: 27 additions & 32 deletions typesafe_builder_derive/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading