Skip to content

Commit 4883a7a

Browse files
committed
Detect changes to environment variable
1 parent 9de8af6 commit 4883a7a

File tree

3 files changed

+88
-15
lines changed

3 files changed

+88
-15
lines changed

crates/common/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ sha2 = "0.10.9"
2626
tokio = { version = "1.46", features = ["sync", "macros", "io-util", "rt", "time"] }
2727
url = "2.4.1"
2828

29+
[build-dependencies]
30+
serde = { version = "1.0", features = ["derive"] }
31+
serde_json = "1.0.91"
32+
config = "0.15.11"
33+
2934
[dev-dependencies]
3035
regex = "1.1.1"
3136
temp-env = "0.3.6"

crates/common/build.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#[path = "src/settings.rs"]
2+
mod settings;
3+
4+
use serde_json::Value;
5+
use std::collections::HashSet;
6+
7+
fn main() {
8+
// Watch the settings.rs file for changes
9+
println!("cargo:rerun-if-changed=../../trusted-server.toml");
10+
11+
// Create a default Settings instance and convert to JSON to discover all fields
12+
let default_settings = settings::Settings::default();
13+
let settings_json = serde_json::to_value(&default_settings).unwrap();
14+
15+
let mut env_vars = HashSet::new();
16+
collect_env_vars(
17+
&settings_json,
18+
settings::ENVIRONMENT_VARIABLE_PREFIX,
19+
settings::ENVIRONMENT_VARIABLE_SEPARATOR,
20+
&mut env_vars,
21+
vec![],
22+
);
23+
24+
// Print rerun-if-env-changed for each variable
25+
let mut sorted_vars: Vec<_> = env_vars.into_iter().collect();
26+
sorted_vars.sort();
27+
28+
for var in sorted_vars {
29+
println!("cargo:rerun-if-env-changed={}", var);
30+
}
31+
}
32+
33+
fn collect_env_vars(
34+
value: &Value,
35+
prefix: &str,
36+
sep: &str,
37+
env_vars: &mut HashSet<String>,
38+
path: Vec<String>,
39+
) {
40+
if let Value::Object(map) = value {
41+
for (key, val) in map {
42+
let mut new_path = path.clone();
43+
new_path.push(key.to_uppercase());
44+
45+
match val {
46+
Value::String(_) | Value::Number(_) | Value::Bool(_) => {
47+
// Leaf node - create environment variable
48+
let env_var = format!("{}{}{}", prefix, sep, new_path.join(sep));
49+
env_vars.insert(env_var);
50+
}
51+
Value::Object(_) => {
52+
// Recurse into nested objects
53+
collect_env_vars(val, prefix, sep, env_vars, new_path);
54+
}
55+
_ => {}
56+
}
57+
}
58+
}
59+
}

crates/common/src/settings.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
use std::str;
22

33
use config::{Config, ConfigError, Environment, File, FileFormat};
4-
use serde::Deserialize;
4+
use serde::{Deserialize, Serialize};
55

6-
#[derive(Debug, Deserialize)]
7-
#[allow(unused)]
6+
pub const ENVIRONMENT_VARIABLE_PREFIX: &str = "TRUSTED_SERVER";
7+
pub const ENVIRONMENT_VARIABLE_SEPARATOR: &str = "__";
8+
9+
#[derive(Debug, Default, Deserialize, Serialize)]
810
pub struct AdServer {
911
pub ad_partner_url: String,
1012
pub sync_url: String,
1113
}
1214

13-
#[derive(Debug, Deserialize)]
14-
#[allow(unused)]
15+
#[derive(Debug, Default, Deserialize, Serialize)]
1516
pub struct Publisher {
1617
pub domain: String,
1718
pub cookie_domain: String,
1819
pub origin_url: String,
1920
}
2021

21-
#[derive(Debug, Deserialize)]
22-
#[allow(unused)]
22+
#[derive(Debug, Default, Deserialize, Serialize)]
2323
pub struct Prebid {
2424
pub server_url: String,
2525
}
2626

27-
#[derive(Debug, Deserialize)]
28-
#[allow(unused)]
27+
#[derive(Debug, Default, Deserialize, Serialize)]
2928
pub struct Synthetic {
3029
pub counter_store: String,
3130
pub opid_store: String,
3231
pub secret_key: String,
3332
pub template: String,
3433
}
3534

36-
#[derive(Debug, Deserialize)]
37-
#[allow(unused)]
35+
#[derive(Debug, Default, Deserialize, Serialize)]
3836
pub struct Settings {
3937
pub ad_server: AdServer,
4038
pub publisher: Publisher,
4139
pub prebid: Prebid,
4240
pub synthetic: Synthetic,
4341
}
4442

43+
#[allow(unused)]
4544
impl Settings {
4645
pub fn new() -> Result<Self, ConfigError> {
4746
let toml_bytes = include_bytes!("../../../trusted-server.toml");
@@ -52,8 +51,8 @@ impl Settings {
5251

5352
pub fn from_toml(toml_str: &str) -> Result<Self, ConfigError> {
5453
let environment = Environment::default()
55-
.prefix("TRUSTED_SERVER")
56-
.separator("__");
54+
.prefix(ENVIRONMENT_VARIABLE_PREFIX)
55+
.separator(ENVIRONMENT_VARIABLE_SEPARATOR);
5756

5857
let toml = File::from_str(toml_str, FileFormat::Toml);
5958
let config = Config::builder()
@@ -181,7 +180,12 @@ mod tests {
181180
let toml_str = re.replace(&toml_str, "");
182181

183182
temp_env::with_var(
184-
"TRUSTED_SERVER__AD_SERVER__AD_PARTNER_URL",
183+
format!(
184+
"{}{}AD_SERVER{}AD_PARTNER_URL",
185+
ENVIRONMENT_VARIABLE_PREFIX,
186+
ENVIRONMENT_VARIABLE_SEPARATOR,
187+
ENVIRONMENT_VARIABLE_SEPARATOR
188+
),
185189
Some("https://change-ad.com/serve"),
186190
|| {
187191
let settings = Settings::from_toml(&toml_str);
@@ -200,7 +204,12 @@ mod tests {
200204
let toml_str = crate_test_settings_str();
201205

202206
temp_env::with_var(
203-
"TRUSTED_SERVER__AD_SERVER__AD_PARTNER_URL",
207+
format!(
208+
"{}{}AD_SERVER{}AD_PARTNER_URL",
209+
ENVIRONMENT_VARIABLE_PREFIX,
210+
ENVIRONMENT_VARIABLE_SEPARATOR,
211+
ENVIRONMENT_VARIABLE_SEPARATOR
212+
),
204213
Some("https://change-ad.com/serve"),
205214
|| {
206215
let settings = Settings::from_toml(&toml_str);

0 commit comments

Comments
 (0)