moddef-codegen 0.1.1

Generate typed Rust device clients from ModDef documents
Documentation
// SPDX-License-Identifier: Apache-2.0

//! Generator unit tests: deterministic output, naming policy, and the key
//! emitted items for a small document. Full compile + behavior coverage
//! lives in the conformance crate (build.rs gate + mock-transport e2e).

use moddef_core::{parse_document, DocumentFormat};

const DOC: &str = r#"{
  "docId": "test.mini",
  "version": "1.0.0",
  "enums": [{
    "typeId": "run_state",
    "values": [
      {"name": "WAITING", "value": "0"},
      {"name": "NORMAL", "value": "1"},
      {"name": "UNKNOWN", "value": "2"}
    ]
  }],
  "devices": [{
    "deviceId": "mini",
    "vendor": "Test",
    "model": "M1",
    "blocks": [{
      "blockId": "b",
      "space": "HOLDING_REGISTER",
      "startOffset": 0,
      "lengthWords": 8,
      "points": [
        {
          "pointId": "state",
          "access": "READ_ONLY",
          "storageType": "U16",
          "valueType": {"enumRef": {"typeId": "run_state"}},
          "mapping": {"space": "HOLDING_REGISTER", "offset": 0, "lengthWords": 1}
        },
        {
          "pointId": "voltage",
          "access": "READ_ONLY",
          "storageType": "U16",
          "valueType": {"primitive": "DECIMAL"},
          "mapping": {"space": "HOLDING_REGISTER", "offset": 1, "lengthWords": 1},
          "transform": {"scale": {"numerator": "1", "denominator": "10"}},
          "measurand": {"baseQuantity": "voltage", "phaseRef": "L1_N"}
        },
        {
          "pointId": "voltage_l2",
          "access": "READ_ONLY",
          "storageType": "U16",
          "valueType": {"primitive": "DECIMAL"},
          "mapping": {"space": "HOLDING_REGISTER", "offset": 2, "lengthWords": 1},
          "transform": {"scale": {"numerator": "1", "denominator": "10"}},
          "measurand": {"baseQuantity": "voltage", "phaseRef": "L2_N"}
        }
      ]
    }]
  }]
}"#;

#[test]
fn deterministic_and_named_as_expected() {
    let doc = parse_document(DOC.as_bytes(), DocumentFormat::Json).unwrap();
    let a = moddef_codegen::generate(&doc);
    let b = moddef_codegen::generate(&doc);
    assert_eq!(a.len(), 1);
    assert_eq!(a[0].path, "test_mini.rs");
    assert_eq!(a[0].content, b[0].content, "output must be deterministic");

    let src = &a[0].content;
    // Enum with a lossless Unknown variant; the profile's own UNKNOWN member
    // must not collide with it (Pascal suffix, not snake).
    assert!(src.contains("pub enum RunState"));
    assert!(src.contains("Unknown2,"));
    assert!(src.contains("Unknown(u16)"));
    // Descriptor table + typed client.
    assert!(src.contains("pub static POINTS: &[PointDesc<'static>]"));
    assert!(src.contains("pub struct Mini<T: Transport>"));
    assert!(src.contains("pub async fn state(&mut self) -> Result<RunState, Error<T::Error>>"));
    // ยง26.2 measurand convenience with a narrowed qualifier enum (the
    // signature may be line-wrapped by prettyplease).
    assert!(src.contains("pub enum VoltagePhase"));
    assert!(src.contains("pub async fn get_voltage("));
    assert!(src.contains("phase: VoltagePhase"));
    assert!(src.contains("VoltagePhase::L2N => self.voltage_l2().await"));
    // Header marker.
    assert!(src.starts_with("// Code generated by moddef-codegen"));
}