(
mut value: Value,
client_id: &str,
default_config: Option<AcpClientConfig>,
enabled: bool,
permission_mode: Option<AcpClientPermissionMode>,
)
| 452 | } |
| 453 | |
| 454 | fn update_client_config_json( |
| 455 | mut value: Value, |
| 456 | client_id: &str, |
| 457 | default_config: Option<AcpClientConfig>, |
| 458 | enabled: bool, |
| 459 | permission_mode: Option<AcpClientPermissionMode>, |
| 460 | ) -> Result<Value> { |
| 461 | ensure_acp_clients_object(&mut value)?; |
| 462 | let acp_clients = value |
| 463 | .get_mut("acpClients") |
| 464 | .and_then(Value::as_object_mut) |
| 465 | .ok_or_else(|| anyhow!("ACP client config must contain an acpClients object"))?; |
| 466 | |
| 467 | let entry = acp_clients.entry(client_id.to_string()).or_insert_with(|| { |
| 468 | default_config |
| 469 | .as_ref() |
| 470 | .and_then(|config| serde_json::to_value(config).ok()) |
| 471 | .unwrap_or_else(|| json!({})) |
| 472 | }); |
| 473 | if !entry.is_object() { |
| 474 | *entry = json!({}); |
| 475 | } |
| 476 | let entry_object = entry |
| 477 | .as_object_mut() |
| 478 | .ok_or_else(|| anyhow!("ACP client '{}' config must be an object", client_id))?; |
| 479 | |
| 480 | if let Some(default_config) = default_config { |
| 481 | let default_value = serde_json::to_value(default_config)?; |
| 482 | let default_object = default_value |
| 483 | .as_object() |
| 484 | .ok_or_else(|| anyhow!("Default ACP client config must be an object"))?; |
| 485 | for (key, value) in default_object { |
| 486 | entry_object |
| 487 | .entry(key.clone()) |
| 488 | .or_insert_with(|| value.clone()); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | entry_object.insert("enabled".to_string(), json!(enabled)); |
| 493 | if let Some(permission_mode) = permission_mode { |
| 494 | entry_object.insert( |
| 495 | "permissionMode".to_string(), |
| 496 | serde_json::to_value(permission_mode)?, |
| 497 | ); |
| 498 | } |
| 499 | |
| 500 | Ok(value) |
| 501 | } |
| 502 | |
| 503 | fn ensure_acp_clients_object(value: &mut Value) -> Result<()> { |
| 504 | if value.get("acpClients").is_none() { |
no test coverage detected