use crate::list_envelope::{ListEnvelope, Reason, Total, Unit};
pub const TRACE_TO_LIST_ID: &str = "payload.paths";
pub const TRACE_TO_UNIT: Unit = Unit::Paths;
pub const TRACE_TO_NARROW: &[&str] = &["depth", "includeTests"];
pub const TRACE_DATA_LIST_ID: &str = "payload.hops";
pub const TRACE_DATA_UNIT: Unit = Unit::Hops;
pub const TRACE_DATA_NARROW: &[&str] = &["depth"];
pub const TRACE_TO_WIRE_KEY: &str = "paths_list_envelope";
pub const TRACE_DATA_WIRE_KEY: &str = "hops_list_envelope";
pub fn build_trace_to_envelope(
shown: usize,
total_paths: usize,
max_depth_reached: bool,
budget_exhausted: bool,
) -> Option<ListEnvelope> {
let is_cap = shown < total_paths;
if !max_depth_reached && !budget_exhausted && !is_cap {
return None;
}
let mut causes = Vec::with_capacity(3);
if max_depth_reached {
causes.push(Reason::Depth);
}
if budget_exhausted {
causes.push(Reason::Budget);
}
if is_cap {
causes.push(Reason::Cap);
}
let total = if max_depth_reached || budget_exhausted {
Total::AtLeast(total_paths)
} else {
Total::Exact(total_paths)
};
Some(ListEnvelope::new(
shown,
total,
TRACE_TO_UNIT,
causes,
TRACE_TO_NARROW,
))
}
pub fn build_trace_data_envelope(shown: usize, depth_limited: bool) -> Option<ListEnvelope> {
if !depth_limited {
return None;
}
Some(ListEnvelope::new(
shown,
Total::AtLeast(shown),
TRACE_DATA_UNIT,
vec![Reason::Depth],
TRACE_DATA_NARROW,
))
}
pub fn attach_trace_to_envelope(value: &mut serde_json::Value, envelope: Option<&ListEnvelope>) {
if let Some(env) = envelope {
if let Some(obj) = value.as_object_mut() {
if let Ok(env_val) = serde_json::to_value(env) {
obj.insert(TRACE_TO_WIRE_KEY.to_string(), env_val);
}
}
}
}
pub fn attach_trace_data_envelope(value: &mut serde_json::Value, envelope: Option<&ListEnvelope>) {
if let Some(env) = envelope {
if let Some(obj) = value.as_object_mut() {
if let Ok(env_val) = serde_json::to_value(env) {
obj.insert(TRACE_DATA_WIRE_KEY.to_string(), env_val);
}
}
}
}