manasight-parser 0.1.1

MTG Arena log file parser — reads Player.log and emits typed game events
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Shared utilities for parsing `==>` / `<==` API request/response pairs.
//!
//! MTG Arena logs API interactions as arrow-delimited entries:
//!
//! | Direction | Format | Example |
//! |-----------|--------|---------|
//! | Request (`==>`) | `==> MethodName {json}` | `==> EventJoin {"request":"..."}` |
//! | Response (`<==`) | `<== MethodName(uuid)\n{json}` | `<== RankGetCombinedRankInfo(a1b2c3d4-...)\n{...}` |
//!
//! These lines appear as continuation lines within a `[UnityCrossThreadLogger]`
//! entry — the `LogEntry.body` will contain the timestamp header line followed
//! by the `==>` or `<==` line and then the JSON payload.

/// Returns `true` if `body` contains a `<== method_name(` response marker.
///
/// Includes the `(` that immediately follows the method name in real log lines
/// (e.g., `<== StartHook(uuid)`), preventing false matches against methods
/// whose names share a common prefix (e.g., `StartHook` vs `StartHookV2`).
pub(crate) fn is_api_response(body: &str, method_name: &str) -> bool {
    let mut marker = String::with_capacity(5 + method_name.len());
    marker.push_str("<== ");
    marker.push_str(method_name);
    marker.push('(');
    body.contains(&marker)
}

/// Returns `true` if `body` contains a `==> method_name ` request marker.
///
/// Includes the space that immediately follows the method name in real log
/// lines (e.g., `==> EventJoin {"id":...}`), preventing false matches against
/// methods whose names share a common prefix (e.g., `EventJoin` vs
/// `EventJoinV2`).
pub(crate) fn is_api_request(body: &str, method_name: &str) -> bool {
    let mut marker = String::with_capacity(5 + method_name.len());
    marker.push_str("==> ");
    marker.push_str(method_name);
    marker.push(' ');
    body.contains(&marker)
}

/// Extracts the first JSON object or array from a multi-line log body.
///
/// Handles `[UnityCrossThreadLogger]` bracket headers by skipping past the
/// first `]` when the body starts with `[`, so header brackets are not
/// confused with JSON array delimiters.
///
/// Uses brace/bracket-depth counting that respects string literals to find
/// the complete JSON boundary.
pub(crate) fn extract_json_from_body(body: &str) -> Option<&str> {
    // If the body starts with a `[...]` header prefix, skip past it
    // so we don't match the header bracket as a JSON array start.
    let search_start = if body.starts_with('[') {
        body.find(']').map_or(0, |pos| pos + 1)
    } else {
        0
    };

    let search_region = &body[search_start..];
    let json_start = search_region.find(['{', '['])?;
    let json_start = search_start + json_start;

    let candidate = &body[json_start..];

    let first_byte = candidate.as_bytes().first().copied()?;
    let (open_char, close_char) = if first_byte == b'{' {
        ('{', '}')
    } else {
        ('[', ']')
    };

    let mut depth: i32 = 0;
    let mut in_string = false;
    let mut escape_next = false;
    let mut end_pos = None;

    for (i, ch) in candidate.char_indices() {
        if escape_next {
            escape_next = false;
            continue;
        }
        match ch {
            '\\' if in_string => {
                escape_next = true;
            }
            '"' => {
                in_string = !in_string;
            }
            c if !in_string && c == open_char => {
                depth += 1;
            }
            c if !in_string && c == close_char => {
                depth -= 1;
                if depth == 0 {
                    end_pos = Some(i + 1);
                    break;
                }
            }
            _ => {}
        }
    }

    end_pos.map(|end| &candidate[..end])
}

/// Extracts and parses JSON from a log body, warning on malformed payloads.
///
/// Combines [`extract_json_from_body`] with `serde_json::from_str`, logging
/// a warning with the given `context` label when JSON parsing fails. Returns
/// `None` if no JSON is found or if parsing fails.
pub(crate) fn parse_json_from_body(body: &str, context: &str) -> Option<serde_json::Value> {
    let json_str = extract_json_from_body(body)?;
    match serde_json::from_str(json_str) {
        Ok(v) => Some(v),
        Err(e) => {
            ::log::warn!("{context}: malformed JSON payload: {e}");
            None
        }
    }
}

