use core::fmt::Display;
use std::{collections::HashMap, env, fmt, fs, io::Write, path::PathBuf};
use serde::{Deserialize, Serialize};
use somni_expr::TypeSet128;
use crate::generate::{validator::Validator, value::Value};
mod markdown;
pub(crate) mod validator;
pub(crate) mod value;
#[derive(Clone, PartialEq, Eq)]
pub enum Error {
Parse(String),
Validation(String),
}
impl Error {
pub fn parse<S>(message: S) -> Self
where
S: Into<String>,
{
Self::Parse(message.into())
}
pub fn validation<S>(message: S) -> Self
where
S: Into<String>,
{
Self::Validation(message.into())
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self}")
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Parse(message) => write!(f, "{message}"),
Error::Validation(message) => write!(f, "{message}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
None
}
fn description(&self) -> &str {
"description() is deprecated; use Display"
}
fn cause(&self) -> Option<&dyn core::error::Error> {
self.source()
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct Config {
#[serde(rename = "crate")]
pub krate: String,
pub options: Vec<CfgOption>,
pub checks: Option<Vec<String>>,
}
fn true_default() -> String {
"true".to_string()
}
fn unstable_default() -> Stability {
Stability::Unstable
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct CfgDefaultValue {
#[serde(rename = "if")]
#[serde(default = "true_default")]
pub if_: String,
pub value: Value,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct CfgOption {
pub name: String,
pub description: String,
#[serde(default = "true_default")]
pub active: String,
pub default: Vec<CfgDefaultValue>,
pub constraints: Option<Vec<CfgConstraint>>,
pub display_hint: Option<DisplayHint>,
#[serde(default = "unstable_default")]
pub stability: Stability,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub struct CfgConstraint {
#[serde(rename = "if")]
#[serde(default = "true_default")]
if_: String,
#[serde(rename = "type")]
type_: Validator,
}
pub fn generate_config_from_yaml_definition(
yaml: &str,
enable_unstable: bool,
emit_md_tables: bool,
chip: Option<esp_metadata_generated::Chip>,
) -> Result<HashMap<String, Value>, Error> {
let features: Vec<String> = env::vars()
.filter(|(k, _)| k.starts_with("CARGO_FEATURE_"))
.map(|(k, _)| k)
.map(|v| {
v.strip_prefix("CARGO_FEATURE_")
.unwrap_or_default()
.to_string()
})
.collect();
let (config, options) = evaluate_yaml_config(yaml, chip, features, false)?;
let cfg = generate_config(&config.krate, &options, enable_unstable, emit_md_tables);
do_checks(config.checks.as_ref(), &cfg)?;
Ok(cfg)
}
pub fn do_checks(checks: Option<&Vec<String>>, cfg: &HashMap<String, Value>) -> Result<(), Error> {
if let Some(checks) = checks {
let mut eval_ctx = somni_expr::Context::<TypeSet128>::new_with_types();
for (k, v) in cfg.iter() {
match v {
Value::Bool(v) => eval_ctx.add_variable(k, *v),
Value::Integer(v) => eval_ctx.add_variable(k, *v),
Value::String(v) => eval_ctx.add_variable::<&str>(k, v),
}
}
for check in checks {
if !eval_ctx
.evaluate::<bool>(check)
.map_err(|err| Error::Parse(format!("Validation error: {err:?}")))?
{
return Err(Error::Validation(format!("Validation error: '{check}'")));
}
}
};
Ok(())
}
pub fn evaluate_yaml_config(
yaml: &str,
chip: Option<esp_metadata_generated::Chip>,
features: Vec<String>,
ignore_feature_gates: bool,
) -> Result<(Config, Vec<ConfigOption>), Error> {
let config: Config = serde_yaml::from_str(yaml).map_err(|err| Error::Parse(err.to_string()))?;
let mut options = Vec::new();
let mut eval_ctx = somni_expr::Context::new();
if let Some(chip) = chip {
eval_ctx.add_variable("chip", chip.name());
eval_ctx.add_variable("ignore_feature_gates", ignore_feature_gates);
eval_ctx.add_function("feature", move |feature: &str| chip.contains(feature));
eval_ctx.add_function("cargo_feature", |feature: &str| {
features.contains(&feature.to_uppercase().replace("-", "_"))
});
}
for option in &config.options {
let active = eval_ctx
.evaluate::<bool>(&option.active)
.map_err(|err| Error::Parse(format!("{err:?}")))?;
let constraint = {
let mut active_constraint = None;
if let Some(constraints) = &option.constraints {
for constraint in constraints {
if eval_ctx
.evaluate::<bool>(&constraint.if_)
.map_err(|err| Error::Parse(format!("{err:?}")))?
{
active_constraint = Some(constraint.type_.clone());
break;
}
}
};
if option.constraints.is_some() && active_constraint.is_none() {
panic!(
"No constraint active for crate {}, option {}",
config.krate, option.name
);
}
active_constraint
};
let default_value = {
let mut default_value = None;
for value in &option.default {
if eval_ctx
.evaluate::<bool>(&value.if_)
.map_err(|err| Error::Parse(format!("{err:?}")))?
{
default_value = Some(value.value.clone());
break;
}
}
if default_value.is_none() {
panic!(
"No default value active for crate {}, option {}",
config.krate, option.name
);
}
default_value
};
let option = ConfigOption {
name: option.name.clone(),
description: option.description.clone(),
default_value: default_value.ok_or_else(|| {
Error::Parse(format!("No default value found for {}", option.name))
})?,
constraint,
stability: option.stability.clone(),
active,
display_hint: option.display_hint.unwrap_or(DisplayHint::None),
};
options.push(option);
}
Ok((config, options))
}
pub fn generate_config(
crate_name: &str,
config: &[ConfigOption],
enable_unstable: bool,
emit_md_tables: bool,
) -> HashMap<String, Value> {
let configs = generate_config_internal(std::io::stdout(), crate_name, config, enable_unstable);
if emit_md_tables {
let file_name = snake_case(crate_name);
let mut doc_table = markdown::DOC_TABLE_HEADER.replace(
"{prefix}",
format!("{}_CONFIG_*", screaming_snake_case(crate_name)).as_str(),
);
let mut selected_config = String::from(markdown::SELECTED_TABLE_HEADER);
for (name, option, value) in configs.iter() {
if !option.active {
continue;
}
markdown::write_doc_table_line(&mut doc_table, name, option);
markdown::write_summary_table_line(&mut selected_config, name, value);
}
write_out_file(format!("{file_name}_config_table.md"), doc_table);
write_out_file(format!("{file_name}_selected_config.md"), selected_config);
}
configs.into_iter().map(|(k, _, v)| (k, v)).collect()
}
pub fn generate_config_internal<'a>(
mut stdout: impl Write,
crate_name: &str,
config: &'a [ConfigOption],
enable_unstable: bool,
) -> Vec<(String, &'a ConfigOption, Value)> {
writeln!(stdout, "cargo:rerun-if-changed=build.rs").ok();
let prefix = format!("{}_CONFIG_", screaming_snake_case(crate_name));
let mut configs = create_config(&prefix, config);
capture_from_env(crate_name, &prefix, &mut configs, enable_unstable);
for (_, option, value) in configs.iter() {
if let Some(ref validator) = option.constraint {
validator.validate(value).unwrap_or_else(|err| {
panic!(
"Validation error for crate {}, option {}: {err}",
crate_name, option.name
)
});
}
}
emit_configuration(&mut stdout, &configs);
configs
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub enum Stability {
Unstable,
Stable(String),
}
impl Display for Stability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Stability::Unstable => write!(f, "⚠️ Unstable"),
Stability::Stable(version) => write!(f, "Stable since {version}"),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisplayHint {
None,
Binary,
Hex,
Octal,
}
impl DisplayHint {
pub fn format_value(self, value: &Value) -> String {
match value {
Value::Bool(b) => b.to_string(),
Value::Integer(i) => match self {
DisplayHint::None => format!("{i}"),
DisplayHint::Binary => format!("0b{i:0b}"),
DisplayHint::Hex => format!("0x{i:X}"),
DisplayHint::Octal => format!("0o{i:o}"),
},
Value::String(s) => s.clone(),
}
}
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct ConfigOption {
pub name: String,
pub description: String,
pub default_value: Value,
pub constraint: Option<Validator>,
pub stability: Stability,
pub active: bool,
pub display_hint: DisplayHint,
}
impl ConfigOption {
pub fn full_env_var(&self, crate_name: &str) -> String {
self.env_var(&format!("{}_CONFIG_", screaming_snake_case(crate_name)))
}
fn env_var(&self, prefix: &str) -> String {
format!("{}{}", prefix, screaming_snake_case(&self.name))
}
fn cfg_name(&self) -> String {
snake_case(&self.name)
}
fn is_stable(&self) -> bool {
matches!(self.stability, Stability::Stable(_))
}
}
fn create_config<'a>(
prefix: &str,
config: &'a [ConfigOption],
) -> Vec<(String, &'a ConfigOption, Value)> {
let mut configs = Vec::with_capacity(config.len());
for option in config {
configs.push((option.env_var(prefix), option, option.default_value.clone()));
}
configs
}
fn capture_from_env(
crate_name: &str,
prefix: &str,
configs: &mut Vec<(String, &ConfigOption, Value)>,
enable_unstable: bool,
) {
let mut unknown = Vec::new();
let mut failed = Vec::new();
let mut unstable = Vec::new();
for (var, value) in env::vars() {
if var.starts_with(prefix) {
let Some((_, option, cfg)) = configs.iter_mut().find(|(k, _, _)| k == &var) else {
unknown.push(var);
continue;
};
if !option.active {
unknown.push(var);
continue;
}
if !enable_unstable && !option.is_stable() {
unstable.push(var);
continue;
}
if let Err(e) = cfg.parse_in_place(&value) {
failed.push(format!("{var}: {e}"));
}
}
}
if !failed.is_empty() {
panic!("Invalid configuration options detected: {failed:?}");
}
if !unstable.is_empty() {
panic!(
"The following configuration options are unstable: {unstable:?}. You can enable it by \
activating the 'unstable' feature in {crate_name}."
);
}
if !unknown.is_empty() {
panic!("Unknown configuration options detected: {unknown:?}");
}
}
fn emit_configuration(mut stdout: impl Write, configs: &[(String, &ConfigOption, Value)]) {
for (env_var_name, option, value) in configs.iter() {
let cfg_name = option.cfg_name();
writeln!(stdout, "cargo:rustc-env={env_var_name}={value}").ok();
writeln!(stdout, "cargo:rerun-if-env-changed={env_var_name}").ok();
writeln!(stdout, "cargo:rustc-check-cfg=cfg({cfg_name})").ok();
if let Value::Bool(true) = value {
writeln!(stdout, "cargo:rustc-cfg={cfg_name}").ok();
}
if let Some(validator) = option.constraint.as_ref() {
validator.emit_cargo_extras(&mut stdout, &cfg_name, value);
}
}
}
fn write_out_file(file_name: String, json: String) {
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
let out_file = out_dir.join(file_name);
fs::write(out_file, json).unwrap();
}
fn snake_case(name: &str) -> String {
let mut name = name.replace("-", "_");
name.make_ascii_lowercase();
name
}
fn screaming_snake_case(name: &str) -> String {
let mut name = name.replace("-", "_");
name.make_ascii_uppercase();
name
}
#[cfg(test)]
mod tests {
use super::*;
use crate::generate::{validator::Validator, value::Value};
#[test]
fn value_number_formats() {
const INPUTS: &[&str] = &["0xAA", "0o252", "0b0000000010101010", "170"];
let mut v = Value::Integer(0);
for input in INPUTS {
v.parse_in_place(input).unwrap();
assert_eq!(v.to_string(), "170");
}
}
#[test]
fn value_bool_inputs() {
let mut v = Value::Bool(false);
v.parse_in_place("true").unwrap();
assert_eq!(v.to_string(), "true");
v.parse_in_place("false").unwrap();
assert_eq!(v.to_string(), "false");
v.parse_in_place("else")
.expect_err("Only true or false are valid");
}
#[test]
fn env_override() {
temp_env::with_vars(
[
("ESP_TEST_CONFIG_NUMBER", Some("0xaa")),
("ESP_TEST_CONFIG_NUMBER_SIGNED", Some("-999")),
("ESP_TEST_CONFIG_STRING", Some("Hello world!")),
("ESP_TEST_CONFIG_BOOL", Some("true")),
],
|| {
let configs = generate_config(
"esp-test",
&[
ConfigOption {
name: String::from("number"),
description: String::from("NA"),
default_value: Value::Integer(999),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("number_signed"),
description: String::from("NA"),
default_value: Value::Integer(-777),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("string"),
description: String::from("NA"),
default_value: Value::String("Demo".to_string()),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("bool"),
description: String::from("NA"),
default_value: Value::Bool(false),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("number_default"),
description: String::from("NA"),
default_value: Value::Integer(999),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("string_default"),
description: String::from("NA"),
default_value: Value::String("Demo".to_string()),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("bool_default"),
description: String::from("NA"),
default_value: Value::Bool(false),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
],
false,
false,
);
assert_eq!(configs["ESP_TEST_CONFIG_NUMBER"], Value::Integer(0xaa));
assert_eq!(
configs["ESP_TEST_CONFIG_NUMBER_SIGNED"],
Value::Integer(-999)
);
assert_eq!(
configs["ESP_TEST_CONFIG_STRING"],
Value::String("Hello world!".to_string())
);
assert_eq!(configs["ESP_TEST_CONFIG_BOOL"], Value::Bool(true));
assert_eq!(
configs["ESP_TEST_CONFIG_NUMBER_DEFAULT"],
Value::Integer(999)
);
assert_eq!(
configs["ESP_TEST_CONFIG_STRING_DEFAULT"],
Value::String("Demo".to_string())
);
assert_eq!(configs["ESP_TEST_CONFIG_BOOL_DEFAULT"], Value::Bool(false));
},
)
}
#[test]
fn builtin_validation_passes() {
temp_env::with_vars(
[
("ESP_TEST_CONFIG_POSITIVE_NUMBER", Some("7")),
("ESP_TEST_CONFIG_NEGATIVE_NUMBER", Some("-1")),
("ESP_TEST_CONFIG_NON_NEGATIVE_NUMBER", Some("0")),
("ESP_TEST_CONFIG_RANGE", Some("9")),
],
|| {
generate_config(
"esp-test",
&[
ConfigOption {
name: String::from("positive_number"),
description: String::from("NA"),
default_value: Value::Integer(-1),
constraint: Some(Validator::PositiveInteger),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("negative_number"),
description: String::from("NA"),
default_value: Value::Integer(1),
constraint: Some(Validator::NegativeInteger),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("non_negative_number"),
description: String::from("NA"),
default_value: Value::Integer(-1),
constraint: Some(Validator::NonNegativeInteger),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: String::from("range"),
description: String::from("NA"),
default_value: Value::Integer(0),
constraint: Some(Validator::IntegerInRange(5..10)),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
},
],
false,
false,
)
},
);
}
#[test]
#[should_panic]
fn builtin_validation_bails() {
temp_env::with_vars([("ESP_TEST_CONFIG_POSITIVE_NUMBER", Some("-99"))], || {
generate_config(
"esp-test",
&[ConfigOption {
name: String::from("positive_number"),
description: String::from("NA"),
default_value: Value::Integer(-1),
constraint: Some(Validator::PositiveInteger),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
}],
false,
false,
)
});
}
#[test]
#[should_panic]
fn env_unknown_bails() {
temp_env::with_vars(
[
("ESP_TEST_CONFIG_NUMBER", Some("0xaa")),
("ESP_TEST_CONFIG_RANDOM_VARIABLE", Some("")),
],
|| {
generate_config(
"esp-test",
&[ConfigOption {
name: String::from("number"),
description: String::from("NA"),
default_value: Value::Integer(999),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
}],
false,
false,
);
},
);
}
#[test]
#[should_panic]
fn env_invalid_values_bails() {
temp_env::with_vars([("ESP_TEST_CONFIG_NUMBER", Some("Hello world"))], || {
generate_config(
"esp-test",
&[ConfigOption {
name: String::from("number"),
description: String::from("NA"),
default_value: Value::Integer(999),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
}],
false,
false,
);
});
}
#[test]
fn env_unknown_prefix_is_ignored() {
temp_env::with_vars(
[("ESP_TEST_OTHER_CONFIG_NUMBER", Some("Hello world"))],
|| {
generate_config(
"esp-test",
&[ConfigOption {
name: String::from("number"),
description: String::from("NA"),
default_value: Value::Integer(999),
constraint: None,
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
}],
false,
false,
);
},
);
}
#[test]
fn enumeration_validator() {
let mut stdout = Vec::new();
temp_env::with_vars([("ESP_TEST_CONFIG_SOME_KEY", Some("variant-0"))], || {
generate_config_internal(
&mut stdout,
"esp-test",
&[ConfigOption {
name: String::from("some-key"),
description: String::from("NA"),
default_value: Value::String("variant-0".to_string()),
constraint: Some(Validator::Enumeration(vec![
"variant-0".to_string(),
"variant-1".to_string(),
])),
stability: Stability::Stable(String::from("testing")),
active: true,
display_hint: DisplayHint::None,
}],
false,
);
});
let cargo_lines: Vec<&str> = std::str::from_utf8(&stdout).unwrap().lines().collect();
assert!(cargo_lines.contains(&"cargo:rustc-check-cfg=cfg(some_key)"));
assert!(cargo_lines.contains(&"cargo:rustc-env=ESP_TEST_CONFIG_SOME_KEY=variant-0"));
assert!(cargo_lines.contains(&"cargo:rustc-check-cfg=cfg(some_key_variant_0)"));
assert!(cargo_lines.contains(&"cargo:rustc-check-cfg=cfg(some_key_variant_1)"));
assert!(cargo_lines.contains(&"cargo:rustc-cfg=some_key_variant_0"));
}
#[test]
#[should_panic]
fn unstable_option_panics_unless_enabled() {
let mut stdout = Vec::new();
temp_env::with_vars([("ESP_TEST_CONFIG_SOME_KEY", Some("variant-0"))], || {
generate_config_internal(
&mut stdout,
"esp-test",
&[ConfigOption {
name: String::from("some-key"),
description: String::from("NA"),
default_value: Value::String("variant-0".to_string()),
constraint: Some(Validator::Enumeration(vec![
"variant-0".to_string(),
"variant-1".to_string(),
])),
stability: Stability::Unstable,
active: true,
display_hint: DisplayHint::None,
}],
false,
);
});
}
#[test]
#[should_panic]
fn inactive_option_panics() {
let mut stdout = Vec::new();
temp_env::with_vars([("ESP_TEST_CONFIG_SOME_KEY", Some("variant-0"))], || {
generate_config_internal(
&mut stdout,
"esp-test",
&[ConfigOption {
name: String::from("some-key"),
description: String::from("NA"),
default_value: Value::String("variant-0".to_string()),
constraint: Some(Validator::Enumeration(vec![
"variant-0".to_string(),
"variant-1".to_string(),
])),
stability: Stability::Stable(String::from("testing")),
active: false,
display_hint: DisplayHint::None,
}],
false,
);
});
}
#[test]
fn deserialization() {
let yml = r#"
crate: esp-bootloader-esp-idf
options:
- name: mmu_page_size
description: ESP32-C2, ESP32-C6 and ESP32-H2 support configurable page sizes. This is currently only used to populate the app descriptor.
default:
- value: '"64k"'
stability: !Stable xxxx
constraints:
- if: true
type:
validator: enumeration
value:
- 8k
- 16k
- 32k
- 64k
- name: esp_idf_version
description: ESP-IDF version used in the application descriptor. Currently it's not checked by the bootloader.
default:
- if: 'chip == "esp32c6"'
value: '"esp32c6"'
- if: 'chip == "esp32"'
value: '"other"'
active: true
- name: partition-table-offset
description: "The address of partition table (by default 0x8000). Allows you to \
move the partition table, it gives more space for the bootloader. Note that the \
bootloader and app will both need to be compiled with the same \
PARTITION_TABLE_OFFSET value."
default:
- if: true
value: 32768
stability: Unstable
active: 'chip == "esp32c6"'
"#;
let (cfg, options) = evaluate_yaml_config(
yml,
Some(esp_metadata_generated::Chip::Esp32c6),
vec![],
false,
)
.unwrap();
assert_eq!("esp-bootloader-esp-idf", cfg.krate);
assert_eq!(
vec![
ConfigOption {
name: "mmu_page_size".to_string(),
description: "ESP32-C2, ESP32-C6 and ESP32-H2 support configurable page sizes. This is currently only used to populate the app descriptor.".to_string(),
default_value: Value::String("64k".to_string()),
constraint: Some(
Validator::Enumeration(
vec![
"8k".to_string(),
"16k".to_string(),
"32k".to_string(),
"64k".to_string(),
],
),
),
stability: Stability::Stable("xxxx".to_string()),
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: "esp_idf_version".to_string(),
description: "ESP-IDF version used in the application descriptor. Currently it's not checked by the bootloader.".to_string(),
default_value: Value::String("esp32c6".to_string()),
constraint: None,
stability: Stability::Unstable,
active: true,
display_hint: DisplayHint::None,
},
ConfigOption {
name: "partition-table-offset".to_string(),
description: "The address of partition table (by default 0x8000). Allows you to move the partition table, it gives more space for the bootloader. Note that the bootloader and app will both need to be compiled with the same PARTITION_TABLE_OFFSET value.".to_string(),
default_value: Value::Integer(32768),
constraint: None,
stability: Stability::Unstable,
active: true,
display_hint: DisplayHint::None,
},
],
options
);
}
#[test]
fn deserialization_fallback_default() {
let yml = r#"
crate: esp-bootloader-esp-idf
options:
- name: esp_idf_version
description: ESP-IDF version used in the application descriptor. Currently it's not checked by the bootloader.
default:
- if: 'chip == "esp32c6"'
value: '"esp32c6"'
- if: 'chip == "esp32"'
value: '"other"'
- value: '"default"'
active: true
"#;
let (cfg, options) = evaluate_yaml_config(
yml,
Some(esp_metadata_generated::Chip::Esp32c3),
vec![],
false,
)
.unwrap();
assert_eq!("esp-bootloader-esp-idf", cfg.krate);
assert_eq!(
vec![
ConfigOption {
name: "esp_idf_version".to_string(),
description: "ESP-IDF version used in the application descriptor. Currently it's not checked by the bootloader.".to_string(),
default_value: Value::String("default".to_string()),
constraint: None,
stability: Stability::Unstable,
active: true,
display_hint: DisplayHint::None,
},
],
options
);
}
#[test]
fn deserialization_fallback_contraint() {
let yml = r#"
crate: esp-bootloader-esp-idf
options:
- name: option
description: Desc
default:
- value: 100
constraints:
- if: 'chip == "esp32c6"'
type:
validator: integer_in_range
value:
start: 0
end: 100
- if: true
type:
validator: integer_in_range
value:
start: 0
end: 50
active: true
"#;
let (cfg, options) = evaluate_yaml_config(
yml,
Some(esp_metadata_generated::Chip::Esp32),
vec![],
false,
)
.unwrap();
assert_eq!("esp-bootloader-esp-idf", cfg.krate);
assert_eq!(
vec![ConfigOption {
name: "option".to_string(),
description: "Desc".to_string(),
default_value: Value::Integer(100),
constraint: Some(Validator::IntegerInRange(0..50)),
stability: Stability::Unstable,
active: true,
display_hint: DisplayHint::None,
},],
options
);
}
}