BlueGit

ict rusty-teams

Files

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

outcome.rs

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