/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use serde_derive::{Deserialize, Serialize}; use serde_json::{Map, Value}; use crate::evaluator::split_locale; use crate::stateless::matcher::AppContext; #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct TargetingAttributes { pub language: Option, pub region: Option, #[serde(flatten)] pub app_context: AppContext, // This should be last so that any calculated fields (e.g. language and region) // are not overwriting the explicit fields set by the request. #[serde(flatten)] pub request_context: Map, } impl TargetingAttributes { pub fn new(app_context: AppContext, request_context: Map) -> Self { let (language, region) = match request_context .get("locale") .unwrap_or(&Value::Null) .as_str() { Some(locale) => split_locale(locale.to_string()), _ => (None, None), }; Self { app_context, request_context, language, region, } } }