use crate::Result;
use chrono::Utc;
use serde_json::Value;
use std::collections::HashMap;
use super::types::*;
#[derive(Debug)]
pub struct FailureContextCollector;
impl FailureContextCollector {
pub fn new() -> Self {
Self
}
pub fn collect_context(
&self,
method: &str,
path: &str,
status_code: Option<u16>,
error_message: Option<String>,
) -> Result<FailureContext> {
let request = RequestDetails {
method: method.to_string(),
path: path.to_string(),
headers: HashMap::new(),
query_params: HashMap::new(),
body: None,
};
let response = status_code.map(|code| ResponseDetails {
status_code: code,
headers: HashMap::new(),
body: None,
duration_ms: None,
});
Ok(FailureContext {
request,
response,
chaos_configs: Vec::new(),
consistency_rules: Vec::new(),
contract_validation: None,
behavioral_rules: Vec::new(),
hook_results: Vec::new(),
error_message,
timestamp: Utc::now(),
})
}
#[allow(clippy::too_many_arguments)]
pub fn collect_context_with_details(
&self,
method: &str,
path: &str,
headers: HashMap<String, String>,
query_params: HashMap<String, String>,
body: Option<Value>,
status_code: Option<u16>,
response_headers: HashMap<String, String>,
response_body: Option<Value>,
duration_ms: Option<u64>,
error_message: Option<String>,
chaos_configs: Vec<ChaosConfigInfo>,
consistency_rules: Vec<ConsistencyRuleInfo>,
contract_validation: Option<ContractValidationInfo>,
behavioral_rules: Vec<BehavioralRuleInfo>,
hook_results: Vec<HookExecutionInfo>,
) -> Result<FailureContext> {
let request = RequestDetails {
method: method.to_string(),
path: path.to_string(),
headers,
query_params,
body,
};
let response = status_code.map(|code| ResponseDetails {
status_code: code,
headers: response_headers,
body: response_body,
duration_ms,
});
Ok(FailureContext {
request,
response,
chaos_configs,
consistency_rules,
contract_validation,
behavioral_rules,
hook_results,
error_message,
timestamp: Utc::now(),
})
}
}
impl Default for FailureContextCollector {
fn default() -> Self {
Self::new()
}
}