munin-msbuild 0.3.0

Reader and seekable indexed data model for MSBuild binary log (.binlog) files.
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
// Copyright (c) Michael Grier

//! Typed event structs and reader functions for MSBuild binary log events.
//!
//! Each event type has:
//! - A struct holding the decoded fields.
//! - A `read_*` function that deserializes the event from a stream, given
//!   the common `BuildEventArgsFields` prefix and the string/NVL tables.
//!
//! The deserialization order and field presence logic matches the C# reference
//! implementation in `BuildEventArgsReader.cs`. Changing the read order or
//! field semantics is a breaking change.

use std::io::Read;

use crate::{
    context::{read_build_event_context, BuildEventContext},
    error::MuninError,
    fields::{read_build_event_args_fields, BuildEventArgsFields},
    nvl_table::NameValueListTable,
    primitives::{read_7bit_count, read_7bit_int, read_bool},
    readers::{read_dedup_string, read_optional_string, read_string_dictionary},
    string_table::StringTable,
};

// ---------------------------------------------------------------------------
// Helper: read diagnostic fields (Error, Warning, CriticalBuildMessage, Message)
// ---------------------------------------------------------------------------

/// Read the 8 diagnostic location fields that are always present (not flag-driven)
/// for Error, Warning, and CriticalBuildMessage events.
fn read_diagnostic_fields(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<DiagnosticLocation, MuninError> {
    let subcategory = read_optional_string(reader, strings, file_format_version)?;
    let code = read_optional_string(reader, strings, file_format_version)?;
    let file = read_optional_string(reader, strings, file_format_version)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let line_number = read_7bit_int(reader)?;
    let column_number = read_7bit_int(reader)?;
    let end_line_number = read_7bit_int(reader)?;
    let end_column_number = read_7bit_int(reader)?;
    Ok(DiagnosticLocation {
        subcategory,
        code,
        file,
        project_file,
        line_number,
        column_number,
        end_line_number,
        end_column_number,
    })
}

/// Location fields for diagnostic events (Error, Warning, CriticalBuildMessage).
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct DiagnosticLocation {
    pub subcategory: Option<String>,
    pub code: Option<String>,
    pub file: Option<String>,
    pub project_file: Option<String>,
    pub line_number: i32,
    pub column_number: i32,
    pub end_line_number: i32,
    pub end_column_number: i32,
}

// ---------------------------------------------------------------------------
// MN-11: Build/Project lifecycle events
// ---------------------------------------------------------------------------

/// `BuildStarted` event — emitted once at the start of the build.
#[derive(Debug, Clone)]
pub struct BuildStartedEvent {
    pub fields: BuildEventArgsFields,
    /// Environment variables at build start (key → value).
    pub environment: Option<Vec<(String, String)>>,
}

/// Read a `BuildStarted` event from the stream.
pub fn read_build_started(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<BuildStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let environment = read_string_dictionary(reader, strings, nvl_table, file_format_version)?;
    Ok(BuildStartedEvent {
        fields,
        environment,
    })
}

/// `BuildFinished` event — emitted once at the end of the build.
#[derive(Debug, Clone)]
pub struct BuildFinishedEvent {
    pub fields: BuildEventArgsFields,
    pub succeeded: bool,
}

/// Read a `BuildFinished` event from the stream.
pub fn read_build_finished(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildFinishedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let succeeded = read_bool(reader)?;
    Ok(BuildFinishedEvent { fields, succeeded })
}

/// `ProjectStarted` event.
#[derive(Debug, Clone)]
pub struct ProjectStartedEvent {
    pub fields: BuildEventArgsFields,
    pub parent_context: Option<BuildEventContext>,
    pub project_file: Option<String>,
    pub project_id: i32,
    pub target_names: Option<String>,
    pub tools_version: Option<String>,
    pub global_properties: Option<Vec<(String, String)>>,
    pub property_list: Option<Vec<(String, String)>>,
    pub item_list: Option<Vec<ItemGroup>>,
}

/// Read a `ProjectStarted` event from the stream.
pub fn read_project_started(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<ProjectStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;

    let parent_context = if read_bool(reader)? {
        Some(read_build_event_context(reader, file_format_version)?)
    } else {
        None
    };

    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let project_id = read_7bit_int(reader)?;
    let target_names = read_dedup_string(reader, strings)?;
    let tools_version = read_optional_string(reader, strings, file_format_version)?;

    let global_properties = if file_format_version > 6 {
        // In v18+ global properties are always written. In v7-17 a bool prefix
        // controls presence.
        if file_format_version >= 18 || read_bool(reader)? {
            read_string_dictionary(reader, strings, nvl_table, file_format_version)?
        } else {
            None
        }
    } else {
        None
    };

    let property_list = read_property_list(reader, strings, nvl_table, file_format_version)?;
    let item_list = read_project_items(reader, strings, nvl_table, file_format_version)?;

    Ok(ProjectStartedEvent {
        fields,
        parent_context,
        project_file,
        project_id,
        target_names,
        tools_version,
        global_properties,
        property_list,
        item_list,
    })
}

/// `ProjectFinished` event.
#[derive(Debug, Clone)]
pub struct ProjectFinishedEvent {
    pub fields: BuildEventArgsFields,
    pub project_file: Option<String>,
    pub succeeded: bool,
}

/// Read a `ProjectFinished` event from the stream.
pub fn read_project_finished(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<ProjectFinishedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let succeeded = read_bool(reader)?;
    Ok(ProjectFinishedEvent {
        fields,
        project_file,
        succeeded,
    })
}

// ---------------------------------------------------------------------------
// MN-12: Target lifecycle events
// ---------------------------------------------------------------------------

/// `TargetStarted` event.
#[derive(Debug, Clone)]
pub struct TargetStartedEvent {
    pub fields: BuildEventArgsFields,
    pub target_name: Option<String>,
    pub project_file: Option<String>,
    pub target_file: Option<String>,
    pub parent_target: Option<String>,
    /// `TargetBuiltReason` (introduced in format version 4).
    pub build_reason: i32,
}

/// Read a `TargetStarted` event from the stream.
pub fn read_target_started(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<TargetStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let target_name = read_optional_string(reader, strings, file_format_version)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let target_file = read_optional_string(reader, strings, file_format_version)?;
    let parent_target = read_optional_string(reader, strings, file_format_version)?;
    let build_reason = if file_format_version > 3 {
        read_7bit_int(reader)?
    } else {
        0 // TargetBuiltReason::None
    };
    Ok(TargetStartedEvent {
        fields,
        target_name,
        project_file,
        target_file,
        parent_target,
        build_reason,
    })
}

/// `TargetFinished` event.
#[derive(Debug, Clone)]
pub struct TargetFinishedEvent {
    pub fields: BuildEventArgsFields,
    pub succeeded: bool,
    pub project_file: Option<String>,
    pub target_file: Option<String>,
    pub target_name: Option<String>,
    pub target_outputs: Option<Vec<TaskItem>>,
}

/// Read a `TargetFinished` event from the stream.
pub fn read_target_finished(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<TargetFinishedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let succeeded = read_bool(reader)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let target_file = read_optional_string(reader, strings, file_format_version)?;
    let target_name = read_optional_string(reader, strings, file_format_version)?;
    let target_outputs = read_task_item_list(reader, strings, nvl_table, file_format_version)?;
    Ok(TargetFinishedEvent {
        fields,
        succeeded,
        project_file,
        target_file,
        target_name,
        target_outputs,
    })
}

/// `TargetSkipped` event.
#[derive(Debug, Clone)]
pub struct TargetSkippedEvent {
    pub fields: BuildEventArgsFields,
    pub target_file: Option<String>,
    pub target_name: Option<String>,
    pub parent_target: Option<String>,
    pub condition: Option<String>,
    pub evaluated_condition: Option<String>,
    pub originally_succeeded: bool,
    /// `TargetSkipReason` (raw i32, introduced in format version 14).
    pub skip_reason: i32,
    pub build_reason: i32,
    pub original_build_event_context: Option<BuildEventContext>,
}

/// Read a `TargetSkipped` event from the stream.
pub fn read_target_skipped(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<TargetSkippedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let target_file = read_optional_string(reader, strings, file_format_version)?;
    let target_name = read_optional_string(reader, strings, file_format_version)?;
    let parent_target = read_optional_string(reader, strings, file_format_version)?;

    let mut condition = None;
    let mut evaluated_condition = None;
    let mut originally_succeeded = false;
    let mut skip_reason = 0; // TargetSkipReason::None

    if file_format_version >= 13 {
        condition = read_optional_string(reader, strings, file_format_version)?;
        evaluated_condition = read_optional_string(reader, strings, file_format_version)?;
        originally_succeeded = read_bool(reader)?;

        // Infer skip reason from available data (matches C# reference).
        skip_reason = if condition.is_some() {
            1 // TargetSkipReason::ConditionWasFalse
        } else if originally_succeeded {
            2 // TargetSkipReason::PreviouslyBuiltSuccessfully
        } else {
            3 // TargetSkipReason::PreviouslyBuiltUnsuccessfully
        };
    }

    let build_reason = read_7bit_int(reader)?;

    let mut original_build_event_context = None;
    if file_format_version >= 14 {
        skip_reason = read_7bit_int(reader)?;
        // Optional BuildEventContext (uses a format like BinaryReaderExtensions)
        if read_bool(reader)? {
            original_build_event_context =
                Some(read_build_event_context(reader, file_format_version)?);
        }
    }

    Ok(TargetSkippedEvent {
        fields,
        target_file,
        target_name,
        parent_target,
        condition,
        evaluated_condition,
        originally_succeeded,
        skip_reason,
        build_reason,
        original_build_event_context,
    })
}

// ---------------------------------------------------------------------------
// MN-13: Task lifecycle events
// ---------------------------------------------------------------------------

/// `TaskStarted` event.
#[derive(Debug, Clone)]
pub struct TaskStartedEvent {
    pub fields: BuildEventArgsFields,
    pub task_name: Option<String>,
    pub project_file: Option<String>,
    pub task_file: Option<String>,
    pub task_assembly_location: Option<String>,
}

/// Read a `TaskStarted` event from the stream.
pub fn read_task_started(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<TaskStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let task_name = read_optional_string(reader, strings, file_format_version)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let task_file = read_optional_string(reader, strings, file_format_version)?;
    let task_assembly_location = if file_format_version > 19 {
        read_optional_string(reader, strings, file_format_version)?
    } else {
        None
    };
    Ok(TaskStartedEvent {
        fields,
        task_name,
        project_file,
        task_file,
        task_assembly_location,
    })
}

/// `TaskFinished` event.
#[derive(Debug, Clone)]
pub struct TaskFinishedEvent {
    pub fields: BuildEventArgsFields,
    pub succeeded: bool,
    pub task_name: Option<String>,
    pub project_file: Option<String>,
    pub task_file: Option<String>,
}

/// Read a `TaskFinished` event from the stream.
pub fn read_task_finished(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<TaskFinishedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let succeeded = read_bool(reader)?;
    let task_name = read_optional_string(reader, strings, file_format_version)?;
    let project_file = read_optional_string(reader, strings, file_format_version)?;
    let task_file = read_optional_string(reader, strings, file_format_version)?;
    Ok(TaskFinishedEvent {
        fields,
        succeeded,
        task_name,
        project_file,
        task_file,
    })
}

/// `TaskCommandLine` event — the command line used to invoke an external tool.
#[derive(Debug, Clone)]
pub struct TaskCommandLineEvent {
    pub fields: BuildEventArgsFields,
    pub command_line: Option<String>,
    pub task_name: Option<String>,
}

/// Read a `TaskCommandLine` event from the stream.
pub fn read_task_command_line(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<TaskCommandLineEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let command_line = read_optional_string(reader, strings, file_format_version)?;
    let task_name = read_optional_string(reader, strings, file_format_version)?;
    Ok(TaskCommandLineEvent {
        fields,
        command_line,
        task_name,
    })
}

/// `TaskParameter` event — input/output parameters of a task.
#[derive(Debug, Clone)]
pub struct TaskParameterEvent {
    pub fields: BuildEventArgsFields,
    /// `TaskParameterMessageKind` (raw i32).
    pub kind: i32,
    pub item_type: Option<String>,
    pub items: Option<Vec<TaskItem>>,
    pub parameter_name: Option<String>,
    pub property_name: Option<String>,
}

/// Read a `TaskParameter` event from the stream.
pub fn read_task_parameter(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<TaskParameterEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let kind = read_7bit_int(reader)?;
    let item_type = read_dedup_string(reader, strings)?;
    let items = read_task_item_list(reader, strings, nvl_table, file_format_version)?;
    let (parameter_name, property_name) = if file_format_version >= 21 {
        (
            read_dedup_string(reader, strings)?,
            read_dedup_string(reader, strings)?,
        )
    } else {
        (None, None)
    };
    Ok(TaskParameterEvent {
        fields,
        kind,
        item_type,
        items,
        parameter_name,
        property_name,
    })
}

// ---------------------------------------------------------------------------
// MN-14: Diagnostic events (Error, Warning, Message, CriticalBuildMessage)
// ---------------------------------------------------------------------------

/// A build error event.
#[derive(Debug, Clone)]
pub struct BuildErrorEvent {
    pub fields: BuildEventArgsFields,
    pub location: DiagnosticLocation,
}

/// Read a `BuildError` event from the stream.
pub fn read_build_error(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildErrorEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let location = read_diagnostic_fields(reader, strings, file_format_version)?;
    Ok(BuildErrorEvent { fields, location })
}

/// A build warning event.
#[derive(Debug, Clone)]
pub struct BuildWarningEvent {
    pub fields: BuildEventArgsFields,
    pub location: DiagnosticLocation,
}

/// Read a `BuildWarning` event from the stream.
pub fn read_build_warning(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildWarningEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let location = read_diagnostic_fields(reader, strings, file_format_version)?;
    Ok(BuildWarningEvent { fields, location })
}

/// A build message event.
#[derive(Debug, Clone)]
pub struct BuildMessageEvent {
    pub fields: BuildEventArgsFields,
}

/// Read a `BuildMessage` event from the stream.
pub fn read_build_message(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildMessageEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    Ok(BuildMessageEvent { fields })
}

/// A critical build message event (same layout as message, different severity).
#[derive(Debug, Clone)]
pub struct CriticalBuildMessageEvent {
    pub fields: BuildEventArgsFields,
}

/// Read a `CriticalBuildMessage` event from the stream.
pub fn read_critical_build_message(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<CriticalBuildMessageEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    Ok(CriticalBuildMessageEvent { fields })
}

// ---------------------------------------------------------------------------
// MN-15: Evaluation events
// ---------------------------------------------------------------------------

/// `ProjectEvaluationStarted` event.
#[derive(Debug, Clone)]
pub struct ProjectEvaluationStartedEvent {
    pub fields: BuildEventArgsFields,
    pub project_file: Option<String>,
}

/// Read a `ProjectEvaluationStarted` event from the stream.
pub fn read_project_evaluation_started(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<ProjectEvaluationStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let project_file = read_dedup_string(reader, strings)?;
    Ok(ProjectEvaluationStartedEvent {
        fields,
        project_file,
    })
}

/// `ProjectEvaluationFinished` event.
#[derive(Debug, Clone)]
pub struct ProjectEvaluationFinishedEvent {
    pub fields: BuildEventArgsFields,
    pub project_file: Option<String>,
    pub global_properties: Option<Vec<(String, String)>>,
    pub property_list: Option<Vec<(String, String)>>,
    pub item_list: Option<Vec<ItemGroup>>,
    pub has_profile_data: bool,
    pub profile_data: Option<Vec<ProfileEntry>>,
}

/// A single profiler entry from evaluation profiling data.
#[derive(Debug, Clone)]
pub struct ProfileEntry {
    pub location: EvaluationLocation,
    pub profiled_location: ProfiledLocation,
}

/// Location data for an evaluation profiler entry.
#[derive(Debug, Clone)]
pub struct EvaluationLocation {
    pub element_name: Option<String>,
    pub description: Option<String>,
    pub evaluation_description: Option<String>,
    pub file: Option<String>,
    pub kind: i32,
    pub evaluation_pass: i32,
    pub line: Option<i32>,
    pub id: i64,
    pub parent_id: Option<i64>,
}

/// Timing data for an evaluation profiler entry.
#[derive(Debug, Clone, Copy)]
pub struct ProfiledLocation {
    pub number_of_hits: i32,
    /// Exclusive time in ticks.
    pub exclusive_time_ticks: i64,
    /// Inclusive time in ticks.
    pub inclusive_time_ticks: i64,
}

/// Read a `ProjectEvaluationFinished` event from the stream.
pub fn read_project_evaluation_finished(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<ProjectEvaluationFinishedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let project_file = read_dedup_string(reader, strings)?;

    let mut global_properties = None;
    let mut property_list = None;
    let mut item_list = None;
    let mut has_profile_data = false;
    let mut profile_data = None;

    if file_format_version >= 12 {
        // In v18+ global properties are always written. In v12-17 a bool prefix.
        if file_format_version >= 18 || read_bool(reader)? {
            global_properties =
                read_string_dictionary(reader, strings, nvl_table, file_format_version)?;
        }

        property_list = read_property_list(reader, strings, nvl_table, file_format_version)?;
        item_list = read_project_items(reader, strings, nvl_table, file_format_version)?;
    }

    // ProfilerResult introduced in version 5.
    if file_format_version > 4 {
        has_profile_data = read_bool(reader)?;
        if has_profile_data {
            let count = read_7bit_count(reader, "profile entry count")?;
            let mut entries = Vec::with_capacity(count);
            for _ in 0..count {
                let location = read_evaluation_location(reader, strings, file_format_version)?;
                let profiled = read_profiled_location(reader)?;
                entries.push(ProfileEntry {
                    location,
                    profiled_location: profiled,
                });
            }
            profile_data = Some(entries);
        }
    }

    Ok(ProjectEvaluationFinishedEvent {
        fields,
        project_file,
        global_properties,
        property_list,
        item_list,
        has_profile_data,
        profile_data,
    })
}

// ---------------------------------------------------------------------------
// MN-16: Property event types
// ---------------------------------------------------------------------------

/// `PropertyReassignment` event — a property value was overwritten.
#[derive(Debug, Clone)]
pub struct PropertyReassignmentEvent {
    pub fields: BuildEventArgsFields,
    pub property_name: Option<String>,
    pub previous_value: Option<String>,
    pub new_value: Option<String>,
    pub location: Option<String>,
}

/// Read a `PropertyReassignment` event from the stream.
pub fn read_property_reassignment(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<PropertyReassignmentEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let property_name = read_dedup_string(reader, strings)?;
    let previous_value = read_dedup_string(reader, strings)?;
    let new_value = read_dedup_string(reader, strings)?;
    let location = read_dedup_string(reader, strings)?;
    Ok(PropertyReassignmentEvent {
        fields,
        property_name,
        previous_value,
        new_value,
        location,
    })
}

/// `UninitializedPropertyRead` event — a property was read before being set.
#[derive(Debug, Clone)]
pub struct UninitializedPropertyReadEvent {
    pub fields: BuildEventArgsFields,
    pub property_name: Option<String>,
}

/// Read a `UninitializedPropertyRead` event from the stream.
pub fn read_uninitialized_property_read(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<UninitializedPropertyReadEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let property_name = read_dedup_string(reader, strings)?;
    Ok(UninitializedPropertyReadEvent {
        fields,
        property_name,
    })
}

/// `PropertyInitialValueSet` event — a property's initial value was set.
#[derive(Debug, Clone)]
pub struct PropertyInitialValueSetEvent {
    pub fields: BuildEventArgsFields,
    pub property_name: Option<String>,
    pub property_value: Option<String>,
    pub property_source: Option<String>,
}

/// Read a `PropertyInitialValueSet` event from the stream.
pub fn read_property_initial_value_set(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<PropertyInitialValueSetEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let property_name = read_dedup_string(reader, strings)?;
    let property_value = read_dedup_string(reader, strings)?;
    let property_source = read_dedup_string(reader, strings)?;
    Ok(PropertyInitialValueSetEvent {
        fields,
        property_name,
        property_value,
        property_source,
    })
}

// ---------------------------------------------------------------------------
// MN-17: Environment/Response event types
// ---------------------------------------------------------------------------

/// `EnvironmentVariableRead` event — an environment variable was read during evaluation.
#[derive(Debug, Clone)]
pub struct EnvironmentVariableReadEvent {
    pub fields: BuildEventArgsFields,
    pub environment_variable_name: Option<String>,
    /// Line number in the project file (format version ≥ 22 only).
    pub line: i32,
    /// Column number in the project file (format version ≥ 22 only).
    pub column: i32,
    /// File name where the read occurred (format version ≥ 22 only).
    pub file_name: Option<String>,
}

/// Read an `EnvironmentVariableRead` event from the stream.
pub fn read_environment_variable_read(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<EnvironmentVariableReadEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let environment_variable_name = read_dedup_string(reader, strings)?;
    let (line, column, file_name) = if file_format_version >= 22 {
        let line = read_7bit_int(reader)?;
        let column = read_7bit_int(reader)?;
        let file_name = read_dedup_string(reader, strings)?;
        (line, column, file_name)
    } else {
        (0, 0, None)
    };
    Ok(EnvironmentVariableReadEvent {
        fields,
        environment_variable_name,
        line,
        column,
        file_name,
    })
}

/// `ResponseFileUsed` event — a response file was consumed by MSBuild.
#[derive(Debug, Clone)]
pub struct ResponseFileUsedEvent {
    pub fields: BuildEventArgsFields,
    pub response_file_path: Option<String>,
}

/// Read a `ResponseFileUsed` event from the stream.
pub fn read_response_file_used(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<ResponseFileUsedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let response_file_path = read_dedup_string(reader, strings)?;
    Ok(ResponseFileUsedEvent {
        fields,
        response_file_path,
    })
}

// ---------------------------------------------------------------------------
// MN-18: Assembly/Import event types
// ---------------------------------------------------------------------------

/// `AssemblyLoad` event — an assembly was loaded during the build.
#[derive(Debug, Clone)]
pub struct AssemblyLoadEvent {
    pub fields: BuildEventArgsFields,
    /// `AssemblyLoadingContext` (raw i32).
    pub context: i32,
    pub loading_initiator: Option<String>,
    pub assembly_name: Option<String>,
    pub assembly_path: Option<String>,
    /// Module version identifier (GUID, 16 bytes).
    pub mvid: [u8; 16],
    pub app_domain_name: Option<String>,
}

/// Read an `AssemblyLoad` event from the stream.
pub fn read_assembly_load(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<AssemblyLoadEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let context = read_7bit_int(reader)?;
    let loading_initiator = read_dedup_string(reader, strings)?;
    let assembly_name = read_dedup_string(reader, strings)?;
    let assembly_path = read_dedup_string(reader, strings)?;
    let mvid = crate::primitives::read_guid(reader)?;
    let app_domain_name = read_dedup_string(reader, strings)?;
    Ok(AssemblyLoadEvent {
        fields,
        context,
        loading_initiator,
        assembly_name,
        assembly_path,
        mvid,
        app_domain_name,
    })
}

/// `ProjectImported` event — a project file was imported.
#[derive(Debug, Clone)]
pub struct ProjectImportedEvent {
    pub fields: BuildEventArgsFields,
    /// Whether the import was ignored (format version > 2 only).
    pub import_ignored: bool,
    pub imported_project_file: Option<String>,
    pub unexpanded_project: Option<String>,
}

/// Read a `ProjectImported` event from the stream.
pub fn read_project_imported(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<ProjectImportedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, true)?;
    let import_ignored = if file_format_version > 2 {
        read_bool(reader)?
    } else {
        false
    };
    let imported_project_file = read_optional_string(reader, strings, file_format_version)?;
    let unexpanded_project = read_optional_string(reader, strings, file_format_version)?;
    Ok(ProjectImportedEvent {
        fields,
        import_ignored,
        imported_project_file,
        unexpanded_project,
    })
}