/// Extracts an event name from a parsed JSON value.
///
/// MTG Arena is inconsistent about where it stores event names. This helper
/// checks the following locations in order:
/// 1. Top-level `EventName` or `InternalEventName`.
/// 2. `Course.InternalEventName` or `Course.EventName` (common in responses).
/// 3. A nested string-escaped `request` field containing `{"EventName": "..."}`
///    (common in outbound requests).
pub(crate) fn extract_event_name(parsed: &serde_json::Value) -> String {
    // 1. Try direct top-level fields.
    if let Some(name) = parsed
        .get("EventName")
        .or_else(|| parsed.get("InternalEventName"))
        .and_then(serde_json::Value::as_str)
    {
        return name.to_owned();
    }

    // 2. Try nested Course object (responses).
    if let Some(name) = parsed.get("Course").and_then(|course| {
        course
            .get("InternalEventName")
            .or_else(|| course.get("EventName"))
            .and_then(serde_json::Value::as_str)
    }) {
        return name.to_owned();
    }

    // 3. Try nested string-escaped request field (requests).
    if let Some(request_str) = parsed.get("request").and_then(serde_json::Value::as_str) {
        if let Ok(request_json) = serde_json::from_str::<serde_json::Value>(request_str) {
            if let Some(name) = request_json
                .get("EventName")
                .or_else(|| request_json.get("InternalEventName"))
                .and_then(serde_json::Value::as_str)
            {
                return name.to_owned();
            }
        }
    }

    String::new()
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;

    // -- is_api_response -------------------------------------------------------

    mod api_response {
        use super::*;

        #[test]
        fn test_is_api_response_matches_method() {
            let body = "[UnityCrossThreadLogger]2/22/2026 11:59:51 AM\n\
                         <== StartHook(e3f1a2b4-5678-9abc-def0-123456789abc)\n\
                         {\"InventoryInfo\": {}}";
            assert!(is_api_response(body, "StartHook"));
        }

        #[test]
        fn test_is_api_response_no_match_wrong_method() {
            let body = "<== RankGetCombinedRankInfo(uuid)\n{}";
            assert!(!is_api_response(body, "StartHook"));
        }

        #[test]
        fn test_is_api_response_no_match_request_arrow() {
            let body = "==> StartHook {\"data\": 1}";
            assert!(!is_api_response(body, "StartHook"));
        }

        #[test]
        fn test_is_api_response_no_match_empty() {
            assert!(!is_api_response("", "StartHook"));
        }

        #[test]
        fn test_is_api_response_no_match_prefix_method() {
            // "StartHook" must not match a hypothetical "StartHookV2" response.
            let body = "<== StartHookV2(uuid)\n{}";
            assert!(!is_api_response(body, "StartHook"));
        }
    }

    // -- is_api_request --------------------------------------------------------

    mod api_request {
        use super::*;

        #[test]
        fn test_is_api_request_matches_method() {
            let body = "[UnityCrossThreadLogger]==> EventJoin {\"request\": \"{}\"}";
            assert!(is_api_request(body, "EventJoin"));
        }

        #[test]
        fn test_is_api_request_no_match_wrong_method() {
            let body = "==> EventClaimPrize {}";
            assert!(!is_api_request(body, "EventJoin"));
        }

        #[test]
        fn test_is_api_request_no_match_response_arrow() {
            let body = "<== EventJoin(uuid)\n{}";
            assert!(!is_api_request(body, "EventJoin"));
        }

        #[test]
        fn test_is_api_request_no_match_empty() {
            assert!(!is_api_request("", "EventJoin"));
        }

        #[test]
        fn test_is_api_request_no_match_prefix_method() {
            // "EventJoin" must not match a hypothetical "EventJoinV2" request.
            let body = "==> EventJoinV2 {\"data\": 1}";
            assert!(!is_api_request(body, "EventJoin"));
        }
    }

    // -- extract_json_from_body ------------------------------------------------

    mod json_extraction {
        use super::*;

        #[test]
        fn test_extract_json_object() {
            let body = "header line\n{\"key\": \"value\"}";
            assert_eq!(extract_json_from_body(body), Some("{\"key\": \"value\"}"));
        }

        #[test]
        fn test_extract_json_array() {
            let body = "header line\n[1, 2, 3]";
            assert_eq!(extract_json_from_body(body), Some("[1, 2, 3]"));
        }

        #[test]
        fn test_extract_json_with_bracket_header() {
            let body = "[UnityCrossThreadLogger]some text\n{\"data\": 1}";
            assert_eq!(extract_json_from_body(body), Some("{\"data\": 1}"));
        }

        #[test]
        fn test_extract_json_nested_objects() {
            let body = "header\n{\"outer\": {\"inner\": 1}}";
            assert_eq!(
                extract_json_from_body(body),
                Some("{\"outer\": {\"inner\": 1}}")
            );
        }

        #[test]
        fn test_extract_json_with_string_braces() {
            let body = "header\n{\"msg\": \"hello {world}\"}";
            assert_eq!(
                extract_json_from_body(body),
                Some("{\"msg\": \"hello {world}\"}")
            );
        }

        #[test]
        fn test_extract_json_no_json() {
            assert!(extract_json_from_body("no json here").is_none());
        }

        #[test]
        fn test_extract_json_multiline() {
            let body = "[UnityCrossThreadLogger]2/22/2026 11:59:51 AM\n\
                         <== StartHook(uuid)\n\
                         {\n\
                           \"InventoryInfo\": {\"Gems\": 1234}\n\
                         }";
            let json = extract_json_from_body(body);
            assert!(json.is_some());
            let parsed: serde_json::Value =
                serde_json::from_str(json.unwrap_or("{}")).unwrap_or_default();
            assert_eq!(parsed["InventoryInfo"]["Gems"], 1234);
        }

        #[test]
        fn test_extract_json_unclosed_brace() {
            let body = "header {\"key\": \"value\"";
            assert!(extract_json_from_body(body).is_none());
        }

        #[test]
        fn test_extract_json_brace_in_string() {
            let body = r#"text {"key": "value with { braces }"}"#;
            assert_eq!(
                extract_json_from_body(body),
                Some(r#"{"key": "value with { braces }"}"#)
            );
        }

        #[test]
        fn test_extract_json_escaped_quote_in_string() {
            let body = r#"prefix {"key": "val\"ue"}"#;
            assert_eq!(extract_json_from_body(body), Some(r#"{"key": "val\"ue"}"#));
        }
    }

    // -- parse_json_from_body --------------------------------------------------

    mod parse_json {
        use super::*;

        #[test]
        fn test_parse_json_from_body_valid_object() {
            let body = "header\n{\"key\": 42}";
            let result = parse_json_from_body(body, "test");
            assert_eq!(result, Some(serde_json::json!({"key": 42})));
        }

        #[test]
        fn test_parse_json_from_body_no_json_returns_none() {
            assert!(parse_json_from_body("no json", "test").is_none());
        }

        #[test]
        fn test_parse_json_from_body_malformed_json_returns_none() {
            let body = "header\n{invalid}";
            assert!(parse_json_from_body(body, "test").is_none());
        }

        #[test]
        fn test_parse_json_from_body_valid_array() {
            let body = "header\n[1, 2, 3]";
            let result = parse_json_from_body(body, "test");
            assert_eq!(result, Some(serde_json::json!([1, 2, 3])));
        }
    }

    // -- extract_event_name ----------------------------------------------------
    mod event_name {
        use super::*;

        #[test]
        fn test_extract_event_name_top_level_event_name_returns_name() {
            let parsed = serde_json::json!({"EventName": "DirectEvent"});
            assert_eq!(extract_event_name(&parsed), "DirectEvent");
        }

        #[test]
        fn test_extract_event_name_top_level_internal_name_returns_name() {
            let parsed = serde_json::json!({"InternalEventName": "InternalTest"});
            assert_eq!(extract_event_name(&parsed), "InternalTest");
        }

        #[test]
        fn test_extract_event_name_course_nested_returns_name() {
            let parsed = serde_json::json!({
                "Course": {"InternalEventName": "CourseInternal"}
            });
            assert_eq!(extract_event_name(&parsed), "CourseInternal");
        }

        #[test]
        fn test_extract_event_name_string_escaped_request_returns_name() {
            let parsed = serde_json::json!({
                "id": "test",
                "request": "{\"EventName\":\"NestedRequest\"}"
            });
            assert_eq!(extract_event_name(&parsed), "NestedRequest");
        }

        #[test]
        fn test_extract_event_name_top_level_wins_over_course_and_request_returns_top_level() {
            // Top-level should win over Course, which should win over request.
            let parsed = serde_json::json!({
                "EventName": "TopLevel",
                "Course": {"EventName": "CourseLevel"},
                "request": "{\"EventName\":\"RequestLevel\"}"
            });
            assert_eq!(extract_event_name(&parsed), "TopLevel");
        }

        #[test]
        fn test_extract_event_name_no_matching_field_returns_empty() {
            let parsed = serde_json::json!({"id": "test"});
            assert_eq!(extract_event_name(&parsed), "");
        }

        #[test]
        fn test_extract_event_name_malformed_request_json_returns_empty() {
            let parsed = serde_json::json!({"request": "not json"});
            assert_eq!(extract_event_name(&parsed), "");
        }
    }
}