Skip to main content

qubit_config/field/
config_field_builder.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10
11use crate::options::ConfigReadOptions;
12
13use super::config_field::ConfigField;
14
15/// Builder state after the field name is provided.
16///
17pub struct ConfigFieldBuilder<T> {
18    /// The primary field name.
19    pub(crate) name: String,
20    /// The fallback aliases.
21    pub(crate) aliases: Vec<String>,
22    /// The default value.
23    pub(crate) default: Option<T>,
24    /// The read options.
25    pub(crate) read_options: Option<ConfigReadOptions>,
26}
27
28impl<T> ConfigFieldBuilder<T> {
29    /// Adds a fallback alias.
30    ///
31    /// # Parameters
32    ///
33    /// * `alias` - Fallback key checked after previous names.
34    ///
35    /// # Returns
36    ///
37    /// Updated builder.
38    pub fn alias(mut self, alias: &str) -> Self {
39        self.aliases.push(alias.to_string());
40        self
41    }
42
43    /// Sets the default value used when all names are absent.
44    ///
45    /// # Parameters
46    ///
47    /// * `default` - Default value.
48    ///
49    /// # Returns
50    ///
51    /// Updated builder.
52    pub fn default(mut self, default: T) -> Self {
53        self.default = Some(default);
54        self
55    }
56
57    /// Sets field-level read options.
58    ///
59    /// # Parameters
60    ///
61    /// * `read_options` - Options that override the reader's global options.
62    ///
63    /// # Returns
64    ///
65    /// Updated builder.
66    pub fn read_options(mut self, read_options: ConfigReadOptions) -> Self {
67        self.read_options = Some(read_options);
68        self
69    }
70
71    /// Finishes the builder.
72    ///
73    /// # Returns
74    ///
75    /// A field declaration ready to be passed to [`crate::ConfigReader::read`].
76    pub fn build(self) -> ConfigField<T> {
77        ConfigField {
78            name: self.name,
79            aliases: self.aliases,
80            default: self.default,
81            read_options: self.read_options,
82        }
83    }
84}