pub struct ConfigBuilder { /* private fields */ }Expand description
A builder for constructing a Config instance in a structured manner.
The ConfigBuilder struct allows users to set configuration parameters individually,
ensuring required fields are provided before building the final Config instance.
§Fields
region(Option<String>): Optional region value.endpoint(Option<String>): Optional endpoint value.disable_ssl(Option<bool>): Optional SSL flag.credentials(Option<credentials::Credentials>): Optional authentication credentials.
Implementations§
Source§impl ConfigBuilder
Implementation of the ConfigBuilder struct, which provides a fluent interface
for constructing a Config instance step-by-step.
impl ConfigBuilder
Implementation of the ConfigBuilder struct, which provides a fluent interface
for constructing a Config instance step-by-step.
The builder pattern allows for greater flexibility when creating configuration objects
by enabling method chaining to set individual parameters. This approach ensures
that required fields are validated before the final Config instance is created.
§Example Usage
use crate::volcengine::config::Config;
let config = Config::builder()
.with_region("us-east-1")
.with_endpoint("https://example.com")
.with_disable_ssl(true)
.with_credentials(credentials)
.build()
.expect("Failed to build config"); Each setter method in ConfigBuilder accepts an argument, modifies the corresponding
field, and returns self to allow method chaining. The build method performs final
validation and returns a Config instance or an error if mandatory fields are missing.
Sourcepub fn with_region(self, region: &str) -> Self
pub fn with_region(self, region: &str) -> Self
Sourcepub fn with_endpoint(self, endpoint: &str) -> Self
pub fn with_endpoint(self, endpoint: &str) -> Self
Sourcepub fn with_disable_ssl(self, disable_ssl: bool) -> Self
pub fn with_disable_ssl(self, disable_ssl: bool) -> Self
Sourcepub fn with_credentials(self, credentials: Credentials) -> Self
pub fn with_credentials(self, credentials: Credentials) -> Self
Sourcepub fn build(self) -> Result<Config, Error>
pub fn build(self) -> Result<Config, Error>
Builds the final Config object.
This method ensures that all required fields (such as credentials) are provided before
constructing the Config instance. If any required field is missing, an error is returned.
§Returns
Ok(Config): The successfully builtConfiginstance.Err(error::Error): Returns an error if required fields (e.g., credentials) are missing.
§Example
let config = Config::builder()
.with_region("us-west-2")
.with_endpoint("https://api.volcengine.com")
.with_disable_ssl(false)
.with_credentials(credentials::Credentials::new("access_key", "secret_key"))
.build();
match config {
Ok(cfg) => println!("Configuration built successfully!"),
Err(err) => println!("Failed to build configuration: {:?}", err),
}Trait Implementations§
Source§impl Default for ConfigBuilder
Provides a default implementation for ConfigBuilder,
initializing all fields to None.
impl Default for ConfigBuilder
Provides a default implementation for ConfigBuilder,
initializing all fields to None.
This implementation ensures that when a ConfigBuilder instance is created
without any parameters, it starts with no predefined values. The user can
then set specific fields as needed before calling build().
§Default Values
region:None(must be provided if required for the configuration)endpoint:None(must be provided if required for the configuration)disable_ssl:None(defaults tofalseif not explicitly set)credentials:None(mandatory;build()will return an error if not provided)
Using the builder pattern with a default constructor allows flexibility
while enforcing validation rules in the build() method.
Auto Trait Implementations§
impl Freeze for ConfigBuilder
impl RefUnwindSafe for ConfigBuilder
impl Send for ConfigBuilder
impl Sync for ConfigBuilder
impl Unpin for ConfigBuilder
impl UnwindSafe for ConfigBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request