Hey everyone☺️
I recently developed a library called typesafe_builder for implementing builder patterns in Rust, and I’d love to get feedback from the community. I was using existing builder libraries and ran into problems that I couldn’t solve:
- Unable to express conditional dependencies (field
B
is required only when fieldA
is set) - No support for complex conditional logic (expressions using
&&
(AND),||
(OR),!
(NOT) operators) - Can’t handle inverse conditions (optional only under specific conditions)
GitHub: https://github.com/tomoikey/typesafe_builder
Example
required
and optional
#[derive(Builder)]
struct User {
#[builder(required)]
name: String,
#[builder(optional)]
age: Option<u32>,
}
// ✅ Compiles successfully
let user1 = UserBuilder::new()
.with_name("Alice".to_string())
.with_age(30)
.build();
// ❌ Compile error: name is required
// let user2 = UserBuilder::new()
// .with_age(40)
// .build();
required_if
#[derive(Builder)]
struct Account {
#[builder(optional)]
email: Option<String>,
#[builder(required_if = "email")] // Required when email is set
email_verified: Option<bool>,
}
// ✅ Compiles successfully
let account1 = AccountBuilder::new().build();
// ✅ Compiles successfully
let account2 = AccountBuilder::new()
.with_email("user@example.com".to_string())
.with_email_verified(true)
.build();
// ❌ Compile error: email_verified is required if email is set
// let account3 = AccountBuilder::new()
// .with_email("user@example.com".to_string())
// .build();
Complex Conditional Logic
#[derive(Builder)]
struct ApiClient {
#[builder(optional)]
use_auth: Option<bool>,
#[builder(optional)]
use_https: Option<bool>,
#[builder(optional)]
api_key: Option<String>,
// Secret is required if using auth OR HTTPS
#[builder(required_if = "use_auth || use_https")]
secret: Option<String>,
// Certificate is required only when using both auth AND HTTPS
#[builder(required_if = "use_auth && use_https")]
certificate: Option<String>,
// Warning is required when using neither auth NOR HTTPS
#[builder(required_if = "!use_auth && !use_https")]
insecure_warning: Option<String>,
// Complex condition: Token required when (auth OR HTTPS) AND (no API key)
#[builder(required_if = "(use_auth || use_https) && !api_key")]
fallback_token: Option<String>,
}
Key Features
-
required_if
- Fields become required based on other field settings
-
optional_if
- Fields are optional only under specific conditions (required otherwise)
- Complex logical operations
- Support for complex conditional expressions using
&&
,||
,!
- Support for complex conditional expressions using
- type safe
- All validation completed at compile time
With traditional builder libraries, expressing these complex conditional relationships was difficult, and we had to rely on runtime validation.
Questions
What do you think about this approach?
- Have you experienced difficulties with conditional dependencies in real projects?
- Are there other features you think would be necessary?
- Would you consider using this in actual projects?
I tried to differentiate from existing libraries by focusing on the expressiveness of conditional dependencies, but there might still be areas lacking in terms of practicality.
I’d really appreciate honest feedback!