feat: add source and public protocol inputs

This commit is contained in:
cflmy committed 2026-11-23 16:58:12 +08:00
1 parent 68b14db095
commit e67697be06
30 files changed
+3021

No files matched your search

+130
View File
@@ -0,0 +1,130 @@
use serde::{Deserialize, Serialize};
use serde_json::Value;
use super::Span;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Producer {
pub name: String,
pub version: String,
pub markdown_profile: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisFunction {
pub id: String,
pub unit_id: String,
pub local_name: String,
pub span: Span,
pub heading_span: Span,
pub signature: Value,
#[serde(default)]
pub effects: Vec<String>,
pub body: Vec<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisModule {
pub id: String,
pub package_instance_id: String,
pub relative_path: String,
pub source_digest: String,
pub units: Vec<Value>,
pub functions: Vec<AnalysisFunction>,
#[serde(default)]
pub imports: Vec<Value>,
#[serde(default)]
pub references: Vec<Value>,
#[serde(default)]
pub calls: Vec<Value>,
#[serde(default)]
pub resources: Vec<Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CoreDiagnostic {
pub module: Option<String>,
pub relative_path: Option<String>,
pub span: Option<Span>,
pub line: Option<usize>,
pub column: Option<usize>,
pub severity: String,
pub code: String,
pub message: String,
#[serde(default)]
pub hints: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct ExecutionProjection {
pub producer: Producer,
pub project: Value,
pub modules: Vec<AnalysisModule>,
pub diagnostics: Vec<CoreDiagnostic>,
pub raw: Value,
}
impl ExecutionProjection {
pub fn from_json(raw: Value) -> Result<Self, String> {
if raw.get("schema").and_then(Value::as_str) != Some("marqdo.analysis") {
return Err("Analysis schema 不是 marqdo.analysis".into());
}
if raw.get("schema_version").and_then(Value::as_u64) != Some(2) {
return Err("只支持 Analysis schema_version 2".into());
}
#[derive(Deserialize)]
struct Wire {
producer: Producer,
project: Value,
modules: Vec<AnalysisModule>,
#[serde(default)]
diagnostics: Vec<CoreDiagnostic>,
}
let wire: Wire = serde_json::from_value(raw.clone()).map_err(|error| error.to_string())?;
if wire.producer.markdown_profile != "marqdo.md.v1" {
return Err(format!(
"不支持的 Markdown Profile {}",
wire.producer.markdown_profile
));
}
for module in &wire.modules {
if module.source_digest.len() != 64
|| !module.source_digest.bytes().all(|byte| byte.is_ascii_hexdigit())
{
return Err(format!("模块 {} 的 source_digest 无效", module.id));
}
if module.relative_path.starts_with('/') || module.relative_path.contains("..") {
return Err(format!("模块 {} 的 relative_path 不安全", module.id));
}
}
Ok(Self {
producer: wire.producer,
project: wire.project,
modules: wire.modules,
diagnostics: wire.diagnostics,
raw,
})
}
}
pub fn semantic_nodes(body: &[Value]) -> impl Iterator<Item = &Value> {
body.iter().flat_map(walk_node)
}
fn walk_node(node: &Value) -> Box<dyn Iterator<Item = &Value> + '_> {
let mut nested = Vec::new();
if let Some(object) = node.as_object() {
for value in object.values() {
match value {
Value::Object(_) => nested.push(value),
Value::Array(array) => {
nested.extend(array.iter().filter(|value| value.is_object()));
}
_ => {}
}
}
}
Box::new(
std::iter::once(node).chain(nested.into_iter().flat_map(|value| walk_node(value))),
)
}