use super::*;
pub(super) const RAISED_FILTER_IDS: &[&str] = &["raised", "all", "caught"];
pub(super) const THROW_FILTER_IDS: &[&str] = &["cpp_throw", "objc_throw", "swift_throw"];
pub(super) fn raised_filter_ids(offered: &[(String, bool)]) -> Option<Vec<String>> {
let has = |id: &str| offered.iter().any(|(o, _)| o == id);
if let Some(id) = RAISED_FILTER_IDS.iter().find(|id| has(id)) {
return Some(vec![id.to_string()]);
}
let throws: Vec<String> = THROW_FILTER_IDS
.iter()
.filter(|id| has(id))
.map(|id| id.to_string())
.collect();
(!throws.is_empty()).then_some(throws)
}
pub(super) fn offered_exception_filters(caps: &Value) -> Option<Vec<(String, bool)>> {
let list = caps.get("exceptionBreakpointFilters")?.as_array()?;
Some(
list.iter()
.filter_map(|f| {
let id = f.get("filter")?.as_str()?.to_string();
Some((id, f.get("default").and_then(Value::as_bool) == Some(true)))
})
.collect(),
)
}
pub fn set_exception_breakpoints_request(filters: &[String]) -> Value {
json!({
"type": "request",
"command": "setExceptionBreakpoints",
"arguments": { "filters": filters }
})
}
pub fn exception_summary(response: &Value) -> Option<String> {
let body = response.get("body")?;
let text = |pointer: &str| {
body.pointer(pointer)
.and_then(Value::as_str)
.map(str::trim)
.unwrap_or("")
};
let id = text("/exceptionId")
.split("(note:")
.next()
.unwrap_or("")
.trim();
let description = [text("/description"), text("/details/message")]
.into_iter()
.find(|s| !s.is_empty())
.and_then(|s| s.lines().next())
.unwrap_or("")
.trim();
match (id.is_empty(), description.is_empty()) {
(true, true) => None,
(false, true) => Some(id.to_string()),
(true, false) => Some(description.to_string()),
(false, false) if description.starts_with(id) => Some(description.to_string()),
(false, false) => Some(format!("{id}: {description}")),
}
}
impl DapSession {
pub fn set_exception_filters(&mut self, filters: Vec<String>) {
self.exception_filters = filters;
let _ = self
.active()
.send(set_exception_breakpoints_request(&self.exception_filters));
}
pub fn raised_exception_filters(&self) -> Option<Vec<String>> {
raised_filter_ids(&offered_exception_filters(&self.capabilities)?)
}
pub fn toggle_raised_exceptions(&mut self) -> Option<bool> {
if self.capabilities.is_null() {
let on = !self.pending_raised.unwrap_or(false);
self.pending_raised = Some(on);
return Some(on);
}
let raised = self.raised_exception_filters()?;
let on = !raised.iter().any(|r| self.has_exception_filter(r));
let mut filters: Vec<String> = self
.exception_filters
.iter()
.filter(|f| !raised.contains(f))
.cloned()
.collect();
if on {
filters.extend(raised);
}
self.set_exception_filters(filters);
Some(on)
}
pub fn has_exception_filter(&self, filter: &str) -> bool {
self.exception_filters.iter().any(|f| f == filter)
}
}