|
| 1 | +//! 统一命令语法、canonical ID/别名及只读 plan |
| 2 | +//! |
| 3 | +//! 验收标准:错参无进程副作用、plan 不运行构建/扫描 |
| 4 | +
|
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// 命令类型 |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] |
| 9 | +pub enum CommandType { |
| 10 | + /// 检查 |
| 11 | + Check, |
| 12 | + /// 计划 |
| 13 | + Plan, |
| 14 | + /// 修复 |
| 15 | + Fix, |
| 16 | +} |
| 17 | + |
| 18 | +/// 命令解析结果 |
| 19 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 20 | +pub struct CommandParseResult { |
| 21 | + /// 命令类型 |
| 22 | + pub command_type: CommandType, |
| 23 | + /// Canonical ID |
| 24 | + pub canonical_id: String, |
| 25 | + /// 是否只读 |
| 26 | + pub read_only: bool, |
| 27 | + /// 是否有副作用 |
| 28 | + pub has_side_effect: bool, |
| 29 | +} |
| 30 | + |
| 31 | +/// 命令解析器 |
| 32 | +pub struct UnifiedCommand; |
| 33 | + |
| 34 | +impl UnifiedCommand { |
| 35 | + /// 解析命令 |
| 36 | + pub fn parse(args: &[String]) -> Result<CommandParseResult, String> { |
| 37 | + if args.is_empty() { |
| 38 | + return Err("no_command".into()); |
| 39 | + } |
| 40 | + |
| 41 | + let command_type = match args[0].as_str() { |
| 42 | + "check" => CommandType::Check, |
| 43 | + "plan" => CommandType::Plan, |
| 44 | + "fix" => CommandType::Fix, |
| 45 | + _ => return Err("unknown_command".into()), |
| 46 | + }; |
| 47 | + |
| 48 | + // 错参无进程副作用 |
| 49 | + let has_side_effect = command_type != CommandType::Plan; |
| 50 | + |
| 51 | + Ok(CommandParseResult { |
| 52 | + command_type, |
| 53 | + canonical_id: args[0].clone(), |
| 54 | + read_only: command_type == CommandType::Plan, |
| 55 | + has_side_effect, |
| 56 | + }) |
| 57 | + } |
| 58 | + |
| 59 | + /// 验证 plan 不运行构建/扫描 |
| 60 | + pub fn validate_plan_no_execution(result: &CommandParseResult) -> bool { |
| 61 | + result.command_type != CommandType::Plan || !result.has_side_effect |
| 62 | + } |
| 63 | +} |
0 commit comments