Skip to main content
← Back to kicad-monkey Rust Docs kicad-monkey Rust Crate API Reference

kicad_monkey_contracts/
source_bundle_contract.rs

1//! Strict promoted boundary for the generated source-bundle manifest DTO.
2
3use crate::generated::source_bundle_manifest::SourceBundleManifestA0;
4use crate::{ValidationError, validation_error};
5
6const SCHEMA: &str = "kicad_monkey.source_bundle_manifest.a0";
7const DOCUMENT_TYPE: &str = "kicad_monkey.source_bundle_manifest";
8const VERSION: &str = "a0";
9
10/// Decode JSON and enforce literals that Typify represents as strings.
11pub fn decode_source_bundle_manifest_a0(
12    source: &[u8],
13) -> Result<SourceBundleManifestA0, SourceBundleDecodeError> {
14    let manifest = serde_json::from_slice(source).map_err(SourceBundleDecodeError::Transport)?;
15    validate_source_bundle_manifest_contract(&manifest)
16        .map_err(SourceBundleDecodeError::Validation)?;
17    Ok(manifest)
18}
19
20/// Enforce the a0 envelope before semantic path/slot validation in core.
21pub fn validate_source_bundle_manifest_contract(
22    manifest: &SourceBundleManifestA0,
23) -> Result<(), ValidationError> {
24    require_literal(&manifest.schema, SCHEMA, "$.schema")?;
25    require_literal(&manifest.type_, DOCUMENT_TYPE, "$.type")?;
26    require_literal(&manifest.version, VERSION, "$.version")?;
27    for (index, source) in manifest.sources.iter().enumerate() {
28        validate_canonical_uint64(&source.source_bytes).map_err(|()| {
29            validation_error(
30                "invalid_uint64",
31                format!("$.sources[{index}].source_bytes"),
32                "source_bytes must be canonical unsigned 64-bit decimal",
33            )
34        })?;
35    }
36    Ok(())
37}
38
39fn validate_canonical_uint64(value: &str) -> Result<(), ()> {
40    let canonical_digits = value == "0"
41        || value
42            .as_bytes()
43            .first()
44            .is_some_and(|first| (b'1'..=b'9').contains(first))
45            && value.as_bytes().iter().all(u8::is_ascii_digit);
46    if canonical_digits && value.parse::<u64>().is_ok() {
47        Ok(())
48    } else {
49        Err(())
50    }
51}
52
53fn require_literal(value: &str, expected: &str, path: &str) -> Result<(), ValidationError> {
54    if value == expected {
55        Ok(())
56    } else {
57        Err(validation_error(
58            "unsupported_contract",
59            path,
60            "source bundle manifest literal does not match the a0 contract",
61        ))
62    }
63}
64
65/// Structural JSON or strict-literal failure at the promoted boundary.
66#[derive(Debug)]
67pub enum SourceBundleDecodeError {
68    Transport(serde_json::Error),
69    Validation(ValidationError),
70}
71
72impl std::fmt::Display for SourceBundleDecodeError {
73    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
74        match self {
75            Self::Transport(error) => write!(formatter, "source bundle transport: {error}"),
76            Self::Validation(error) => write!(formatter, "source bundle contract: {error}"),
77        }
78    }
79}
80
81impl std::error::Error for SourceBundleDecodeError {}