Expand description
§More Options

More Options is a library for defining configuration options in Rust. Options can be initialized in code, bound from configuration, and/or composed through dependency injection (DI).
You may be looking for:
§Features
This crate provides the following features:
- default - Abstractions for options
- async - Enable options in asynchronous contexts
- di - Dependency injection extensions
- cfg - Dependency injection extensions to bind configurations to options
§Options Pattern
The options pattern uses structures to provide strongly typed access to groups of related settings without having to know how the settings were configured. The settings can be set explicitly in code or they can come from an external configuration source such as a file.
Consider the following options:
pub struct EndpointOptions {
pub url: String,
pub retries: usize,
}
These might be used by a HTTP client as follows:
use options::Options;
use std::rc::Rc;
pub struct HttpClient {
options: Rc<dyn Options<EndpointOptions>>,
}
impl HttpClient {
pub fn new(options: Rc<dyn Options<EndpointOptions>>) -> Self {
Self { options }
}
pub fn retries(&self) -> usize {
self.options.value().retries
}
}
§Options in Action
The defined options can be used in any number of ways, including just explicitly specifying the settings.
use crate::*;
use std::rc::Rc;
fn main() {
let options = Rc::new(options::create(EndpointOptions {
url: "https://tempuri.org",
retries: 2,
}));
let client = HttpClient::new(options);
// TODO: use the client
}
If you expect to process your options from an external data source, then you’ll almost certainly require supporting deserialization using serde as follows:
use serde::Deserialize;
#[derive(Deserialize)]
pub struct EndpointOptions {
pub url: String,
pub retries: usize,
}
Suppose you had the following appSettings.json
file:
{
"url": "https://tempuri.org",
"retries": 3
}
You can construct the options from the settings by including the more-config crate as follows:
use crate::*;
use config::{*, ext::*};
fn main() {
let config = DefaultConfigurationBuilder::new()
.add_json_file("appsettings.json")
.build()
.unwrap();
let options: EndpointOptions = config.reify();
let client = HttpClient::new(options);
// TODO: use the client
}
You can go one step further and combine that configuration with the more-di crate to assemble all of the pieces for you:
use crate::*;
use config::{*, ext::*};
use di::*;
use std::rc::Rc;
fn main() {
let config = Rc::from(
DefaultConfigurationBuilder::new()
.add_json_file("appsettings.json")
.build()
.unwrap()
.as_config());
let provider = ServiceCollection::new()
.add(transient_as_self::<HttpClient>())
.apply_config::<EndpointOptions>(config)
.build_provider()
.unwrap();
let client = provider.get_required::<HttpClient>();
// TODO: use the client
}
§License
This project is licensed under the MIT license.
Modules§
- ext
- Contains options extension methods.
Structs§
- Default
Options Factory - Represents the default factory used to create configuration
Options
. - Default
Options Monitor - Represents the default implementation for notifications when option instances change.
- Options
Builder di
- Represents a builder used to configure
Options
. - Options
Cache - Represents a cache for configured options.
- Options
Manager - Represents an object that manages
Options
and option snapshots. - Subscription
- Represents a change subscription.
- Validate
Options Result - Represents the result of
Options
validation.
Traits§
- Configure
Options - Defines the behavior of something that configures
Options
. - Options
- Defines the behavior to retrieve configured options.
- Options
Change Token Source - Used to fetch
ChangeToken
used for tracking options changes. - Options
Factory - Defines the behavior of an object that creates configuration
Options
. - Options
Monitor - Defines the behavior for notifications when
Options
instances change. - Options
Monitor Cache - Defines the behavior of an
Options
monitor cache. - Options
Snapshot - Defines the behavior for a snapshot of configuration
Options
. - Post
Configure Options - Defines the behavior of something that configures
Options
. - Validate
Options - Defines the behavior of an object that validates configuration options.
- Value
Functions§
- configure
- Creates and returns options configuration for the specified action.
- create
- Creates a wrapper around a value to return itself as
Options
. - post_
configure - Creates and returns options post-configuration for the specified action.
Type Aliases§
- Ref
- Represents the type alias for an options reference.