Skip to main content

alp_core/
loader.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Generation-target catalog — a port of the TS loader
3//! `GENERATION_TARGET_CATALOG` / `listGenerationTargetSupport`. Static metadata
4//! describing the four emit targets (used by `explain`, and later `generate`).
5
6/// Static metadata for one generation/emit target (Zephyr conf, DTS overlay, etc.).
7pub struct GenerationTargetSupport {
8    /// Emit-mode key (e.g. `zephyr-conf`); the stable identifier used for lookup.
9    pub emit: &'static str,
10    /// Human-readable name shown in UI.
11    pub display_name: &'static str,
12    /// Output path relative to the workspace root.
13    pub output_relative_path: &'static str,
14    /// Label for a preview pane of the generated output.
15    pub preview_label: &'static str,
16    /// Editor/VS Code language id for syntax-highlighting the preview.
17    pub preview_language_id: &'static str,
18}
19
20static GENERATION_TARGET_CATALOG: &[GenerationTargetSupport] = &[
21    GenerationTargetSupport {
22        emit: "zephyr-conf",
23        display_name: "Zephyr config",
24        output_relative_path: "build/generated/alp.conf",
25        preview_label: "Zephyr config preview",
26        preview_language_id: "properties",
27    },
28    GenerationTargetSupport {
29        emit: "dts-overlay",
30        display_name: "Devicetree overlay",
31        output_relative_path: "build/generated/alp.overlay",
32        preview_label: "Devicetree overlay preview",
33        preview_language_id: "dts",
34    },
35    GenerationTargetSupport {
36        emit: "cmake-args",
37        display_name: "CMake args",
38        output_relative_path: "build/generated/alp-cmake-args.txt",
39        preview_label: "CMake args preview",
40        preview_language_id: "plaintext",
41    },
42    GenerationTargetSupport {
43        emit: "yocto-conf",
44        display_name: "Yocto config",
45        output_relative_path: "build/generated/alp-yocto.conf",
46        preview_label: "Yocto config preview",
47        preview_language_id: "properties",
48    },
49];
50
51/// Returns the full catalog of generation targets, in fixed catalog order.
52pub fn list_generation_target_support() -> &'static [GenerationTargetSupport] {
53    GENERATION_TARGET_CATALOG
54}
55
56/// Looks up a target by its `emit` key; `None` if no target matches.
57pub fn generation_target_support(emit: &str) -> Option<&'static GenerationTargetSupport> {
58    GENERATION_TARGET_CATALOG.iter().find(|t| t.emit == emit)
59}
60
61/// The four emit modes, in catalog order (mirrors TS `ALL_EMIT_MODES`).
62pub const ALL_EMIT_MODES: [&str; 4] = ["zephyr-conf", "dts-overlay", "cmake-args", "yocto-conf"];
63
64/// The output path + command line a loader run would use (mirror of TS
65/// `createLoaderPlan`, limited to the fields `trace`/`support-bundle` surface).
66/// `emit` must be a valid target; paths are joined as given (callers pass
67/// resolved roots).
68pub struct LoaderPlan {
69    /// Absolute output path (workspace root joined with the target's relative path).
70    pub output_path: String,
71    /// Full python invocation that would run the loader script.
72    pub command_line: String,
73}
74
75/// Builds the `LoaderPlan` for `target`: resolves the output path under
76/// `workspace_root` and formats the `alp_project.py` command line.
77pub fn create_loader_plan(
78    workspace_root: &str,
79    sdk_root: &str,
80    board_yaml_path: &str,
81    python_binary: &str,
82    target: &GenerationTargetSupport,
83) -> LoaderPlan {
84    let output_path = std::path::Path::new(workspace_root)
85        .join(target.output_relative_path)
86        .to_string_lossy()
87        .to_string();
88    let script_path = std::path::Path::new(sdk_root)
89        .join("scripts")
90        .join("alp_project.py")
91        .to_string_lossy()
92        .to_string();
93    let command_line = format!(
94        "{python_binary} {script_path} --input {board_yaml_path} --emit {} --output {output_path}",
95        target.emit
96    );
97    LoaderPlan {
98        output_path,
99        command_line,
100    }
101}
102
103#[cfg(test)]
104mod tests {
105    use super::*;
106
107    #[test]
108    fn catalog_lists_four_targets_in_order() {
109        let emits: Vec<&str> = list_generation_target_support()
110            .iter()
111            .map(|t| t.emit)
112            .collect();
113        assert_eq!(
114            emits,
115            ["zephyr-conf", "dts-overlay", "cmake-args", "yocto-conf"]
116        );
117    }
118
119    #[test]
120    fn lookup_by_emit() {
121        assert_eq!(
122            generation_target_support("cmake-args").map(|t| t.display_name),
123            Some("CMake args")
124        );
125        assert!(generation_target_support("bogus").is_none());
126    }
127}