Skip to main content

device_driver_codegen/
lib.rs

1use clap::Subcommand;
2use device_driver_lir::model::Driver;
3use itertools::Itertools;
4
5pub use crate::rust::RustCodegenOptions;
6
7mod rust;
8
9#[derive(Debug, Clone, Subcommand)]
10pub enum Target {
11    /// Generate Rust code
12    Rust(RustCodegenOptions),
13}
14
15impl Target {
16    pub fn create_error_message(&self) -> &'static str {
17        match self {
18            Target::Rust(_) => {
19                "compile_error!(\"The device driver input has errors that need to be solved!\");"
20            }
21        }
22    }
23
24    /// Converts the multiline text to comments that work for the target
25    pub fn to_comments(&self, text: &str) -> String {
26        match self {
27            Target::Rust(_) => text.lines().map(|line| format!("// {line}")).join("\n"),
28        }
29    }
30}
31
32pub fn codegen(target: &Target, lir_driver: &Driver, source: &str) -> String {
33    match target {
34        Target::Rust(codegen_options) => {
35            rust::DriverTemplateRust::new(lir_driver, source, codegen_options).to_string()
36        }
37    }
38}