Skip to main content

alp_core/
preview.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Effective-config preview helpers (Rust mirror of LSP preview payload).
3
4use serde::Serialize;
5
6use crate::model::{BoardModel, normalize_board_model};
7pub use crate::project::ProjectContext;
8use crate::validate::{ParseError, parse_board_model};
9
10/// Serializable effective-config preview payload (mirrors the LSP preview JSON).
11#[derive(Debug, Clone, Serialize)]
12#[serde(rename_all = "camelCase")]
13pub struct EffectiveConfigPreviewPayload {
14    /// Payload schema version; currently always `"1"`.
15    pub schema_version: String,
16    /// Timestamp the payload was generated (caller-supplied, ISO-8601 string).
17    pub generated_at: String,
18    /// Path to the source `board.yaml`.
19    pub board_yaml_path: String,
20    /// Resolved project context (workspace/SDK roots, west cwd, python binary).
21    pub project_context: ProjectContext,
22    /// Parsed and normalized board model — the effective configuration.
23    pub effective_config: BoardModel,
24}
25
26/// Create an effective-config preview payload equivalent to TS
27/// `createEffectiveConfigPreviewPayload`:
28/// parse board.yaml, normalize it, and wrap with metadata.
29pub fn create_effective_config_preview_payload(
30    document_text: &str,
31    board_yaml_path: &str,
32    project_context: ProjectContext,
33    generated_at: impl Into<String>,
34) -> Result<EffectiveConfigPreviewPayload, ParseError> {
35    let parsed = parse_board_model(document_text)?;
36    let effective_config = normalize_board_model(parsed);
37
38    Ok(EffectiveConfigPreviewPayload {
39        schema_version: "1".to_string(),
40        generated_at: generated_at.into(),
41        board_yaml_path: board_yaml_path.to_string(),
42        project_context,
43        effective_config,
44    })
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn preview_payload_uses_normalized_model() {
53        let text = "schema_version: 2\nos: zephyr\ncores:\n  m55_hp:\n    app: ./src\n";
54        let payload = create_effective_config_preview_payload(
55            text,
56            "board.yaml",
57            ProjectContext {
58                workspace_root: Some("/ws".to_string()),
59                sdk_root: Some("/sdk".to_string()),
60                board_yaml_path: Some("/ws/board.yaml".to_string()),
61                west_cwd: Some("/ws".to_string()),
62                python_binary: "python3".to_string(),
63            },
64            "2026-01-01T00:00:00.000Z",
65        )
66        .unwrap();
67
68        assert_eq!(payload.schema_version, "1");
69        assert_eq!(payload.board_yaml_path, "board.yaml");
70        assert!(payload.effective_config.os.is_none());
71    }
72}