Feature Evaluation
Evaluate feature keys using one shared TogglyClient and an owned context for the current operation. Feature flags select behavior; your application's authentication and authorization remain separate.
Basic Evaluation
Check if Enabled
Check if Disabled
Check if Defined
This function accepts an already initialized client and context, so it can be called from a handler or a background job:
use toggly::{EvalContext, TogglyClient};
pub async fn describe(
client: &TogglyClient,
context: EvalContext,
) -> toggly::Result<(bool, bool, bool)> {
let enabled = client.is_enabled("new-dashboard", context.clone()).await?;
let disabled = client.is_disabled("new-dashboard", context).await?;
// Defined means a key exists, even when its conditions evaluate false.
let defined = client.is_defined("new-dashboard").await;
Ok((enabled, disabled, defined))
}
An undefined flag returns false with the default configuration; is_disabled therefore returns true. Errors use toggly::Result; do not treat every transport or configuration error as a missing flag.
Evaluation Context
Identity
Use a stable application identity for targeting and percentage rollouts. A demonstration identity such as alice is not authentication.
Groups
.groups(...) takes Vec<String>; .group(...) appends a single group.
Traits
Traits are arbitrary JSON values for ContextualTargeting. Claims and request fields have separate containers and are not automatically inferred from traits.
Full Example
use toggly::{EvalContext, HttpRequestMapper};
pub fn authenticated_context(user_id: &str) -> EvalContext {
// Replace these example memberships and claims with verified auth data.
EvalContext::builder()
.identity(user_id)
.groups(vec!["beta-testers".into(), "enterprise".into()])
.claim("role", "admin")
.trait_value("plan", "enterprise")
.request(HttpRequestMapper::from_http_headers([
("user-agent", "Mozilla/5.0 Chrome/128.0.0.0 Safari/537.36"),
("accept-language", "en-US,en;q=0.9"),
("cf-ipcountry", "US"),
]))
.build()
}
| Context field | Input for |
|---|---|
identity | Percentage and user targeting |
groups | Group targeting |
claims | UserClaims |
request | Country, browser, language, device and OS filters |
traits | ContextualTargeting |
entity | ContextProperty |
Mapping HTTP headers
HttpRequestMapper::from_http_headers reads User-Agent, Accept-Language and country headers. Country precedence is CF-IPCountry, X-Vercel-IP-Country, then CloudFront-Viewer-Country. Trust country headers only when your proxy controls them. merge_into(headers, Some(&base)) clones the base context and replaces its request fields with mapped values; it retains the base identity, groups, claims, traits and entity.
Native Actix/Axum/Rocket Feature helpers map identity headers only. They do not automatically apply the complete context above to their own checks. Use the adapter's direct client access and pass the enriched context explicitly, as shown in each guide. Preparing context does not fetch definitions or invalidate an existing cached decision; see Caching.