OpenFeature Provider for Rust
Getting started
1. Install the provider
Run the following Cargo command in your project directory:
cargo add configcat-openfeature-provider
Or add the following to your Cargo.toml
:
[dependencies]
configcat-openfeature-provider = "0.1"
2. Initialize the provider
The ConfigCatProvider
needs a pre-configured ConfigCat Rust SDK client:
use std::time::Duration;
use configcat::{Client, PollingMode};
use open_feature::OpenFeature;
use configcat_openfeature_provider::ConfigCatProvider;
#[tokio::main]
async fn main() {
// Acquire an OpenFeature API instance.
let mut api = OpenFeature::singleton_mut().await;
// Configure the ConfigCat SDK.
let configcat_client = Client::builder("<YOUR-CONFIGCAT-SDK-KEY>")
.polling_mode(PollingMode::AutoPoll(Duration::from_secs(60)))
.build()
.unwrap();
// Configure the provider.
api.set_provider(ConfigCatProvider::new(configcat_client)).await;
// Create a client.
let client = api.create_client();
}
For more information about all the configuration options, see the Rust SDK documentation.
3. Evaluate your feature flag
let is_awesome_feature_enabled = client
.get_bool_value("isAwesomeFeatureEnabled", None, None)
.await
.unwrap_or(false);
if is_awesome_feature_enabled {
do_the_new_thing();
} else {
do_the_old_thing();
}
Evaluation Context
An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.
The following table shows how the different context attributes are mapped to User Object attributes.
Evaluation context | User Object | Required |
---|---|---|
targeting_key | identifier | ☑ |
Email | email | |
Country | country | |
Any other | custom |
To evaluate feature flags for a context, use the OpenFeature Evaluation API:
let context = EvaluationContext::default()
.with_targeting_key("#SOME-USER-ID#")
.with_custom_field("Email", "[email protected]")
.with_custom_field("Country", "CountryID")
.with_custom_field("Rating", 4.5)
.with_custom_field("Roles", ["Role1", "Role2"]);
let is_awesome_feature_enabled = client
.get_bool_value("isAwesomeFeatureEnabled", Some(&context), None)
.await
.unwrap_or(false);