ty_plugin_protocol 0.0.2

Stable serialized protocol types for ty semantic extensions.
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
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
//! Stable data protocol for `ty` semantic extensions.
//!
//! This crate intentionally contains only serialized protocol types. It must
//! not depend on `ty_python_semantic`, Salsa, AST ids, file ids, or any other
//! checker internals.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

pub const CURRENT_PROTOCOL_VERSION: ProtocolVersion = ProtocolVersion { major: 0, minor: 3 };

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProtocolVersion {
    pub major: u16,
    pub minor: u16,
}

impl ProtocolVersion {
    /// The protocol version this build of the host implements.
    pub const CURRENT: ProtocolVersion = CURRENT_PROTOCOL_VERSION;

    /// Negotiate whether a host advertising `self` can serve a plugin built for `plugin`.
    ///
    /// The rule mirrors pre-1.0 semver expectations: the `major` numbers must match, and the
    /// host must be at least as new as the plugin's `minor` (a plugin built against protocol
    /// features the host does not yet implement is rejected). Because the protocol structs do
    /// not set `deny_unknown_fields`, an older host can still *parse* a newer plugin's messages;
    /// negotiation is what prevents it from silently dropping features it cannot honor.
    #[must_use]
    pub fn negotiate(self, plugin: ProtocolVersion) -> ProtocolCompatibility {
        if self.major != plugin.major {
            ProtocolCompatibility::MajorMismatch
        } else if plugin.minor > self.minor {
            ProtocolCompatibility::MinorTooNew
        } else {
            ProtocolCompatibility::Compatible
        }
    }

    /// Convenience wrapper around [`ProtocolVersion::negotiate`] for callers that only need a
    /// boolean answer.
    #[must_use]
    pub fn supports(self, plugin: ProtocolVersion) -> bool {
        self.negotiate(plugin) == ProtocolCompatibility::Compatible
    }
}

