use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct OperationInfo {
pub operation: String,
pub object_id: String,
}
impl OperationInfo {
pub fn new(operation: impl Into<String>, object_id: impl Into<String>) -> Self {
Self {
operation: operation.into(),
object_id: object_id.into(),
}
}
}
pub trait DynamicContextProvider: Send + Sync + std::fmt::Debug {
fn provide_context(&self, info: &OperationInfo) -> HashMap<String, String>;
}
#[cfg(test)]
mod tests {
use super::*;
#[derive(Debug)]
struct MockContextProvider {
prefix: String,
}
impl DynamicContextProvider for MockContextProvider {
fn provide_context(&self, info: &OperationInfo) -> HashMap<String, String> {
let mut context = HashMap::new();
context.insert(
"test-header".to_string(),
format!("{}-{}", self.prefix, info.operation),
);
context.insert("object-id".to_string(), info.object_id.clone());
context
}
}
#[test]
fn test_operation_info_creation() {
let info = OperationInfo::new("describe_table", "workspace$my_table");
assert_eq!(info.operation, "describe_table");
assert_eq!(info.object_id, "workspace$my_table");
}
#[test]
fn test_context_provider_basic() {
let provider = MockContextProvider {
prefix: "test".to_string(),
};
let info = OperationInfo::new("list_tables", "workspace$ns");
let context = provider.provide_context(&info);
assert_eq!(
context.get("test-header"),
Some(&"test-list_tables".to_string())
);
assert_eq!(context.get("object-id"), Some(&"workspace$ns".to_string()));
}
#[test]
fn test_empty_context() {
#[derive(Debug)]
struct EmptyProvider;
impl DynamicContextProvider for EmptyProvider {
fn provide_context(&self, _info: &OperationInfo) -> HashMap<String, String> {
HashMap::new()
}
}
let provider = EmptyProvider;
let info = OperationInfo::new("list_tables", "ns");
let context = provider.provide_context(&info);
assert!(context.is_empty());
}
}