// ---------------------------------------------------------------------------
// MN-19: BuildCheck event types
// ---------------------------------------------------------------------------

// BuildCheckMessage, BuildCheckWarning, and BuildCheckError share the same
// binary layout as BuildMessage, BuildWarning, and BuildError respectively.
// Use `read_build_message`, `read_build_warning`, and `read_build_error` to
// read them. The record kind discriminant distinguishes them at the dispatch
// layer.

/// Type alias: a BuildCheck message has the same structure as a `BuildMessageEvent`.
pub type BuildCheckMessageEvent = BuildMessageEvent;

/// Type alias: a BuildCheck warning has the same structure as a `BuildWarningEvent`.
pub type BuildCheckWarningEvent = BuildWarningEvent;

/// Type alias: a BuildCheck error has the same structure as a `BuildErrorEvent`.
pub type BuildCheckErrorEvent = BuildErrorEvent;

/// `BuildCheckTracing` event — timing data for build check rules.
#[derive(Debug, Clone)]
pub struct BuildCheckTracingEvent {
    pub fields: BuildEventArgsFields,
    /// Raw tracing data: rule name → duration ticks as string.
    pub tracing_data: Option<Vec<(String, String)>>,
}

/// Read a `BuildCheckTracing` event from the stream.
pub fn read_build_check_tracing(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<BuildCheckTracingEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let tracing_data = read_string_dictionary(reader, strings, nvl_table, file_format_version)?;
    Ok(BuildCheckTracingEvent {
        fields,
        tracing_data,
    })
}