/// The outcome of negotiating a plugin's declared [`ProtocolVersion`] against the host's.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolCompatibility {
    /// The host can serve the plugin.
    Compatible,
    /// The `major` versions differ; the wire shape is not guaranteed to match.
    MajorMismatch,
    /// The plugin requires a newer `minor` than the host implements.
    MinorTooNew,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct VersionReq {
    pub requirement: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct PluginManifest {
    pub id: String,
    pub name: String,
    pub version: String,
    pub protocol_version: ProtocolVersion,
    pub ty_compatibility: VersionReq,
    pub runtime: RuntimeSpec,
    #[serde(default)]
    pub capabilities: PluginCapabilities,
    #[serde(default)]
    pub claims: PluginClaims,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config_schema: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_config: Option<serde_json::Value>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stub_overlays: Vec<StubOverlay>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum RuntimeSpec {
    Mock,
    Wasm(WasmRuntimeSpec),
    Subprocess(SubprocessRuntimeSpec),
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct WasmRuntimeSpec {
    pub artifact: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SubprocessRuntimeSpec {
    pub command: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub args: Vec<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
#[expect(
    clippy::struct_excessive_bools,
    reason = "Plugin capabilities are serialized manifest flags."
)]
pub struct PluginCapabilities {
    pub stub_overlays: bool,
    pub class_transform: bool,
    pub class_member: bool,
    pub instance_member: bool,
    pub call_signature: bool,
    pub call_return: bool,
    pub additional_dependencies: bool,
    pub project_index: bool,
    pub cross_symbol_contributions: bool,
    pub settings_data: bool,
    pub virtual_types: bool,
    pub mutation_validation: bool,
}

#[derive(Debug, Default, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "kebab-case")]
pub struct PluginClaims {
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub modules: Vec<ModuleClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub classes: Vec<ClassClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub decorators: Vec<SymbolClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub functions: Vec<SymbolClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub methods: Vec<MethodClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub attributes: Vec<AttributeClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub settings: Vec<SettingsClaim>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub mutations: Vec<ClassClaim>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ModuleClaim {
    pub name: String,
    #[serde(default)]
    pub recursive: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ClassClaim {
    #[serde(flatten)]
    pub kind: ClassClaimKind,
}

impl ClassClaim {
    #[must_use]
    pub fn exact(qualified_name: impl Into<String>) -> Self {
        Self {
            kind: ClassClaimKind::Exact {
                qualified_name: qualified_name.into(),
            },
        }
    }

    #[must_use]
    pub fn subclass_of(base_qualified_name: impl Into<String>) -> Self {
        Self {
            kind: ClassClaimKind::SubclassOf {
                base_qualified_name: base_qualified_name.into(),
            },
        }
    }

    #[must_use]
    pub fn exact_qualified_name(&self) -> Option<&str> {
        match &self.kind {
            ClassClaimKind::Exact { qualified_name } => Some(qualified_name),
            ClassClaimKind::SubclassOf { .. } => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum ClassClaimKind {
    Exact { qualified_name: String },
    SubclassOf { base_qualified_name: String },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SymbolClaim {
    pub qualified_name: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MethodClaim {
    #[serde(flatten)]
    pub kind: MethodClaimKind,
}

impl MethodClaim {
    #[must_use]
    pub fn exact(class_qualified_name: impl Into<String>, method_name: impl Into<String>) -> Self {
        Self {
            kind: MethodClaimKind::Exact {
                class_qualified_name: class_qualified_name.into(),
                method_name: method_name.into(),
            },
        }
    }

    #[must_use]
    pub fn on_subclass_of(
        base_qualified_name: impl Into<String>,
        method_name: impl Into<String>,
    ) -> Self {
        Self {
            kind: MethodClaimKind::OnSubclassOf {
                base_qualified_name: base_qualified_name.into(),
                method_name: method_name.into(),
            },
        }
    }

    #[must_use]
    pub fn exact_method(&self) -> Option<(&str, &str)> {
        match &self.kind {
            MethodClaimKind::Exact {
                class_qualified_name,
                method_name,
            } => Some((class_qualified_name, method_name)),
            MethodClaimKind::OnSubclassOf { .. } => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum MethodClaimKind {
    Exact {
        class_qualified_name: String,
        method_name: String,
    },
    OnSubclassOf {
        base_qualified_name: String,
        method_name: String,
    },
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AttributeClaim {
    #[serde(flatten)]
    pub kind: AttributeClaimKind,
}

impl AttributeClaim {
    #[must_use]
    pub fn exact(
        owner_qualified_name: impl Into<String>,
        attribute_name: impl Into<String>,
        scope: AttributeScope,
    ) -> Self {
        Self {
            kind: AttributeClaimKind::Exact {
                owner_qualified_name: owner_qualified_name.into(),
                attribute_name: attribute_name.into(),
                scope,
            },
        }
    }

    #[must_use]
    pub fn contribution_target(
        owner_base_qualified_name: impl Into<String>,
        scope: AttributeScope,
    ) -> Self {
        Self {
            kind: AttributeClaimKind::ContributionTarget {
                owner_base_qualified_name: owner_base_qualified_name.into(),
                scope,
            },
        }
    }

    #[must_use]
    pub fn on_subclass_of(
        owner_base_qualified_name: impl Into<String>,
        scope: AttributeScope,
    ) -> Self {
        Self {
            kind: AttributeClaimKind::OnSubclassOf {
                owner_base_qualified_name: owner_base_qualified_name.into(),
                scope,
            },
        }
    }

    #[must_use]
    pub fn exact_attribute(&self) -> Option<(&str, &str, AttributeScope)> {
        match &self.kind {
            AttributeClaimKind::Exact {
                owner_qualified_name,
                attribute_name,
                scope,
            } => Some((owner_qualified_name, attribute_name, *scope)),
            AttributeClaimKind::ContributionTarget { .. }
            | AttributeClaimKind::OnSubclassOf { .. } => None,
        }
    }

    #[must_use]
    pub fn scope(&self) -> AttributeScope {
        match &self.kind {
            AttributeClaimKind::Exact { scope, .. }
            | AttributeClaimKind::ContributionTarget { scope, .. }
            | AttributeClaimKind::OnSubclassOf { scope, .. } => *scope,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum AttributeClaimKind {
    Exact {
        owner_qualified_name: String,
        attribute_name: String,
        scope: AttributeScope,
    },
    ContributionTarget {
        owner_base_qualified_name: String,
        scope: AttributeScope,
    },
    OnSubclassOf {
        owner_base_qualified_name: String,
        scope: AttributeScope,
    },
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum AttributeScope {
    Class,
    Instance,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SettingsClaim {
    #[serde(default, skip_serializing_if = "String::is_empty")]
    pub module: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub config_key: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct StubOverlay {
    pub module: String,
    pub path: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
pub enum PluginRequest {
    Manifest,
    BuildProjectIndex(BuildProjectIndexRequest),
    AnalyzeClass(AnalyzeClassRequest),
    ResolveClassMember(ResolveMemberRequest),
    ResolveInstanceMember(ResolveMemberRequest),
    AdjustCallSignature(CallRequest),
    AdjustCallReturn(CallRequest),
    AdditionalDependencies(DependencyRequest),
    ValidateMutation(MutationRequest),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProjectContext {
    pub root: String,
    pub python_version: String,
    pub platform: String,
    #[serde(default)]
    pub config: serde_json::Value,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct BuildProjectIndexRequest {
    pub context: ProjectContext,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub classes: Vec<ClassSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub settings: Vec<SettingsModuleSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub assignments: Vec<AssignmentSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub previous_index_fingerprint: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AnalyzeClassRequest {
    pub context: SemanticContext,
    pub class: ClassSummary,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_index: Option<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ResolveMemberRequest {
    pub context: SemanticContext,
    pub owner: TypeExpr,
    pub member_name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub existing_member: Option<MemberSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_index: Option<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CallRequest {
    pub context: SemanticContext,
    pub callee: TypeExpr,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub receiver: Option<ReceiverSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub arguments: Vec<ArgumentSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub existing_signature: Option<CallableSignature>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_return_type: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_index: Option<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DependencyRequest {
    pub config: serde_json::Value,
}

/// A syntax-level write or delete operation offered to a semantic plugin for validation.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MutationRequest {
    pub context: SemanticContext,
    pub operation: MutationOperation,
    /// The inferred receiver, including its lossless structural snapshot when available.
    pub receiver: TypeExpr,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub key: Option<ArgumentSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub value: Option<ArgumentSummary>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_index: Option<serde_json::Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MutationOperation {
    AttributeSet,
    ItemSet,
    AttributeDelete,
    ItemDelete,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SemanticContext {
    pub module: String,
    pub file_path: String,
    pub python_version: String,
    pub platform: String,
    #[serde(default)]
    pub speculative: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ClassSummary {
    pub qualified_name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bases: Vec<TypeExpr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub decorators: Vec<CallOrSymbolSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub metaclass: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<FieldSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub methods: Vec<MethodSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub nested_classes: Vec<NestedClassSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub class_constants: Vec<ConstantSummary>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SymbolRef {
    pub qualified_name: String,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum CallOrSymbolSummary {
    Symbol(SymbolRef),
    Call(CallValueSummary),
    Other {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        inferred_type: Option<TypeExpr>,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct FieldSummary {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub annotation: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub assigned_value: Option<AssignedValueSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inferred_type: Option<TypeExpr>,
    #[serde(default)]
    pub has_default: bool,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MethodSummary {
    pub name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub decorators: Vec<CallOrSymbolSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub return_type: Option<TypeExpr>,
    #[serde(default)]
    pub is_public: bool,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct AssignmentSummary {
    pub name: String,
    pub qualified_name: String,
    pub assigned_value: AssignedValueSummary,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub inferred_type: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct NestedClassSummary {
    pub name: String,
    pub qualified_name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub bases: Vec<TypeExpr>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub class_constants: Vec<ConstantSummary>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ConstantSummary {
    pub name: String,
    pub value: LiteralValue,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub type_expr: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum AssignedValueSummary {
    Call(CallValueSummary),
    Literal {
        value: LiteralValue,
    },
    Name(SymbolRef),
    Attribute(SymbolRef),
    Other {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        inferred_type: Option<TypeExpr>,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CallValueSummary {
    pub callee: SymbolRef,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub receiver: Option<ValueSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub arguments: Vec<ArgumentSummary>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub return_type: Option<TypeExpr>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ValueSummary {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub symbol: Option<SymbolRef>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub type_expr: Option<TypeExpr>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum LiteralValue {
    Bool { value: bool },
    Int { value: i64 },
    Str { value: String },
    None,
    EnumRef(SymbolRef),
    SymbolRef(SymbolRef),
    ClassRef(SymbolRef),
    Tuple { items: Vec<LiteralValue> },
    List { items: Vec<LiteralValue> },
    Dict { entries: Vec<LiteralDictEntry> },
    Unknown,
}

impl Default for LiteralValue {
    fn default() -> Self {
        Self::Unknown
    }
}

impl LiteralValue {
    #[must_use]
    pub const fn is_unknown(&self) -> bool {
        matches!(self, Self::Unknown)
    }
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct LiteralDictEntry {
    pub key: LiteralValue,
    pub value: LiteralValue,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MemberSummary {
    pub name: String,
    pub access: MemberAccessPatch,
    #[serde(default)]
    pub is_read_only: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ArgumentSummary {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub kind: ArgumentKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub type_expr: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "LiteralValue::is_unknown")]
    pub value: LiteralValue,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub source: Option<SymbolSource>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ArgumentKind {
    Positional,
    Keyword,
    StarArgs,
    StarKwargs,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ReceiverSummary {
    pub type_expr: TypeExpr,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub nominal_class: Option<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub generic_arguments: Vec<TypeExpr>,
    #[serde(default)]
    pub plugin_metadata: serde_json::Value,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SettingsModuleSummary {
    pub module: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub values: Vec<SettingValueSummary>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<PluginDependency>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SettingValueSummary {
    pub name: String,
    pub value: LiteralValue,
    #[serde(default, skip_serializing_if = "SymbolSource::is_unknown")]
    pub source: SymbolSource,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "kebab-case")]
#[expect(
    clippy::large_enum_variant,
    reason = "Protocol responses mirror the serialized wire shape."
)]
pub enum PluginResponse {
    Manifest(PluginManifest),
    ProjectIndex(ProjectIndexResponse),
    ClassPatch(ClassPatch),
    MemberPatch(MemberPatch),
    CallSignaturePatch(CallSignaturePatch),
    CallReturnPatch(CallReturnPatch),
    Dependencies(Vec<PluginDependency>),
    MutationDiagnostics(MutationResponse),
    NoChange,
    Error(PluginError),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MutationResponse {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ProjectIndexResponse {
    #[serde(default)]
    pub plugin_index: serde_json::Value,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub contributions: Vec<Contribution>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub virtual_types: Vec<VirtualTypeDefinition>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub dependencies: Vec<PluginDependency>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ClassPatch {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub fields: Vec<FieldPatch>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub class_members: Vec<MemberPatch>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub instance_members: Vec<MemberPatch>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub constructor: Option<CallableSignature>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct FieldPatch {
    pub name: String,
    #[serde(default)]
    pub mode: MemberPatchMode,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub descriptor: Option<MemberAccessPatch>,
    pub instance_get_type: TypeExpr,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub instance_set_type: Option<TypeExpr>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub constructor_parameter: Option<Parameter>,
    #[serde(default)]
    pub has_default: bool,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct MemberPatch {
    pub name: String,
    #[serde(default)]
    pub mode: MemberPatchMode,
    pub access: MemberAccessPatch,
    #[serde(default)]
    pub read_only: bool,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum MemberAccessPatch {
    Value {
        type_expr: TypeExpr,
    },
    Descriptor {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        class_type: Option<TypeExpr>,
        instance_get_type: TypeExpr,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        instance_set_type: Option<TypeExpr>,
    },
    Callable {
        signature: CallableSignature,
        /// Protocol-v1 fallback for hosts that do not understand callable member patches.
        fallback_type: TypeExpr,
    },
}

impl MemberAccessPatch {
    #[must_use]
    pub fn value(type_expr: TypeExpr) -> Self {
        Self::Value { type_expr }
    }

    #[must_use]
    pub fn instance_get_type(&self) -> &TypeExpr {
        match self {
            Self::Value { type_expr }
            | Self::Descriptor {
                instance_get_type: type_expr,
                ..
            }
            | Self::Callable {
                fallback_type: type_expr,
                ..
            } => type_expr,
        }
    }
}

#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum MemberPatchMode {
    #[default]
    FillOnMiss,
    ReplaceExisting,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Contribution {
    pub source: SymbolSource,
    pub target: ContributionTarget,
    pub patch: ContributionPatch,
    pub conflict_key: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum ContributionTarget {
    Class { qualified_name: String },
    Instance { qualified_name: String },
    Constructor { qualified_name: String },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum ContributionPatch {
    Member(MemberPatch),
    Field(FieldPatch),
    Constructor(CallableSignature),
    Diagnostic(PluginDiagnostic),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CallSignaturePatch {
    pub signature: CallableSignature,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CallReturnPatch {
    pub return_type: TypeExpr,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub diagnostics: Vec<PluginDiagnostic>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub result_metadata: Option<serde_json::Value>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct CallableSignature {
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub parameters: Vec<Parameter>,
    pub return_type: TypeExpr,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct Parameter {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub kind: ParameterKind,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub type_expr: Option<TypeExpr>,
    #[serde(default)]
    pub required: bool,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ParameterKind {
    PositionalOnly,
    PositionalOrKeyword,
    VarArgs,
    KeywordOnly,
    Kwargs,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TypeExpr {
    pub expression: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub imports: Vec<ImportBinding>,
    pub mode: TypeExprMode,
    /// A lossless structural representation of this type when the host can provide one.
    ///
    /// The display expression remains the protocol-v1 fallback. Keeping the snapshot optional
    /// lets older plugins ignore it and lets newer plugins return an unchanged host type without
    /// reparsing a lossy display string.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub snapshot: Option<Box<TypeSnapshot>>,
}

impl TypeExpr {
    /// A bare type expression (e.g. a runtime value's inferred type), imports left empty.
    pub fn expression(expression: impl Into<String>) -> Self {
        Self::with_mode(expression, TypeExprMode::Expression)
    }

    /// An annotation-position type expression (e.g. a parameter or field annotation).
    pub fn annotation(expression: impl Into<String>) -> Self {
        Self::with_mode(expression, TypeExprMode::Annotation)
    }

    /// A stub-position type expression (e.g. content destined for a generated `.pyi` overlay).
    pub fn stub(expression: impl Into<String>) -> Self {
        Self::with_mode(expression, TypeExprMode::Stub)
    }

    fn with_mode(expression: impl Into<String>, mode: TypeExprMode) -> Self {
        Self {
            expression: expression.into(),
            imports: Vec::new(),
            mode,
            snapshot: None,
        }
    }

    #[must_use]
    pub fn with_snapshot(mut self, snapshot: TypeSnapshot) -> Self {
        self.snapshot = Some(Box::new(snapshot));
        self
    }
}

/// Structural type data that survives a complete host -> plugin -> host round trip.
///
/// This intentionally models Python type shapes rather than checker internals. Unsupported
/// internal types use [`TypeSnapshot::Expression`], which preserves the existing TypeExpr
/// behavior while still retaining imports at the exact nested position where they are needed.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum TypeSnapshot {
    Expression(TypeSnapshotExpression),
    Nominal {
        qualified_name: String,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        arguments: Vec<TypeSnapshot>,
    },
    Tuple {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        prefix: Vec<TypeSnapshot>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        variadic: Option<Box<TypeSnapshot>>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        suffix: Vec<TypeSnapshot>,
    },
    TypedDict {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        fields: Vec<TypeSnapshotField>,
        #[serde(default, skip_serializing_if = "Option::is_none")]
        extra_items: Option<Box<TypeSnapshotField>>,
        #[serde(default)]
        closed: bool,
    },
    Union {
        elements: Vec<TypeSnapshot>,
    },
    PluginClass {
        identity: String,
    },
    SelfType {
        #[serde(default, skip_serializing_if = "Option::is_none")]
        bound: Option<Box<TypeSnapshot>>,
    },
    Annotated {
        base: Box<TypeSnapshot>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        metadata: Vec<TypeSnapshotMetadata>,
    },
}

impl TypeSnapshot {
    #[must_use]
    pub fn expression(type_expr: &TypeExpr) -> Self {
        Self::Expression(TypeSnapshotExpression {
            expression: type_expr.expression.clone(),
            imports: type_expr.imports.clone(),
            mode: type_expr.mode,
        })
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TypeSnapshotExpression {
    pub expression: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub imports: Vec<ImportBinding>,
    pub mode: TypeExprMode,
}

impl TypeSnapshotExpression {
    #[must_use]
    pub fn to_type_expr(&self) -> TypeExpr {
        TypeExpr {
            expression: self.expression.clone(),
            imports: self.imports.clone(),
            mode: self.mode,
            snapshot: None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TypeSnapshotField {
    pub name: String,
    pub type_snapshot: TypeSnapshot,
    #[serde(default = "default_true")]
    pub required: bool,
    #[serde(default)]
    pub read_only: bool,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TypeSnapshotMetadata {
    pub qualified_name: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub arguments: Vec<TypeSnapshot>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct ImportBinding {
    pub module: String,
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub alias: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum TypeExprMode {
    Expression,
    Annotation,
    Stub,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct VirtualTypeDefinition {
    pub name: String,
    pub shape: VirtualTypeShape,
    #[serde(default)]
    pub metadata: serde_json::Value,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(
    tag = "kind",
    rename_all = "kebab-case",
    rename_all_fields = "kebab-case"
)]
pub enum VirtualTypeShape {
    Class {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        bases: Vec<TypeExpr>,
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        members: Vec<MemberPatch>,
    },
    TypedDict {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        fields: Vec<VirtualTypeField>,
        #[serde(default = "default_true")]
        total: bool,
    },
    NamedTuple {
        #[serde(default, skip_serializing_if = "Vec::is_empty")]
        fields: Vec<VirtualTypeField>,
    },
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct VirtualTypeField {
    pub name: String,
    pub type_expr: TypeExpr,
    #[serde(default = "default_true")]
    pub required: bool,
    #[serde(default)]
    pub read_only: bool,
}

const fn default_true() -> bool {
    true
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct PluginDependency {
    pub path: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sha256: Option<String>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct PluginDiagnostic {
    pub id: String,
    pub message: String,
    pub severity: DiagnosticSeverity,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub location: Option<DiagnosticLocation>,
    #[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
    pub metadata: BTreeMap<String, serde_json::Value>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum DiagnosticSeverity {
    Error,
    Warning,
    Info,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct DiagnosticLocation {
    pub file_path: String,
    pub start: TextPosition,
    pub end: TextPosition,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SymbolSource {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub module: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub qualified_name: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub file_path: Option<String>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub start: Option<TextPosition>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub end: Option<TextPosition>,
}

impl Default for SymbolSource {
    fn default() -> Self {
        Self {
            module: None,
            qualified_name: None,
            file_path: None,
            start: None,
            end: None,
        }
    }
}

impl SymbolSource {
    #[must_use]
    pub fn is_unknown(&self) -> bool {
        self == &Self::default()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct TextPosition {
    pub line: u32,
    pub column: u32,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct PluginError {
    pub message: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub diagnostic: Option<PluginDiagnostic>,
}