diff --git a/src/core_client.rs b/src/core_client.rs index c52e681..8d684c4 100644 --- a/src/core_client.rs +++ b/src/core_client.rs @@ -696,7 +696,7 @@ pub fn verify_command(opts: &RuntimeOpts, args: &VerifyArgs) -> ViewResult }, "analysis": { "schema": "marqdo.analysis", - "schema_version": tool_v1::ANALYSIS_PROTOCOL, + "schema_version": projection.schema_version, "digest": projection.analysis_digest, "producer_version": projection.producer_version, "markdown_profile": projection.markdown_profile, @@ -736,7 +736,8 @@ pub fn verify_command(opts: &RuntimeOpts, args: &VerifyArgs) -> ViewResult ); println!("document: {entry}"); println!( - "analysis: schema=marqdo.analysis v2 modules={} functions={} digest={}", + "analysis: schema=marqdo.analysis v{} modules={} functions={} digest={}", + projection.schema_version, projection.modules.len(), projection.functions.len(), projection.analysis_digest diff --git a/src/protocol/analysis_v2.rs b/src/protocol/analysis_v2.rs index e4f7f58..104217d 100644 --- a/src/protocol/analysis_v2.rs +++ b/src/protocol/analysis_v2.rs @@ -73,6 +73,8 @@ pub enum StatementKind { Branch, Loop, Return, + Break, + Continue, Other, } @@ -84,6 +86,8 @@ impl StatementKind { StatementKind::Branch => "条件分支", StatementKind::Loop => "循环", StatementKind::Return => "返回", + StatementKind::Break => "跳出循环", + StatementKind::Continue => "继续循环", StatementKind::Other => "语句", } } @@ -95,6 +99,8 @@ impl StatementKind { StatementKind::Branch => "branch", StatementKind::Loop => "loop", StatementKind::Return => "return", + StatementKind::Break => "break", + StatementKind::Continue => "continue", StatementKind::Other => "other", } } @@ -210,6 +216,7 @@ pub struct ModuleSummary { #[derive(Debug, Clone)] pub struct SemanticProjection { + pub schema_version: u32, pub analysis_sha256: String, pub analysis_digest: String, pub producer_version: String, @@ -541,19 +548,18 @@ pub fn verify_analysis( ) -> ViewResult { let raw: RawAnalysis = serde_json::from_slice(analysis_bytes).map_err(|err| ViewError::AnalysisFailed { - detail: format!("Analysis v2 is not valid JSON: {err}"), + detail: format!("Analysis is not valid JSON: {err}"), })?; if raw.schema != "marqdo.analysis" { return Err(ViewError::ProtocolIncompatible { detail: format!("unexpected Analysis schema {:?}", raw.schema), }); } - if raw.schema_version != crate::protocol::tool_v1::ANALYSIS_PROTOCOL { + if !crate::protocol::tool_v1::ANALYSIS_PROTOCOLS.contains(&raw.schema_version) { return Err(ViewError::ProtocolIncompatible { detail: format!( - "Analysis schema_version {} is not supported (View implements v{})", - raw.schema_version, - crate::protocol::tool_v1::ANALYSIS_PROTOCOL + "Analysis schema_version {} is not supported (View implements v2/v3)", + raw.schema_version ), }); } @@ -641,7 +647,9 @@ pub fn verify_analysis( let statements = function .body .iter() - .filter_map(|statement| convert_statement(statement, &mut valid_span)) + .filter_map(|statement| { + convert_statement(statement, raw.schema_version, &mut valid_span) + }) .collect(); Some(SemanticFunction { id: function.id.clone(), @@ -777,6 +785,7 @@ pub fn verify_analysis( Ok(VerifiedAnalysis { projection: SemanticProjection { + schema_version: raw.schema_version, analysis_sha256: sha256_hex(analysis_bytes), analysis_digest: sha256_prefixed(analysis_bytes), producer_version: raw.producer.version.clone(), @@ -804,6 +813,7 @@ pub fn verify_analysis( fn convert_statement( raw: &RawStatement, + schema_version: u32, valid_span: &mut impl FnMut(Option, &str) -> Option, ) -> Option { let span = valid_span(raw.span, "语句")?; @@ -813,12 +823,14 @@ fn convert_statement( "branch" => StatementKind::Branch, "loop" => StatementKind::Loop, "return" => StatementKind::Return, + "break" if schema_version == 3 => StatementKind::Break, + "continue" if schema_version == 3 => StatementKind::Continue, _ => StatementKind::Other, }; let body = raw .body .iter() - .filter_map(|child| convert_statement(child, valid_span)) + .filter_map(|child| convert_statement(child, schema_version, valid_span)) .collect(); let arms = raw .arms @@ -829,7 +841,7 @@ fn convert_statement( body: arm .body .iter() - .filter_map(|child| convert_statement(child, valid_span)) + .filter_map(|child| convert_statement(child, schema_version, valid_span)) .collect(), }) .collect(); @@ -905,7 +917,7 @@ mod tests { let index = SourceIndex::new(source); let digest = sha256_hex(source.as_bytes()); let err = - verify_analysis(&analysis(3, &digest), source, &index, "index.mq.md").unwrap_err(); + verify_analysis(&analysis(4, &digest), source, &index, "index.mq.md").unwrap_err(); assert_eq!(err.code(), "view.protocol_incompatible"); } diff --git a/src/protocol/tool_v1.rs b/src/protocol/tool_v1.rs index 2b55320..21dbc16 100644 --- a/src/protocol/tool_v1.rs +++ b/src/protocol/tool_v1.rs @@ -15,6 +15,7 @@ pub const MIN_PRODUCT: &str = "3.0.0-beta.1"; pub const TOOL_PROTOCOL: u32 = 1; pub const ANALYSIS_PROTOCOL: u32 = 2; +pub const ANALYSIS_PROTOCOLS: [u32; 2] = [2, 3]; pub const TRACE_PROTOCOL: u32 = 2; /// Environment variables injected by CLI Tool Protocol v1. @@ -83,10 +84,10 @@ pub fn negotiate_protocol_env( } if let Some(raw) = analysis_protocol { let value = parse_env_u32(ENV_PROTOCOL_ANALYSIS, raw)?; - if value != ANALYSIS_PROTOCOL { + if !ANALYSIS_PROTOCOLS.contains(&value) { return Err(ViewError::ProtocolIncompatible { detail: format!( - "{ENV_PROTOCOL_ANALYSIS}={value} is not supported (this View requires Analysis v{ANALYSIS_PROTOCOL})" + "{ENV_PROTOCOL_ANALYSIS}={value} is not supported (this View implements Analysis v2/v3)" ), }); } @@ -141,6 +142,7 @@ mod tests { fn negotiates_known_protocols_only() { assert!(negotiate_protocol_env(Some("1"), Some("2"), Some("2")).is_ok()); assert!(negotiate_protocol_env(None, None, None).is_ok()); + assert!(negotiate_protocol_env(Some("1"), Some("3"), Some("2")).is_ok()); assert!(negotiate_protocol_env(Some("2"), None, None).is_err()); assert!(negotiate_protocol_env(None, Some("1"), None).is_err()); assert!(negotiate_protocol_env(None, None, Some("3")).is_err()); diff --git a/src/server/routes.rs b/src/server/routes.rs index bb6ff99..077ccd1 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -288,7 +288,7 @@ fn serve_documents(state: &ServerState, request: Request, query: &str) -> ViewRe "source_status": model.source_status.data_value(), "semantics_usable": model.source_status.semantics_usable(), "analysis": semantic.map(|projection| serde_json::json!({ - "schema_version": crate::protocol::tool_v1::ANALYSIS_PROTOCOL, + "schema_version": projection.schema_version, "digest": projection.analysis_digest, "producer_version": projection.producer_version, "project_root_id": projection.project_root_id, diff --git a/tests/contract/no_coupling.rs b/tests/contract/no_coupling.rs index 76e9833..15b0f1b 100644 --- a/tests/contract/no_coupling.rs +++ b/tests/contract/no_coupling.rs @@ -182,7 +182,7 @@ fn tool_manifest_matches_package_and_protocols() { "tool.json version must match Cargo package version" ); assert_eq!(tool["compatibility"]["marqdo"], ">=3.0.0-beta.1,<4.0.0"); - assert_eq!(tool["compatibility"]["analysis"], serde_json::json!([2])); + assert_eq!(tool["compatibility"]["analysis"], serde_json::json!([2, 3])); assert_eq!(tool["compatibility"]["trace"], serde_json::json!([2])); } diff --git a/tool.json b/tool.json index 5e4bab7..e14c2a8 100644 --- a/tool.json +++ b/tool.json @@ -7,7 +7,7 @@ "entrypoint": "bin/marqdo-view", "compatibility": { "marqdo": ">=3.0.0-beta.1,<4.0.0", - "analysis": [2], + "analysis": [2, 3], "trace": [2] } }