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
/*
* Bevy Debugger MCP Server - BRP Command Handler Interface
* Copyright (C) 2025 ladvien
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::brp_messages::{BrpRequest, BrpResponse};
use crate::error::Result;
/// Version information for command handlers
#[derive(Debug, Clone)]
pub struct CommandVersion {
pub major: u16,
pub minor: u16,
pub patch: u16,
}
impl CommandVersion {
pub fn new(major: u16, minor: u16, patch: u16) -> Self {
Self { major, minor, patch }
}
pub fn is_compatible_with(&self, other: &CommandVersion) -> bool {
// Major version must match, minor/patch can differ
self.major == other.major
}
}
impl Default for CommandVersion {
fn default() -> Self {
Self::new(1, 0, 0)
}
}
/// Metadata for command handlers
#[derive(Debug, Clone)]
pub struct CommandHandlerMetadata {
pub name: String,
pub version: CommandVersion,
pub description: String,
pub supported_commands: Vec<String>,
}
/// Trait for handling BRP commands in an extensible way.
///
/// Implementors of this trait can process specific types of BRP requests
/// and return appropriate responses. Handlers are registered with a
/// `CommandHandlerRegistry` and selected based on their ability to handle
/// a request and their priority.
///
/// # Example
/// ```ignore
/// struct MyHandler;
///
/// #[async_trait]
/// impl BrpCommandHandler for MyHandler {
/// fn metadata(&self) -> CommandHandlerMetadata { ... }
/// fn can_handle(&self, request: &BrpRequest) -> bool { ... }
/// async fn handle(&self, request: BrpRequest) -> Result<BrpResponse> { ... }
/// }
/// ```
#[async_trait]
pub trait BrpCommandHandler: Send + Sync {
/// Get metadata about this handler including name, version, and supported commands
fn metadata(&self) -> CommandHandlerMetadata;
/// Check if this handler can process the given request.
/// Returns true if the handler supports the request type.
fn can_handle(&self, request: &BrpRequest) -> bool;
/// Process a BRP request and return a response.
/// This is called after validation passes.
async fn handle(&self, request: BrpRequest) -> Result<BrpResponse>;
/// Validate a request before processing.
/// Override this to add custom validation logic.
/// Default implementation accepts all requests.
async fn validate(&self, request: &BrpRequest) -> Result<()> {
Ok(())
}
/// Get the handler's priority (higher = processed first).
/// Handlers with higher priority are checked first when finding
/// a handler for a request. Default priority is 0.
fn priority(&self) -> i32 {
0
}
}
/// Registry for command handlers with versioning support
pub struct CommandHandlerRegistry {
handlers: Arc<RwLock<Vec<Arc<dyn BrpCommandHandler>>>>,
version_map: Arc<RwLock<HashMap<String, CommandVersion>>>,
}
impl CommandHandlerRegistry {
pub fn new() -> Self {
Self {
handlers: Arc::new(RwLock::new(Vec::new())),
version_map: Arc::new(RwLock::new(HashMap::new())),
}
}
/// Register a new command handler
pub async fn register(&self, handler: Arc<dyn BrpCommandHandler>) {
let metadata = handler.metadata();
// Update version map
let mut version_map = self.version_map.write().await;
version_map.insert(metadata.name.clone(), metadata.version.clone());
// Add handler sorted by priority
let mut handlers = self.handlers.write().await;
handlers.push(handler);
handlers.sort_by_key(|h| -h.priority()); // Sort descending by priority
}
/// Find a handler for the given request
pub async fn find_handler(&self, request: &BrpRequest) -> Option<Arc<dyn BrpCommandHandler>> {
let handlers = self.handlers.read().await;
for handler in handlers.iter() {
if handler.can_handle(request) {
return Some(handler.clone());
}
}
None
}
/// Process a request using the appropriate handler
pub async fn process(&self, request: BrpRequest) -> Result<BrpResponse> {
if let Some(handler) = self.find_handler(&request).await {
// Validate first
handler.validate(&request).await?;
// Then handle
handler.handle(request).await
} else {
Err(crate::error::Error::Validation(format!(
"No handler found for request type: {:?}",
request
)))
}
}
/// Get version information for all registered handlers
pub async fn get_versions(&self) -> HashMap<String, CommandVersion> {
self.version_map.read().await.clone()
}
/// Check if a specific version is supported
pub async fn is_version_supported(&self, handler_name: &str, version: &CommandVersion) -> bool {
let version_map = self.version_map.read().await;
if let Some(registered_version) = version_map.get(handler_name) {
registered_version.is_compatible_with(version)
} else {
false
}
}
}
/// Default handler for core BRP commands
pub struct CoreBrpHandler;
#[async_trait]
impl BrpCommandHandler for CoreBrpHandler {
fn metadata(&self) -> CommandHandlerMetadata {
CommandHandlerMetadata {
name: "core".to_string(),
version: CommandVersion::new(1, 0, 0),
description: "Handler for core BRP commands".to_string(),
supported_commands: vec![
"Query".to_string(),
"Get".to_string(),
"Set".to_string(),
"ListEntities".to_string(),
"ListComponents".to_string(),
"SpawnEntity".to_string(),
"DestroyEntity".to_string(),
],
}
}
fn can_handle(&self, request: &BrpRequest) -> bool {
matches!(
request,
BrpRequest::Query { .. }
| BrpRequest::Get { .. }
| BrpRequest::Set { .. }
| BrpRequest::ListEntities { .. }
| BrpRequest::ListComponents { .. }
| BrpRequest::Spawn { .. }
| BrpRequest::Destroy { .. }
)
}
async fn handle(&self, request: BrpRequest) -> Result<BrpResponse> {
// This will be handled by the actual BRP WebSocket connection
// For now, return a placeholder
Ok(BrpResponse::Success(Box::new(crate::brp_messages::BrpResult::Success)))
}
fn priority(&self) -> i32 {
-100 // Low priority, let specialized handlers go first
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_version_compatibility() {
let v1 = CommandVersion::new(1, 0, 0);
let v2 = CommandVersion::new(1, 1, 0);
let v3 = CommandVersion::new(2, 0, 0);
assert!(v1.is_compatible_with(&v2));
assert!(!v1.is_compatible_with(&v3));
}
#[tokio::test]
async fn test_handler_registry() {
let registry = CommandHandlerRegistry::new();
let handler = Arc::new(CoreBrpHandler);
registry.register(handler.clone()).await;
let request = BrpRequest::ListEntities { filter: None };
let found = registry.find_handler(&request).await;
assert!(found.is_some());
}
#[tokio::test]
async fn test_handler_priority() {
struct HighPriorityHandler;
#[async_trait]
impl BrpCommandHandler for HighPriorityHandler {
fn metadata(&self) -> CommandHandlerMetadata {
CommandHandlerMetadata {
name: "high_priority".to_string(),
version: CommandVersion::default(),
description: "High priority handler".to_string(),
supported_commands: vec!["ListEntities".to_string()],
}
}
fn can_handle(&self, request: &BrpRequest) -> bool {
matches!(request, BrpRequest::ListEntities { .. })
}
async fn handle(&self, _request: BrpRequest) -> Result<BrpResponse> {
Ok(BrpResponse::Success(Box::new(crate::brp_messages::BrpResult::Success)))
}
fn priority(&self) -> i32 {
100 // High priority
}
}
let registry = CommandHandlerRegistry::new();
// Register low priority first
registry.register(Arc::new(CoreBrpHandler)).await;
// Then high priority
registry.register(Arc::new(HighPriorityHandler)).await;
let request = BrpRequest::ListEntities { filter: None };
let handler = registry.find_handler(&request).await.unwrap();
// Should get high priority handler
assert_eq!(handler.priority(), 100);
}
}