BlueGit

ict rusty-teams

Files

rusty-teams / crates / calling / src / outcome.rs

outcome.rs

outcome.rs 4.0 KiB Download Raw
1use serde_json::Value;
2
3use crate::trouter_events::CallEnd;
4
5const OUTCOME_KEYS: [&str; 6] = ["code", "subCode", "phrase", "reason", "resultCategories", "failureReason"];
6const MAX_FIELDS: usize = 12;
7
8pub fn outcome_summary(body: &Value) -> String {
9 let mut fields = Vec::new();
10 collect_fields(body, "", &mut fields);
11 if fields.is_empty() {
12 return format!("keys [{}]", top_level_keys(body));
13 }
14 fields.join(" ")
15}
16
17pub fn embedded_end(body: &Value) -> Option<CallEnd> {
18 let holder = find_code_holder(body)?;
19 Some(CallEnd {
20 code: holder["code"].as_i64()?,
21 sub_code: holder["subCode"].as_i64().unwrap_or_default(),
22 phrase: holder["phrase"].as_str().unwrap_or_default().to_owned(),
23 accepted_elsewhere_by: None,
24 })
25}
26
27pub fn failed_participant_count(body: &Value) -> usize {
28 body["participantInfos"].as_array().map_or(1, |infos| infos.len().max(1))
29}
30
31fn collect_fields(value: &Value, path: &str, fields: &mut Vec<String>) {
32 match value {
33 Value::Object(map) => {
34 for (key, child) in map {
35 if fields.len() >= MAX_FIELDS {
36 return;
37 }
38 let child_path = if path.is_empty() { key.clone() } else { format!("{path}.{key}") };
39 if OUTCOME_KEYS.contains(&key.as_str()) && !child.is_object() {
40 fields.push(format!("{child_path}={}", scalar_text(child)));
41 } else {
42 collect_fields(child, &child_path, fields);
43 }
44 }
45 }
46 Value::Array(items) => {
47 for (index, item) in items.iter().enumerate() {
48 collect_fields(item, &format!("{path}[{index}]"), fields);
49 }
50 }
51 _ => {}
52 }
53}
54
55fn scalar_text(value: &Value) -> String {
56 match value {
57 Value::String(text) => text.clone(),
58 other => other.to_string(),
59 }
60}
61
62fn top_level_keys(body: &Value) -> String {
63 body.as_object()
64 .map(|map| map.keys().cloned().collect::<Vec<_>>().join(","))
65 .unwrap_or_default()
66}
67
68fn find_code_holder(value: &Value) -> Option<&Value> {
69 match value {
70 Value::Object(map) => {
71 if map.get("code").is_some_and(Value::is_i64) {
72 return Some(value);
73 }
74 map.values().find_map(find_code_holder)
75 }
76 Value::Array(items) => items.iter().find_map(find_code_holder),
77 _ => None,
78 }
79}
80
81#[cfg(test)]
82mod tests {
83 use serde_json::json;
84
85 use super::*;
86
87 #[test]
88 fn the_summary_keeps_codes_and_drops_names() {
89 let body = json!({"conversationEnd": {"reason": "noError", "code": 480, "subCode": 10037, "phrase": "No endpoint",
90 "participant": {"displayName": "Bea", "id": "8:orgid:bea"}}});
91 let summary = outcome_summary(&body);
92 assert!(summary.contains("conversationEnd.code=480"));
93 assert!(summary.contains("conversationEnd.subCode=10037"));
94 assert!(summary.contains("conversationEnd.phrase=No endpoint"));
95 assert!(!summary.contains("Bea") && !summary.contains("orgid"));
96 }
97
98 #[test]
99 fn failures_count_every_listed_participant() {
100 assert_eq!(failed_participant_count(&json!({"participantInfos": [{"id": "a"}, {"id": "b"}]})), 2);
101 assert_eq!(failed_participant_count(&json!({"participantInfos": []})), 1);
102 assert_eq!(failed_participant_count(&json!({})), 1);
103 }
104
105 #[test]
106 fn a_body_without_codes_lists_its_keys() {
107 assert_eq!(outcome_summary(&json!({"conversationEnd": {}, "links": {}})), "keys [conversationEnd,links]");
108 }
109
110 #[test]
111 fn the_first_nested_code_becomes_the_end() {
112 let body = json!({"participantInfos": [{"id": "x", "failure": {"code": 408, "subCode": 10408, "phrase": "timeout"}}]});
113 let end = embedded_end(&body).unwrap();
114 assert_eq!((end.code, end.sub_code, end.phrase.as_str()), (408, 10408, "timeout"));
115 assert!(embedded_end(&json!({"conversationEnd": {"reason": "x"}})).is_none());
116 }
117}