use crate::list_envelope::{ListEnvelope, Reason, Total, Unit};
pub const SEARCH_COMMAND: &str = "search";
pub const SEARCH_LIST_ID: &str = "payload.results";
pub const SEARCH_UNIT: Unit = Unit::Results;
pub const SEARCH_NARROW: &[&str] = &["offset", "topK", "path", "includeTests"];
pub const SEARCH_WIRE_KEY: &str = "results_list_envelope";
pub fn skipped_files_envelope(shown: usize, total: usize) -> ListEnvelope {
ListEnvelope::new(
shown,
Total::Exact(total),
Unit::Files,
if shown < total {
vec![Reason::Cap]
} else {
vec![]
},
&["path"],
)
}
pub fn build_fallback_search_envelope(
shown: usize,
walk_limited: bool,
more_available: bool,
engine_capped: bool,
) -> Option<ListEnvelope> {
if !walk_limited && !more_available && !engine_capped {
return None;
}
let mut causes = Vec::with_capacity(3);
if walk_limited {
causes.push(Reason::Walk);
}
if engine_capped {
causes.push(Reason::Budget);
}
if more_available {
causes.push(Reason::Cap);
}
Some(ListEnvelope::new(
shown,
Total::AtLeast(if more_available {
shown.saturating_add(1)
} else {
shown
}),
Unit::Results,
causes,
SEARCH_NARROW,
))
}
pub fn build_exact_total_search_envelope(shown: usize, total: usize) -> Option<ListEnvelope> {
if shown >= total {
return None;
}
Some(ListEnvelope::new(
shown,
Total::Exact(total),
Unit::Results,
vec![Reason::Cap],
SEARCH_NARROW,
))
}
pub fn attach_projected_search_envelope(
map: &mut serde_json::Map<String, serde_json::Value>,
envelope: &ListEnvelope,
) {
let value = serde_json::to_value(envelope)
.expect("the concrete ListEnvelope representation is serializable");
map.insert(SEARCH_WIRE_KEY.to_string(), value);
}