/// `BuildCheckAcquisition` event — a build check rule was acquired.
#[derive(Debug, Clone)]
pub struct BuildCheckAcquisitionEvent {
    pub fields: BuildEventArgsFields,
    pub acquisition_path: String,
    pub project_path: String,
}

/// Read a `BuildCheckAcquisition` event from the stream.
pub fn read_build_check_acquisition(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildCheckAcquisitionEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let acquisition_path = crate::primitives::read_dotnet_string(reader)?;
    let project_path = crate::primitives::read_dotnet_string(reader)?;
    Ok(BuildCheckAcquisitionEvent {
        fields,
        acquisition_path,
        project_path,
    })
}

// ---------------------------------------------------------------------------
// MN-20: Remaining event types
// ---------------------------------------------------------------------------

/// `BuildSubmissionStarted` event — a build submission was queued.
#[derive(Debug, Clone)]
pub struct BuildSubmissionStartedEvent {
    pub fields: BuildEventArgsFields,
    pub global_properties: Option<Vec<(String, String)>>,
    pub entry_projects_full_path: Option<Vec<String>>,
    pub target_names: Option<Vec<String>>,
    /// `BuildRequestDataFlags` (raw i32).
    pub flags: i32,
    pub submission_id: i32,
}

/// Read a `BuildSubmissionStarted` event from the stream.
pub fn read_build_submission_started(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<BuildSubmissionStartedEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    let global_properties =
        read_string_dictionary(reader, strings, nvl_table, file_format_version)?;
    let entry_projects_full_path = read_string_list(reader, strings)?;
    let target_names = read_string_list(reader, strings)?;
    let flags = read_7bit_int(reader)?;
    let submission_id = read_7bit_int(reader)?;
    Ok(BuildSubmissionStartedEvent {
        fields,
        global_properties,
        entry_projects_full_path,
        target_names,
        flags,
        submission_id,
    })
}

