use std::collections::HashMap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
fn is_false(value: &bool) -> bool {
!*value
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct BuildConfig {
pub system: BuildSystem,
}
impl Default for BuildConfig {
fn default() -> Self {
Self {
system: BuildSystem::Cargo(Cargo::default()),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum BuildSystem {
Custom(Custom),
Cargo(Cargo),
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct Custom {
pub build_cmd: String,
pub elf_path: String,
pub to_bin: bool,
}
#[derive(Default, Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct Cargo {
pub env: HashMap<String, String>,
pub target: String,
pub package: String,
#[serde(default)]
pub bin: Option<String>,
pub features: Vec<String>,
pub log: Option<LogLevel>,
pub extra_config: Option<String>,
#[serde(default)]
pub profile: Option<CargoBuildProfile>,
#[serde(default, skip_serializing_if = "is_false")]
pub disable_someboot_build_config: bool,
pub args: Vec<String>,
pub pre_build_cmds: Vec<String>,
pub post_build_cmds: Vec<String>,
pub to_bin: bool,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, JsonSchema, PartialEq, Eq)]
pub enum CargoBuildProfile {
Debug,
Release,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub struct Depend {
pub name: String,
pub d_features: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema, PartialEq)]
pub enum LogLevel {
Trace,
Debug,
Info,
Warn,
Error,
}
#[cfg(test)]
mod tests {
use super::Cargo;
#[test]
fn cargo_config_defaults_someboot_injection_to_enabled_when_field_is_absent() {
let cargo: Cargo = toml::from_str(
r#"
env = {}
target = "x86_64-unknown-none"
package = "kernel"
features = []
args = []
pre_build_cmds = []
post_build_cmds = []
to_bin = false
"#,
)
.unwrap();
assert!(!cargo.disable_someboot_build_config);
}
#[test]
fn cargo_config_omits_someboot_disable_field_when_false() {
let cargo = Cargo {
env: Default::default(),
target: "x86_64-unknown-none".into(),
package: "kernel".into(),
features: Vec::new(),
args: Vec::new(),
pre_build_cmds: Vec::new(),
post_build_cmds: Vec::new(),
to_bin: false,
..Cargo::default()
};
let rendered = toml::to_string(&cargo).unwrap();
assert!(!rendered.contains("disable_someboot_build_config"));
}
#[test]
fn cargo_config_serializes_someboot_disable_field_when_true() {
let cargo = Cargo {
env: Default::default(),
target: "x86_64-unknown-none".into(),
package: "kernel".into(),
features: Vec::new(),
disable_someboot_build_config: true,
args: Vec::new(),
pre_build_cmds: Vec::new(),
post_build_cmds: Vec::new(),
to_bin: false,
..Cargo::default()
};
let rendered = toml::to_string(&cargo).unwrap();
assert!(rendered.contains("disable_someboot_build_config = true"));
}
}