use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum SlotType {
Secret {
name: String,
},
OauthClient {
name: String,
},
ChannelBinding {
name: String,
},
HumanRole {
role: String,
description: String,
responsibilities: Vec<String>,
channel_hints: Vec<String>,
},
LlmProvider {
name: String,
tested_value: String,
},
BudgetOverride {
name: String,
tested_value: String,
},
Endpoint {
name: String,
tested_value: String,
},
AuthMode {
name: String,
tested_value: String,
},
}
impl SlotType {
pub fn key(&self) -> (&'static str, &str) {
match self {
SlotType::Secret { name } => ("secret", name.as_str()),
SlotType::OauthClient { name } => ("oauth_client", name.as_str()),
SlotType::ChannelBinding { name } => ("channel_binding", name.as_str()),
SlotType::HumanRole { role, .. } => ("human_role", role.as_str()),
SlotType::LlmProvider { name, .. } => ("llm_provider", name.as_str()),
SlotType::BudgetOverride { name, .. } => ("budget_override", name.as_str()),
SlotType::Endpoint { name, .. } => ("endpoint", name.as_str()),
SlotType::AuthMode { name, .. } => ("auth_mode", name.as_str()),
}
}
pub fn tested_value(&self) -> Option<&str> {
match self {
SlotType::Secret { .. }
| SlotType::OauthClient { .. }
| SlotType::ChannelBinding { .. }
| SlotType::HumanRole { .. } => None,
SlotType::LlmProvider { tested_value, .. }
| SlotType::BudgetOverride { tested_value, .. }
| SlotType::Endpoint { tested_value, .. }
| SlotType::AuthMode { tested_value, .. } => Some(tested_value.as_str()),
}
}
pub fn with_tested_value(&self, value: &str) -> Option<SlotType> {
match self {
SlotType::Secret { .. }
| SlotType::OauthClient { .. }
| SlotType::ChannelBinding { .. }
| SlotType::HumanRole { .. } => None,
SlotType::LlmProvider { name, .. } => Some(SlotType::LlmProvider {
name: name.clone(),
tested_value: value.to_string(),
}),
SlotType::BudgetOverride { name, .. } => Some(SlotType::BudgetOverride {
name: name.clone(),
tested_value: value.to_string(),
}),
SlotType::Endpoint { name, .. } => Some(SlotType::Endpoint {
name: name.clone(),
tested_value: value.to_string(),
}),
SlotType::AuthMode { name, .. } => Some(SlotType::AuthMode {
name: name.clone(),
tested_value: value.to_string(),
}),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum SuppliedBy {
#[default]
Environment,
Platform,
Runtime,
}
impl SuppliedBy {
#[must_use]
pub fn is_operator_supplied(self) -> bool {
matches!(self, Self::Environment)
}
}
fn is_environment_supplied(supplied_by: &SuppliedBy) -> bool {
supplied_by.is_operator_supplied()
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[non_exhaustive]
pub struct ConfigSlot {
pub slot: SlotType,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub config_key: Option<String>,
#[serde(default, skip_serializing_if = "is_environment_supplied")]
pub supplied_by: SuppliedBy,
}
impl ConfigSlot {
pub fn new(slot: SlotType) -> Self {
Self {
slot,
config_key: None,
supplied_by: SuppliedBy::Environment,
}
}
#[must_use]
pub fn with_config_key(mut self, key: impl Into<String>) -> Self {
self.config_key = Some(key.into());
self
}
#[must_use]
pub fn with_supplied_by(mut self, supplied_by: SuppliedBy) -> Self {
self.supplied_by = supplied_by;
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn secret_round_trips_with_snake_case_discriminator() {
let slot = SlotType::Secret {
name: "LICHESS_API_KEY".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "secret");
assert_eq!(json["name"], "LICHESS_API_KEY");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn oauth_client_round_trips_with_snake_case_discriminator() {
let slot = SlotType::OauthClient {
name: "primary-oauth".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "oauth_client");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn channel_binding_round_trips_with_snake_case_discriminator() {
let slot = SlotType::ChannelBinding {
name: "notify-channel".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "channel_binding");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn human_role_round_trips_with_all_fields_and_no_identity_field() {
let slot = SlotType::HumanRole {
role: "approver".to_string(),
description: "Approves budget overrides".to_string(),
responsibilities: vec!["review".to_string(), "approve".to_string()],
channel_hints: vec!["slack".to_string()],
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "human_role");
assert_eq!(json["role"], "approver");
assert!(json.get("userId").is_none());
assert!(json.get("channelId").is_none());
assert!(json.get("email").is_none());
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn llm_provider_round_trips_with_tested_value() {
let slot = SlotType::LlmProvider {
name: "primary-llm".to_string(),
tested_value: "anthropic".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "llm_provider");
assert_eq!(json["tested_value"], "anthropic");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn budget_override_round_trips_with_tested_value() {
let slot = SlotType::BudgetOverride {
name: "monthly-cap".to_string(),
tested_value: "1000".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "budget_override");
assert_eq!(json["tested_value"], "1000");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn secret_variant_constructs_with_only_a_name_field() {
let _ = SlotType::Secret {
name: "X".to_string(),
};
}
#[test]
fn key_uses_role_as_identifying_field_for_human_role() {
let slot = SlotType::HumanRole {
role: "approver".to_string(),
description: String::new(),
responsibilities: vec![],
channel_hints: vec![],
};
assert_eq!(slot.key(), ("human_role", "approver"));
}
#[test]
fn tested_value_is_none_for_identity_bearing_variants() {
let slot = SlotType::Secret {
name: "X".to_string(),
};
assert_eq!(slot.tested_value(), None);
}
#[test]
fn tested_value_is_some_for_behavior_relevant_variants() {
let slot = SlotType::LlmProvider {
name: "primary-llm".to_string(),
tested_value: "anthropic".to_string(),
};
assert_eq!(slot.tested_value(), Some("anthropic"));
}
#[test]
fn endpoint_round_trips_with_tested_value() {
let slot = SlotType::Endpoint {
name: "backend.base_url".to_string(),
tested_value: "https://api.tfl.gov.uk".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "endpoint");
assert_eq!(json["name"], "backend.base_url");
assert_eq!(json["tested_value"], "https://api.tfl.gov.uk");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn auth_mode_round_trips_with_tested_value() {
let slot = SlotType::AuthMode {
name: "backend.auth.type".to_string(),
tested_value: "api_key".to_string(),
};
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["type"], "auth_mode");
assert_eq!(json["name"], "backend.auth.type");
assert_eq!(json["tested_value"], "api_key");
let round: SlotType = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn key_uses_endpoint_and_auth_mode_discriminators() {
let endpoint = SlotType::Endpoint {
name: "backend.base_url".to_string(),
tested_value: "https://api.tfl.gov.uk".to_string(),
};
assert_eq!(endpoint.key(), ("endpoint", "backend.base_url"));
let auth_mode = SlotType::AuthMode {
name: "backend.auth.type".to_string(),
tested_value: "api_key".to_string(),
};
assert_eq!(auth_mode.key(), ("auth_mode", "backend.auth.type"));
}
#[test]
fn config_slot_without_a_key_emits_no_config_key_field_at_all() {
let slot = ConfigSlot::new(SlotType::Secret {
name: "X".to_string(),
});
assert_eq!(slot.config_key, None);
let json = serde_json::to_value(&slot).unwrap();
assert!(json.get("config_key").is_none());
assert_eq!(
json,
serde_json::json!({"slot": {"type": "secret", "name": "X"}})
);
}
#[test]
fn config_slot_with_a_key_serializes_it_and_round_trips() {
let slot = ConfigSlot::new(SlotType::Endpoint {
name: "backend.base_url".to_string(),
tested_value: "https://api.tfl.gov.uk".to_string(),
})
.with_config_key("backend.base_url");
let json = serde_json::to_value(&slot).unwrap();
assert_eq!(json["config_key"], "backend.base_url");
let round: ConfigSlot = serde_json::from_value(json).unwrap();
assert_eq!(round, slot);
}
#[test]
fn legacy_config_slot_json_without_config_key_deserializes_to_none() {
let legacy = serde_json::json!({"slot": {"type": "secret", "name": "X"}});
let slot: ConfigSlot = serde_json::from_value(legacy).unwrap();
assert_eq!(slot.config_key, None);
assert_eq!(
slot.slot,
SlotType::Secret {
name: "X".to_string()
}
);
}
#[test]
fn tested_value_is_some_for_the_two_new_behavior_relevant_variants() {
let endpoint = SlotType::Endpoint {
name: "backend.base_url".to_string(),
tested_value: "https://api.tfl.gov.uk".to_string(),
};
assert_eq!(endpoint.tested_value(), Some("https://api.tfl.gov.uk"));
let auth_mode = SlotType::AuthMode {
name: "backend.auth.type".to_string(),
tested_value: "api_key".to_string(),
};
assert_eq!(auth_mode.tested_value(), Some("api_key"));
}
}