WizardBuilder

Struct WizardBuilder 

Source
pub struct WizardBuilder<T: Wizard> { /* private fields */ }
Expand description

Builder for configuring and executing a wizard

Implementations§

Source§

impl<T: Wizard> WizardBuilder<T>

Source

pub fn new() -> Self

Create a new wizard builder

Source

pub fn with_suggestions(self, suggestions: T) -> Self

Set suggested values for the wizard

Examples found in repository?
examples/builders/builder_comprehensive.rs (line 124)
115fn run_suggestions_demo() {
116    println!("\n--- Demo with Suggestions ---");
117    let suggestions = UserProfile {
118        name: "John Doe".to_string(),
119        age: 30,
120        email: "john@example.com".to_string(),
121        subscribe: true,
122    };
123    let profile = UserProfile::wizard_builder()
124        .with_suggestions(suggestions)
125        .build()
126        .unwrap();
127    println!("Profile: {:#?}", profile);
128}
More examples
Hide additional examples
examples/features/suggestions.rs (line 24)
16fn main() {
17    println!("=== Creating a new user profile ===");
18    let profile = UserProfile::wizard_builder().build().unwrap();
19    println!("Created profile: {profile:#?}");
20
21    println!("=== Editing the existing profile ===");
22    println!("The current values will be pre-filled as suggestions.");
23    let updated_profile = UserProfile::wizard_builder()
24        .with_suggestions(profile)
25        .build()
26        .unwrap();
27    println!("Updated profile: {updated_profile:#?}");
28}
examples/builders/builder_api.rs (line 45)
17fn main() {
18    println!("=== Builder API Demo ===");
19
20    // Example 1: Simple builder with default backend
21    println!("Example 1: Using default backend");
22    let config1 = ServerConfig::wizard_builder().build().unwrap();
23    println!("Config: {:#?}", config1);
24
25    // Example 2: Builder with custom backend (dialoguer)
26    #[cfg(feature = "dialoguer-backend")]
27    {
28        println!("Example 2: Using dialoguer backend");
29        let backend = derive_wizard::DialoguerBackend::new();
30        let config2 = ServerConfig::wizard_builder()
31            .with_backend(backend)
32            .build()
33            .unwrap();
34        println!("Config: {:#?}", config2);
35    }
36
37    // Example 3: Builder with suggestions
38    println!("Example 3: Using suggestions (re-prompting with existing values)");
39    let suggestions = ServerConfig {
40        host: "localhost".to_string(),
41        port: 8080,
42        ssl: true,
43    };
44    let config3 = ServerConfig::wizard_builder()
45        .with_suggestions(suggestions)
46        .build()
47        .unwrap();
48    println!("Config: {:#?}", config3);
49}
Source

pub fn suggest_field( self, field: impl Into<FieldPath>, value: impl Into<SuggestedAnswer>, ) -> Self

Suggest a specific field value. The question will still be asked but with a pre-filled default.

For nested fields, use the field! macro.

Source

pub fn assume_field( self, field: impl Into<FieldPath>, value: impl Into<AssumedAnswer>, ) -> Self

Assume a specific field value. The question for this field will be skipped.

For nested fields, use the field! macro.

Examples found in repository?
examples/features/assumptions.rs (line 34)
26fn main() {
27    println!("=== Deployment Configuration Tool ===");
28
29    // Scenario 1: Partial assumptions - the most common use case
30    println!("--- Scenario 1: Partial Assumptions (Recommended) ---");
31    println!("We'll assume some security-critical values but ask about others.");
32
33    let config = DeploymentConfig::wizard_builder()
34        .assume_field("environment", "production".to_string()) // Fixed: production
35        .assume_field("debug", false) // Fixed: no debug in prod
36        .assume_field("port", 443) // Fixed: HTTPS port
37        .build()
38        .unwrap(); // Will only ask about 'app_name' and 'database_url'
39
40    println!("=== Configuration (with partial assumptions) ===");
41    println!("{:#?}", config);
42    println!("Notice: Only asked about app_name and database_url!");
43    println!("The fields 'environment', 'debug', and 'port' were assumed.");
44
45    // Scenario 2: Full assumptions - for batch processing
46    println!("--- Scenario 2: Full Assumptions (for automation) ---");
47    println!("Using a complete template - no questions will be asked.");
48
49    let batch_config = DeploymentConfig::wizard_builder()
50        .assume_field("app_name", "batch-processor".to_string())
51        .assume_field("environment", "production".to_string())
52        .assume_field("port", 8080)
53        .assume_field("debug", false)
54        .assume_field(
55            "database_url",
56            "postgresql://prod-db:5432/batch".to_string(),
57        )
58        .build()
59        .unwrap();
60
61    println!("=== Batch Configuration (all assumed, no questions) ===");
62    println!("{:#?}", batch_config);
63
64    println!("--- Summary ---");
65    println!("Partial assumptions: Fix some fields, ask about others");
66    println!("Full assumptions: Fix all fields, no user interaction");
67    println!("Suggestions: Pre-fill values, but still ask all questions");
68}
Source

pub fn with_backend<B: InterviewBackend + 'static>(self, backend: B) -> Self

Set a custom backend

Examples found in repository?
examples/backends/requestty_multi_select.rs (line 44)
41fn main() {
42    let backend = derive_wizard::RequesttyBackend;
43    let order = PizzaOrder::wizard_builder()
44        .with_backend(backend)
45        .build()
46        .unwrap();
47
48    println!("\n📋 Order Summary");
49    println!("────────────────");
50    println!("Name: {}", order.name);
51    println!("Size: {:?}", order.size);
52    println!("Toppings: {:?}", order.toppings);
53}
Source

pub fn build(self) -> Result<T, BackendError>

Execute the wizard and return the result

Examples found in repository?
examples/basic/nested_structs.rs (line 43)
42fn main() {
43    let user = User::wizard_builder().build().unwrap();
44    println!("{user:#?}");
45}
More examples
Hide additional examples
examples/advanced/deeply_nested.rs (line 40)
39fn main() {
40    let event = Event::wizard_builder().build().unwrap();
41    println!("{event:#?}");
42}
examples/advanced/nested_enum_payment.rs (line 40)
39fn main() {
40    let order = Order::wizard_builder().build().unwrap();
41    println!("{:#?}", order);
42}
examples/features/validation.rs (line 66)
65fn main() {
66    let config = ServerConfig::wizard_builder().build().unwrap();
67    println!("{config:#?}");
68}
examples/features/password_masking.rs (line 15)
14fn main() {
15    let form = LoginForm::wizard_builder().build().unwrap();
16    println!("LoginForm: {form:#?}");
17}
examples/basic/pathbuf.rs (line 11)
10fn main() {
11    let config = FileConfig::wizard_builder().build().unwrap();
12    println!("  Input:  {:?}", config.path);
13}

Trait Implementations§

Source§

impl<T: Default + Wizard> Default for WizardBuilder<T>

Source§

fn default() -> WizardBuilder<T>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for WizardBuilder<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for WizardBuilder<T>

§

impl<T> !Send for WizardBuilder<T>

§

impl<T> !Sync for WizardBuilder<T>

§

impl<T> Unpin for WizardBuilder<T>
where T: Unpin,

§

impl<T> !UnwindSafe for WizardBuilder<T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.