Files
marqdo-web-design/src/diagnostic.rs
T
cflmy 75cd26ef89 feat: WEB-01 core Design interpreter over public Analysis 3
Implement the first real Design capability in the unique owner repository:
- src/analysis.rs runs the verified CLI check --emit with direct argv, validates
  Analysis 3 schema/source_digest, and snapshots one module without any implicit
  discovery or Markdown parser.
- src/document.rs selects the exact Knowledge H2 by anchor, direct retained H3
  sections, and the single GFM 组件 table from the analysis AST; cell text is
  read from the analysis-provided source by UTF-8 byte span.
- src/lib.rs binds design links by exact candidate_path + anchor (never label or
  filename guessing), validates rows/slots/widths, recursion, containment cycles
  and module/depth/byte limits, and builds deterministic component definitions,
  separate instances, source identities and a provisional handoff.
- Stable diagnostics separate Engine failures (exit 2) from author-contract
  errors (exit 1); no partial model on failure.
- tests/design_contract.rs exercises the real CLI for recursive layout,
  definition reuse, width forms and the main negative cases; scripts/ci.sh is the
  explicit entry (fmt/clippy/test) with a verified CLI.

Scenarios/input/event/dependency contracts and the full WEB-03 handoff remain
open and are recorded as unverified in the evidence document.

Refs cflmy/marqdo-web-design#1, cflmy/marqdo#6
2026-11-27 02:33:33 +08:00

87 lines
2.0 KiB
Rust

//! Stable diagnostic codes and source locations.
use serde_json::Value;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
Error,
Warning,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Span {
pub start: usize,
pub end: usize,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Diagnostic {
pub code: &'static str,
pub severity: Severity,
pub message: String,
pub path: Option<String>,
pub anchor: Option<String>,
pub span: Option<Span>,
}
impl Diagnostic {
pub fn error(code: &'static str, message: impl Into<String>) -> Self {
Self {
code,
severity: Severity::Error,
message: message.into(),
path: None,
anchor: None,
span: None,
}
}
pub fn at(mut self, path: impl Into<String>, span: Option<(usize, usize)>) -> Self {
self.path = Some(path.into());
self.span = span.map(|(start, end)| Span { start, end });
self
}
pub fn with_anchor(mut self, anchor: impl Into<String>) -> Self {
self.anchor = Some(anchor.into());
self
}
}
pub type Diagnostics = Vec<Diagnostic>;
#[derive(Debug, Clone)]
pub enum LoadError {
/// The CLI/engine/protocol failed; hosts map this to exit code 2.
Engine(Diagnostics),
/// The author document violates the design contract; hosts map this to exit code 1.
Design(Diagnostics),
}
impl From<Diagnostic> for LoadError {
fn from(diagnostic: Diagnostic) -> Self {
LoadError::Design(vec![diagnostic])
}
}
impl LoadError {
pub fn diagnostics(&self) -> &Diagnostics {
match self {
Self::Engine(items) | Self::Design(items) => items,
}
}
pub fn into_diagnostics(self) -> Diagnostics {
match self {
Self::Engine(items) | Self::Design(items) => items,
}
}
}
pub(crate) fn span_from(value: &Value) -> Option<(usize, usize)> {
Some((
value.get("start")?.as_u64()? as usize,
value.get("end")?.as_u64()? as usize,
))
}