/// `BuildCanceled` event — the build was canceled.
#[derive(Debug, Clone)]
pub struct BuildCanceledEvent {
    pub fields: BuildEventArgsFields,
}

/// Read a `BuildCanceled` event from the stream.
pub fn read_build_canceled(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<BuildCanceledEvent, MuninError> {
    let fields = read_build_event_args_fields(reader, strings, file_format_version, false)?;
    Ok(BuildCanceledEvent { fields })
}

// ---------------------------------------------------------------------------
// Shared data structures
// ---------------------------------------------------------------------------

/// A task item (item spec + metadata).
#[derive(Debug, Clone)]
pub struct TaskItem {
    pub item_spec: Option<String>,
    pub metadata: Option<Vec<(String, String)>>,
}

/// A group of items with the same type name.
#[derive(Debug, Clone)]
pub struct ItemGroup {
    pub item_type: String,
    pub items: Vec<TaskItem>,
}

// ---------------------------------------------------------------------------
// Shared item/property readers
// ---------------------------------------------------------------------------

/// Read a count-prefixed list of deduplicated strings.
///
/// Returns `None` if the count is zero.
fn read_string_list(
    reader: &mut impl Read,
    strings: &StringTable,
) -> Result<Option<Vec<String>>, MuninError> {
    let count = read_7bit_count(reader, "string-list count")?;
    if count == 0 {
        return Ok(None);
    }
    let mut list = Vec::with_capacity(count);
    for _ in 0..count {
        if let Some(s) = read_dedup_string(reader, strings)? {
            list.push(s);
        }
    }
    if list.is_empty() {
        Ok(None)
    } else {
        Ok(Some(list))
    }
}

/// Read a property list (string dictionary representing properties).
fn read_property_list(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<Option<Vec<(String, String)>>, MuninError> {
    read_string_dictionary(reader, strings, nvl_table, file_format_version)
}

/// Read a single task item (item spec + metadata dictionary).
fn read_task_item(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<TaskItem, MuninError> {
    let item_spec = read_dedup_string(reader, strings)?;
    let metadata = read_string_dictionary(reader, strings, nvl_table, file_format_version)?;
    Ok(TaskItem {
        item_spec,
        metadata,
    })
}

/// Read a list of task items (count-prefixed).
fn read_task_item_list(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<Option<Vec<TaskItem>>, MuninError> {
    let count = read_7bit_count(reader, "task-item count")?;
    if count == 0 {
        return Ok(None);
    }
    let mut items = Vec::with_capacity(count);
    for _ in 0..count {
        items.push(read_task_item(
            reader,
            strings,
            nvl_table,
            file_format_version,
        )?);
    }
    Ok(Some(items))
}

/// Read project items (grouped by item type).
///
/// The format evolved across versions:
/// - Pre-v10: flat list of (item-name-string, task-item) pairs.
/// - v10–11: count-prefixed groups of (item-type-dedup, task-item-list).
/// - v12+: sequential groups terminated by an empty item-type string.
fn read_project_items(
    reader: &mut impl Read,
    strings: &StringTable,
    nvl_table: &NameValueListTable,
    file_format_version: i32,
) -> Result<Option<Vec<ItemGroup>>, MuninError> {
    if file_format_version < 10 {
        let count = read_7bit_count(reader, "item count")?;
        if count == 0 {
            return Ok(None);
        }
        let mut groups: Vec<ItemGroup> = Vec::new();
        let mut group_index: std::collections::HashMap<String, usize> =
            std::collections::HashMap::new();
        for _ in 0..count {
            let item_name = read_dotnet_string_via_reader(reader)?;
            let item = read_task_item(reader, strings, nvl_table, file_format_version)?;
            // Group by item_name using a map to avoid O(n²) linear scans.
            if let Some(&idx) = group_index.get(&item_name) {
                groups[idx].items.push(item);
            } else {
                let idx = groups.len();
                group_index.insert(item_name.clone(), idx);
                groups.push(ItemGroup {
                    item_type: item_name,
                    items: vec![item],
                });
            }
        }
        Ok(Some(groups))
    } else if file_format_version < 12 {
        let count = read_7bit_count(reader, "item-group count")?;
        if count == 0 {
            return Ok(None);
        }
        let mut groups = Vec::with_capacity(count);
        for _ in 0..count {
            let item_type = read_dedup_string(reader, strings)?.unwrap_or_default();
            let items = read_task_item_list(reader, strings, nvl_table, file_format_version)?
                .unwrap_or_default();
            groups.push(ItemGroup { item_type, items });
        }
        if groups.is_empty() {
            Ok(None)
        } else {
            Ok(Some(groups))
        }
    } else {
        // v12+: groups terminated by empty item type.
        let mut groups = Vec::new();
        loop {
            let item_type = read_dedup_string(reader, strings)?.unwrap_or_default();
            if item_type.is_empty() {
                break;
            }
            let items = read_task_item_list(reader, strings, nvl_table, file_format_version)?
                .unwrap_or_default();
            groups.push(ItemGroup { item_type, items });
        }
        if groups.is_empty() {
            Ok(None)
        } else {
            Ok(Some(groups))
        }
    }
}

/// Read a raw .NET string (used for pre-v10 item names).
fn read_dotnet_string_via_reader(reader: &mut impl Read) -> Result<String, MuninError> {
    crate::primitives::read_dotnet_string(reader)
}

/// Read an `EvaluationLocation` for profiler data.
fn read_evaluation_location(
    reader: &mut impl Read,
    strings: &StringTable,
    file_format_version: i32,
) -> Result<EvaluationLocation, MuninError> {
    let element_name = read_optional_string(reader, strings, file_format_version)?;
    let description = read_optional_string(reader, strings, file_format_version)?;
    let evaluation_description = read_optional_string(reader, strings, file_format_version)?;
    let file = read_optional_string(reader, strings, file_format_version)?;
    let kind = read_7bit_int(reader)?;
    let evaluation_pass = read_7bit_int(reader)?;

    let line = if read_bool(reader)? {
        Some(read_7bit_int(reader)?)
    } else {
        None
    };

    // id and parent_id introduced in format version 6.
    let (id, parent_id) = if file_format_version > 5 {
        let id = crate::primitives::read_i64_le(reader)?;
        let parent_id = if read_bool(reader)? {
            Some(crate::primitives::read_i64_le(reader)?)
        } else {
            None
        };
        (id, parent_id)
    } else {
        (0, None)
    };

    Ok(EvaluationLocation {
        element_name,
        description,
        evaluation_description,
        file,
        kind,
        evaluation_pass,
        line,
        id,
        parent_id,
    })
}

/// Read a `ProfiledLocation` (hit count + exclusive/inclusive times).
fn read_profiled_location(reader: &mut impl Read) -> Result<ProfiledLocation, MuninError> {
    let number_of_hits = read_7bit_int(reader)?;
    let exclusive_time_ticks = crate::primitives::read_i64_le(reader)?;
    let inclusive_time_ticks = crate::primitives::read_i64_le(reader)?;
    Ok(ProfiledLocation {
        number_of_hits,
        exclusive_time_ticks,
        inclusive_time_ticks,
    })
}

#[cfg(test)]
mod tests;