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
/// BRP (Bevy Remote Protocol) message types and serialization
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fmt;
/// Unique identifier for an entity in the Bevy ECS world
pub type EntityId = u64;
/// Unique identifier for a component type
pub type ComponentTypeId = String;
/// Raw JSON value for flexible component data
pub type ComponentValue = serde_json::Value;
/// BRP request message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "method", content = "params")]
#[non_exhaustive]
pub enum BrpRequest {
/// Query entities matching certain criteria
#[serde(rename = "bevy/query")]
Query {
/// Optional filter to limit results
filter: Option<QueryFilter>,
/// Maximum number of results to return
limit: Option<usize>,
},
/// Get specific entity's components
#[serde(rename = "bevy/get")]
Get {
/// Entity ID to retrieve
entity: EntityId,
/// Optional list of component types to include
components: Option<Vec<ComponentTypeId>>,
},
/// Set component values on an entity
#[serde(rename = "bevy/set")]
Set {
/// Target entity ID
entity: EntityId,
/// Component type and value pairs to set
components: HashMap<ComponentTypeId, ComponentValue>,
},
/// Spawn a new entity with components
#[serde(rename = "bevy/spawn")]
Spawn {
/// Initial components for the new entity
components: HashMap<ComponentTypeId, ComponentValue>,
},
/// Destroy an entity
#[serde(rename = "bevy/destroy")]
Destroy {
/// Entity ID to destroy
entity: EntityId,
},
/// List all available component types
#[serde(rename = "bevy/list_components")]
ListComponents,
/// List all entities (optionally filtered)
#[serde(rename = "bevy/list_entities")]
ListEntities {
/// Optional filter criteria
filter: Option<QueryFilter>,
},
/// Take a screenshot of the primary window
#[serde(rename = "bevy_debugger/screenshot")]
Screenshot {
/// Path where to save the screenshot (optional)
path: Option<String>,
/// Time in milliseconds to wait before capture (game warmup)
warmup_duration: Option<u64>,
/// Additional delay in milliseconds before capture
capture_delay: Option<u64>,
/// Whether to wait for at least one frame to render
wait_for_render: Option<bool>,
/// Optional description for logging/debugging
description: Option<String>,
},
/// Spawn a new entity (for experiment system)
SpawnEntity {
components: Vec<(ComponentTypeId, ComponentValue)>,
},
/// Modify an existing entity (for experiment system)
ModifyEntity {
entity_id: EntityId,
components: Vec<(ComponentTypeId, ComponentValue)>,
},
/// Delete an entity (for experiment system)
DeleteEntity { entity_id: EntityId },
/// Query a specific entity (for experiment system)
QueryEntity { entity_id: EntityId },
/// Debug command wrapper for extensible debugging operations
#[serde(rename = "bevy_debugger/debug")]
Debug {
/// Debug command to execute
command: DebugCommand,
/// Unique correlation ID for tracking responses
correlation_id: String,
/// Priority level (higher values = higher priority)
priority: Option<u8>,
},
}
/// Query filter for selecting entities
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub struct QueryFilter {
/// Entities must have all of these components
pub with: Option<Vec<ComponentTypeId>>,
/// Entities must not have any of these components
pub without: Option<Vec<ComponentTypeId>>,
/// Component value filters
pub where_clause: Option<Vec<ComponentFilter>>,
}
/// Filter for component values
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentFilter {
/// Component type to filter on
pub component: ComponentTypeId,
/// Field path within the component (e.g., "position.x")
pub field: Option<String>,
/// Filter operation
pub op: FilterOp,
/// Value to compare against
pub value: ComponentValue,
}
/// Filter operations for component values
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum FilterOp {
#[serde(rename = "eq")]
Equal,
#[serde(rename = "ne")]
NotEqual,
#[serde(rename = "gt")]
GreaterThan,
#[serde(rename = "gte")]
GreaterThanOrEqual,
#[serde(rename = "lt")]
LessThan,
#[serde(rename = "lte")]
LessThanOrEqual,
#[serde(rename = "contains")]
Contains,
#[serde(rename = "regex")]
Regex,
}
/// Debug command types for extensible debugging operations
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "params")]
#[non_exhaustive]
pub enum DebugCommand {
/// Inspect entity with detailed component information
InspectEntity {
entity_id: EntityId,
/// Include component metadata
include_metadata: Option<bool>,
/// Include parent/child relationships
include_relationships: Option<bool>,
},
/// Inspect multiple entities in a single batch operation
InspectBatch {
entity_ids: Vec<EntityId>,
/// Include component metadata for all entities
include_metadata: Option<bool>,
/// Include parent/child relationships for all entities
include_relationships: Option<bool>,
/// Maximum number of entities to inspect (default: 100)
limit: Option<usize>,
},
/// Profile system performance
ProfileSystem {
/// System name to profile
system_name: String,
/// Duration in milliseconds
duration_ms: Option<u64>,
/// Include memory allocations
track_allocations: Option<bool>,
},
/// Enable/disable visual debugging overlays
SetVisualDebug {
/// Type of overlay
overlay_type: DebugOverlayType,
/// Enable or disable
enabled: bool,
/// Optional configuration
config: Option<serde_json::Value>,
},
/// Execute a validated ECS query
ExecuteQuery {
/// Validated query structure
query: ValidatedQuery,
/// Pagination offset
offset: Option<usize>,
/// Results limit
limit: Option<usize>,
},
/// Validate a query without executing it
ValidateQuery {
/// Query parameters as JSON
params: serde_json::Value,
},
/// Estimate the cost of executing a query
EstimateCost {
/// Query parameters as JSON
params: serde_json::Value,
},
/// Get optimization suggestions for a query
GetQuerySuggestions {
/// Query parameters as JSON
params: serde_json::Value,
},
/// Build and execute a query using the query builder
BuildAndExecuteQuery {
/// Query parameters as JSON
params: serde_json::Value,
},
/// Memory profiling command
ProfileMemory {
/// Capture allocation backtraces
capture_backtraces: Option<bool>,
/// Track specific systems
target_systems: Option<Vec<String>>,
/// Duration in seconds for profiling session
duration_seconds: Option<u64>,
},
/// Stop memory profiling session
StopMemoryProfiling {
/// Session ID to stop (None for default session)
session_id: Option<String>,
},
/// Get current memory profile
GetMemoryProfile,
/// Detect memory leaks
DetectMemoryLeaks {
/// Target systems to check for leaks
target_systems: Option<Vec<String>>,
},
/// Analyze memory usage trends
AnalyzeMemoryTrends {
/// Target systems to analyze
target_systems: Option<Vec<String>>,
},
/// Take a memory snapshot
TakeMemorySnapshot,
/// Get memory profiler statistics
GetMemoryStatistics,
/// Session management
SessionControl {
/// Session operation
operation: SessionOperation,
/// Session ID
session_id: Option<String>,
},
/// Get debug system status
GetStatus,
/// Get entity hierarchy information
GetHierarchy {
/// Optional root entity to start from
root_entity: Option<EntityId>,
/// Maximum depth to traverse
max_depth: Option<usize>,
},
/// Get system information and metadata
GetSystemInfo {
/// Optional system name filter
system_name: Option<String>,
/// Include scheduling information
include_scheduling: Option<bool>,
},
/// Start automated issue detection monitoring
StartIssueDetection,
/// Stop automated issue detection monitoring
StopIssueDetection,
/// Get detected issues/alerts
GetDetectedIssues {
/// Maximum number of issues to return
limit: Option<usize>,
},
/// Acknowledge an issue alert
AcknowledgeIssue {
/// Alert ID to acknowledge
alert_id: String,
},
/// Report an alert as a false positive
ReportFalsePositive {
/// Alert ID to mark as false positive
alert_id: String,
},
/// Get issue detection statistics
GetIssueDetectionStats,
/// Update a detection rule configuration
UpdateDetectionRule {
/// Rule name to update
name: String,
/// Enable/disable the rule
enabled: Option<bool>,
/// Sensitivity level (0.0 to 1.0)
sensitivity: Option<f32>,
},
/// Clear issue alert history
ClearIssueHistory,
/// Start performance budget monitoring
StartBudgetMonitoring,
/// Stop performance budget monitoring
StopBudgetMonitoring,
/// Set performance budget configuration
SetPerformanceBudget {
/// Budget configuration as JSON
config: serde_json::Value,
},
/// Get current performance budget configuration
GetPerformanceBudget,
/// Check for current budget violations
CheckBudgetViolations,
/// Get budget violation history
GetBudgetViolationHistory {
/// Maximum number of violations to return
limit: Option<usize>,
},
/// Generate compliance report for specified duration
GenerateComplianceReport {
/// Duration in seconds (default: 3600 = 1 hour)
duration_seconds: Option<u64>,
},
/// Get budget recommendations based on historical data
GetBudgetRecommendations,
/// Clear budget violation history
ClearBudgetHistory,
/// Get budget monitoring statistics
GetBudgetStatistics,
/// Custom debug command for extensions
Custom {
/// Command name
name: String,
/// Command parameters
params: serde_json::Value,
},
}
/// Debug overlay types for visual debugging
#[derive(Debug, Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[non_exhaustive]
pub enum DebugOverlayType {
/// Entity highlight overlay
EntityHighlight,
/// Physics collider visualization
Colliders,
/// Physics collider visualization (alternative name for compatibility)
ColliderVisualization,
/// Transform gizmos
Transforms,
/// Transform gizmos (alternative name for compatibility)
TransformGizmos,
/// System execution flow
SystemFlow,
/// Performance metrics
PerformanceMetrics,
/// Debug markers
DebugMarkers,
/// Custom overlay
Custom(String),
}
/// Validated query structure for safe ECS queries
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ValidatedQuery {
/// Query ID for caching
pub id: String,
/// Validated filter
pub filter: QueryFilter,
/// Estimated cost
pub estimated_cost: QueryCost,
/// Optimization hints
pub hints: Vec<String>,
}
/// Query cost estimation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryCost {
/// Estimated entities to scan
pub estimated_entities: usize,
/// Estimated time in microseconds
pub estimated_time_us: u64,
/// Memory usage estimate in bytes
pub estimated_memory: usize,
}
/// Session operations for debug session management
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SessionOperation {
/// Create new session
Create,
/// Resume existing session
Resume,
/// Checkpoint current state
Checkpoint,
/// Restore from checkpoint
Restore { checkpoint_id: String },
/// End session
End,
}
/// Debug response types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
#[non_exhaustive]
pub enum DebugResponse {
/// Entity inspection result
EntityInspection {
entity: EntityData,
metadata: Option<EntityMetadata>,
relationships: Option<EntityRelationships>,
},
/// Batch entity inspection result
BatchEntityInspection {
entities: Vec<EntityInspectionResult>,
/// Total entities requested
requested_count: usize,
/// Successfully inspected entities
found_count: usize,
/// Entities that were not found (despawned)
missing_entities: Vec<EntityId>,
/// Total inspection time in microseconds
inspection_time_us: u64,
},
/// System profiling result
SystemProfile(SystemProfile),
/// Profiling started response
ProfilingStarted {
system_name: String,
duration_ms: Option<u64>,
},
/// Profiling history response
ProfileHistory {
system_name: String,
samples: Vec<ProfileSample>,
frame_count: usize,
},
/// Performance anomalies response
PerformanceAnomalies {
count: usize,
anomalies: Vec<serde_json::Value>,
},
/// Visual debug status
VisualDebugStatus {
overlay_type: DebugOverlayType,
enabled: bool,
config: Option<serde_json::Value>,
},
/// Query execution result
QueryResult {
entities: Vec<EntityData>,
total_count: usize,
execution_time_us: u64,
has_more: bool,
},
/// Memory profile result
MemoryProfile {
total_allocated: usize,
allocations_per_system: HashMap<String, usize>,
top_allocations: Vec<AllocationInfo>,
},
/// Session control result
SessionStatus {
session_id: String,
state: SessionState,
command_count: usize,
checkpoints: Vec<CheckpointInfo>,
},
/// Debug system status
Status {
version: String,
active_sessions: usize,
command_queue_size: usize,
performance_overhead_percent: f32,
},
/// Query validation result
QueryValidation {
valid: bool,
query: Option<ValidatedQuery>,
errors: Vec<String>,
suggestions: Vec<String>,
},
/// Query cost estimation result
QueryCost {
cost: QueryCost,
performance_budget_exceeded: bool,
suggestions: Vec<String>,
},
/// Query optimization suggestions
QuerySuggestions {
suggestions: Vec<String>,
query_complexity: u32,
},
/// Query execution result
QueryExecution {
success: bool,
result: Option<Box<BrpResponse>>,
execution_time_us: u64,
entities_processed: Option<usize>,
},
/// Generic success response
Success {
message: String,
data: Option<serde_json::Value>,
},
/// Custom debug response
Custom(serde_json::Value),
}
/// Entity metadata for inspection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityMetadata {
/// Component count
pub component_count: usize,
/// Total memory size in bytes
pub memory_size: usize,
/// Last modified timestamp
pub last_modified: Option<u64>,
/// Entity generation
pub generation: u32,
/// Component type information
pub component_types: Vec<DetailedComponentTypeInfo>,
/// Which components have been modified
pub modified_components: Vec<String>,
/// Entity archetype information
pub archetype_id: Option<u32>,
/// Entity location in world storage
pub location_info: Option<EntityLocationInfo>,
}
/// Detailed component type information with reflection data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DetailedComponentTypeInfo {
/// Component type identifier
pub type_id: String,
/// Human-readable type name
pub type_name: String,
/// Size in bytes
pub size_bytes: usize,
/// Whether component has reflection data
pub is_reflected: bool,
/// Type schema if available
pub schema: Option<serde_json::Value>,
/// Whether component was modified this frame
pub is_modified: bool,
}
/// Entity location information in Bevy's storage
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityLocationInfo {
/// Archetype ID
pub archetype_id: u32,
/// Index within archetype
pub index: u32,
/// Table ID
pub table_id: Option<u32>,
/// Row in table
pub table_row: Option<u32>,
}
/// Entity relationships
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityRelationships {
/// Parent entity if exists
pub parent: Option<EntityId>,
/// Child entities
pub children: Vec<EntityId>,
/// Related entities (custom relationships)
pub related: HashMap<String, Vec<EntityId>>,
}
/// System performance metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemMetrics {
/// Total execution time in microseconds
pub total_time_us: u64,
/// Minimum execution time in microseconds
pub min_time_us: u64,
/// Maximum execution time in microseconds
pub max_time_us: u64,
/// Average execution time in microseconds
pub avg_time_us: u64,
/// Median execution time in microseconds
pub median_time_us: u64,
/// 95th percentile time
pub p95_time_us: u64,
/// 99th percentile time
pub p99_time_us: u64,
/// Total memory allocations
pub total_allocations: usize,
/// Allocation rate per invocation
pub allocation_rate: f32,
/// Overhead percentage
pub overhead_percent: f32,
}
/// Profile sample point
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ProfileSample {
/// Timestamp in microseconds
pub timestamp: u64,
/// Execution time in microseconds
pub duration_us: u64,
/// Memory allocations during execution
pub allocations: Option<usize>,
}
/// System profile data
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemProfile {
/// System name
pub system_name: String,
/// Performance metrics
pub metrics: SystemMetrics,
/// Sample timeline
pub samples: Vec<ProfileSample>,
/// System dependencies
pub dependencies: Vec<String>,
}
/// Memory allocation information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AllocationInfo {
/// Size in bytes
pub size: usize,
/// Allocation site (function name)
pub location: String,
/// Backtrace if available
pub backtrace: Option<Vec<String>>,
/// Allocation count
pub count: usize,
}
/// Debug session state
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum SessionState {
/// Session is active
Active,
/// Session is paused
Paused,
/// Session is replaying commands
Replaying,
/// Session ended
Ended,
}
/// Checkpoint information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CheckpointInfo {
/// Checkpoint ID
pub id: String,
/// Creation timestamp
pub timestamp: u64,
/// Description
pub description: Option<String>,
/// Size in bytes
pub size: usize,
}
/// Individual entity inspection result for batch operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityInspectionResult {
/// Entity ID
pub entity_id: EntityId,
/// Whether the entity was found
pub found: bool,
/// Entity data if found
pub entity: Option<EntityData>,
/// Entity metadata if requested and found
pub metadata: Option<EntityMetadata>,
/// Entity relationships if requested and found
pub relationships: Option<EntityRelationships>,
/// Error message if inspection failed
pub error: Option<String>,
}
/// BRP response message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum BrpResponse {
/// Successful response
Success(Box<BrpResult>),
/// Error response
Error(BrpError),
}
/// Successful BRP operation result
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", content = "data")]
#[non_exhaustive]
pub enum BrpResult {
/// Entity data response
#[serde(rename = "entities")]
Entities(Vec<EntityData>),
/// Single entity response
#[serde(rename = "entity")]
Entity(EntityData),
/// Entity ID response (for spawn operations)
#[serde(rename = "entity_id")]
EntityId(EntityId),
/// Component types list
#[serde(rename = "component_types")]
ComponentTypes(Vec<ComponentTypeInfo>),
/// Simple success confirmation
#[serde(rename = "success")]
Success,
/// Entity spawned successfully
EntitySpawned(EntityId),
/// Entity modified successfully
EntityModified,
/// Entity deleted successfully
EntityDeleted,
/// Screenshot taken successfully
#[serde(rename = "screenshot")]
Screenshot {
/// Path where the screenshot was saved
path: String,
/// Success status
success: bool,
},
/// Debug command response
#[serde(rename = "debug")]
Debug(Box<DebugResponse>),
}
/// Entity data with components
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EntityData {
/// Entity identifier
pub id: EntityId,
/// Component data by type
pub components: HashMap<ComponentTypeId, ComponentValue>,
}
/// Information about a component type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ComponentTypeInfo {
/// Component type identifier
pub id: ComponentTypeId,
/// Human-readable name
pub name: String,
/// JSON schema for the component structure
pub schema: Option<serde_json::Value>,
}
/// BRP error response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BrpError {
/// Error code
pub code: BrpErrorCode,
/// Human-readable error message
pub message: String,
/// Optional additional error details
pub details: Option<serde_json::Value>,
}
/// BRP error codes
#[derive(Debug, Clone, Serialize, Deserialize)]
#[non_exhaustive]
pub enum BrpErrorCode {
/// Entity not found
#[serde(rename = "entity_not_found")]
EntityNotFound,
/// Component type not found
#[serde(rename = "component_not_found")]
ComponentNotFound,
/// Invalid component data
#[serde(rename = "invalid_component_data")]
InvalidComponentData,
/// Query syntax error
#[serde(rename = "invalid_query")]
InvalidQuery,
/// Permission denied
#[serde(rename = "permission_denied")]
PermissionDenied,
/// Internal server error
#[serde(rename = "internal_error")]
InternalError,
/// Request timeout
#[serde(rename = "timeout")]
Timeout,
/// Debug command not supported
#[serde(rename = "debug_not_supported")]
DebugNotSupported,
/// Debug session error
#[serde(rename = "debug_session_error")]
DebugSessionError,
/// Debug command validation failed
#[serde(rename = "debug_validation_error")]
DebugValidationError,
}
impl fmt::Display for BrpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}: {}", self.code, self.message)
}
}
impl fmt::Display for BrpErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::EntityNotFound => write!(f, "Entity not found"),
Self::ComponentNotFound => write!(f, "Component not found"),
Self::InvalidComponentData => write!(f, "Invalid component data"),
Self::InvalidQuery => write!(f, "Invalid query"),
Self::PermissionDenied => write!(f, "Permission denied"),
Self::InternalError => write!(f, "Internal error"),
Self::Timeout => write!(f, "Request timeout"),
Self::DebugNotSupported => write!(f, "Debug command not supported"),
Self::DebugSessionError => write!(f, "Debug session error"),
Self::DebugValidationError => write!(f, "Debug command validation error"),
}
}
}
/// Common Bevy component type wrappers
pub mod components {
use serde::{Deserialize, Serialize};
/// 3D Transform component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transform {
pub translation: Vec3,
pub rotation: Quat,
pub scale: Vec3,
}
/// 3D Vector
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Vec3 {
pub x: f32,
pub y: f32,
pub z: f32,
}
/// Quaternion rotation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Quat {
pub x: f32,
pub y: f32,
pub z: f32,
pub w: f32,
}
/// Velocity component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Velocity {
pub linear: Vec3,
pub angular: Vec3,
}
/// Name component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Name {
pub name: String,
}
/// Visibility component
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Visibility {
pub is_visible: bool,
}
}
/// Validation utilities for BRP messages
pub mod validation {
use super::{BrpRequest, EntityId};
/// Validate entity ID format
pub fn validate_entity_id(id: EntityId) -> Result<(), String> {
if id == 0 {
Err("Entity ID cannot be zero".to_string())
} else {
Ok(())
}
}
/// Validate component type ID format
pub fn validate_component_type_id(type_id: &str) -> Result<(), String> {
if type_id.is_empty() {
return Err("Component type ID cannot be empty".to_string());
}
if !type_id
.chars()
.all(|c| c.is_alphanumeric() || c == '_' || c == ':')
{
return Err("Component type ID contains invalid characters".to_string());
}
Ok(())
}
/// Validate BRP request
pub fn validate_request(request: &BrpRequest) -> Result<(), String> {
match request {
BrpRequest::Get { entity, .. } | BrpRequest::Destroy { entity } => {
validate_entity_id(*entity)
}
BrpRequest::Set { entity, components } => {
validate_entity_id(*entity)?;
for type_id in components.keys() {
validate_component_type_id(type_id)?;
}
Ok(())
}
BrpRequest::Spawn { components } => {
for type_id in components.keys() {
validate_component_type_id(type_id)?;
}
Ok(())
}
_ => Ok(()),
}
}
}
/// Conversion utilities between MCP JSON and BRP messages
pub mod conversion {
use super::{BrpError, BrpErrorCode, BrpRequest, BrpResponse};
use crate::error::{Error, Result};
/// Convert MCP JSON arguments to BRP request
pub fn mcp_to_brp_request(method: &str, args: &serde_json::Value) -> Result<BrpRequest> {
let request_json = serde_json::json!({
"method": method,
"params": args
});
serde_json::from_value(request_json).map_err(Error::Json)
}
/// Convert BRP response to MCP JSON
pub fn brp_to_mcp_response(response: &BrpResponse) -> Result<serde_json::Value> {
serde_json::to_value(response).map_err(Error::Json)
}
/// Helper to create BRP error response
#[must_use]
pub fn create_brp_error(code: BrpErrorCode, message: String) -> BrpResponse {
BrpResponse::Error(BrpError {
code,
message,
details: None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_brp_request_serialization() {
let request = BrpRequest::Query {
filter: Some(QueryFilter {
with: Some(vec!["Transform".to_string(), "Velocity".to_string()]),
without: None,
where_clause: None,
}),
limit: Some(10),
};
let json = serde_json::to_string(&request).unwrap();
let deserialized: BrpRequest = serde_json::from_str(&json).unwrap();
match deserialized {
BrpRequest::Query { filter, limit } => {
assert_eq!(limit, Some(10));
assert!(filter.is_some());
}
_ => panic!("Wrong variant"),
}
}
#[test]
fn test_entity_validation() {
use validation::*;
assert!(validate_entity_id(1).is_ok());
assert!(validate_entity_id(0).is_err());
assert!(validate_component_type_id("Transform").is_ok());
assert!(validate_component_type_id("core::Transform").is_ok());
assert!(validate_component_type_id("").is_err());
assert!(validate_component_type_id("invalid-name").is_err());
}
#[test]
fn test_component_types() {
use components::*;
let transform = Transform {
translation: Vec3 {
x: 1.0,
y: 2.0,
z: 3.0,
},
rotation: Quat {
x: 0.0,
y: 0.0,
z: 0.0,
w: 1.0,
},
scale: Vec3 {
x: 1.0,
y: 1.0,
z: 1.0,
},
};
let json = serde_json::to_string(&transform).unwrap();
let _deserialized: Transform = serde_json::from_str(&json).unwrap();
}
}