2021-09-05 20:03:52 +02:00
|
|
|
//! FIXME discuss about which errors to print and when to panic
|
|
|
|
|
2022-04-08 03:40:51 +02:00
|
|
|
use std::path::Path;
|
2021-08-27 11:09:27 +02:00
|
|
|
|
|
|
|
const SEPARATOR_START: &str = "# BEGIN GENERATED FEATURES\n";
|
|
|
|
const SEPARATOR_END: &str = "# END GENERATED FEATURES\n";
|
|
|
|
const HELP: &str = "# Generated by stm32-gen-features. DO NOT EDIT.\n";
|
|
|
|
|
2021-09-05 20:03:52 +02:00
|
|
|
/// Get the list of all the chips and their supported cores
|
2021-08-27 11:09:27 +02:00
|
|
|
///
|
|
|
|
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
|
|
|
|
/// [`Vec`]
|
2021-09-05 20:03:52 +02:00
|
|
|
///
|
|
|
|
/// This function is slow because all the yaml files are parsed.
|
|
|
|
pub fn chip_names_and_cores() -> Vec<(String, Vec<String>)> {
|
2022-03-20 23:15:09 +01:00
|
|
|
glob::glob("../stm32-data/data/chips/*.json")
|
2021-09-05 20:03:52 +02:00
|
|
|
.unwrap()
|
2021-08-27 11:09:27 +02:00
|
|
|
.filter_map(|entry| entry.map_err(|e| eprintln!("{:?}", e)).ok())
|
|
|
|
.filter_map(|entry| {
|
|
|
|
if let Some(name) = entry.file_stem().and_then(|stem| stem.to_str()) {
|
2021-09-05 20:03:52 +02:00
|
|
|
Some((name.to_lowercase(), chip_cores(&entry)))
|
2021-08-27 11:09:27 +02:00
|
|
|
} else {
|
2021-09-05 20:03:52 +02:00
|
|
|
eprintln!("{:?} is not a regular file", entry);
|
2021-08-27 11:09:27 +02:00
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the list of the cores of a chip by its associated file
|
|
|
|
///
|
|
|
|
/// # Panic
|
2021-09-05 20:03:52 +02:00
|
|
|
/// Panics if the file does not exist or if it contains yaml syntax errors.
|
|
|
|
/// Panics if "cores" is not an array.
|
|
|
|
fn chip_cores(path: &Path) -> Vec<String> {
|
2021-08-27 11:09:27 +02:00
|
|
|
let file_contents = std::fs::read_to_string(path).unwrap();
|
|
|
|
let doc = &yaml_rust::YamlLoader::load_from_str(&file_contents).unwrap()[0];
|
2021-09-05 20:03:52 +02:00
|
|
|
doc["cores"]
|
|
|
|
.as_vec()
|
|
|
|
.unwrap_or_else(|| panic!("{:?}:[cores] is not an array", path))
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, core)| {
|
|
|
|
core["name"]
|
|
|
|
.as_str()
|
|
|
|
.unwrap_or_else(|| panic!("{:?}:[cores][{}][name] is not a string", path, i))
|
|
|
|
.to_owned()
|
|
|
|
})
|
|
|
|
.collect()
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
2021-09-05 20:03:52 +02:00
|
|
|
/// Generate data needed in `../embassy-stm32/Cargo.toml`
|
|
|
|
///
|
|
|
|
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
|
|
|
|
/// [`Vec`]
|
2021-08-27 11:09:27 +02:00
|
|
|
///
|
|
|
|
/// # Panic
|
|
|
|
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
|
2021-09-11 20:04:57 +02:00
|
|
|
pub fn embassy_stm32_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
|
|
|
|
let mut result = String::new();
|
2022-04-08 03:40:51 +02:00
|
|
|
for (chip_name, cores) in names_and_cores {
|
2021-08-27 11:09:27 +02:00
|
|
|
if cores.len() > 1 {
|
2021-09-05 20:03:52 +02:00
|
|
|
for core_name in cores.iter() {
|
2021-09-11 20:04:57 +02:00
|
|
|
result += &format!(
|
2021-11-27 02:21:53 +01:00
|
|
|
"{chip}-{core} = [ \"stm32-metapac/{chip}-{core}\" ]\n",
|
2021-09-11 20:04:57 +02:00
|
|
|
chip = chip_name,
|
|
|
|
core = core_name
|
|
|
|
);
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-11 20:04:57 +02:00
|
|
|
result += &format!("{chip} = [ \"stm32-metapac/{chip}\" ]\n", chip = chip_name);
|
2021-09-05 20:03:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generate data needed in `../stm32-metapac/Cargo.toml`
|
|
|
|
///
|
|
|
|
/// Print errors to `stderr` when something is returned by the glob but is not in the returned
|
|
|
|
/// [`Vec`]
|
|
|
|
///
|
|
|
|
/// # Panic
|
|
|
|
/// Panics if a file contains yaml syntax errors or if a value does not have a consistent type
|
2021-09-11 20:04:57 +02:00
|
|
|
pub fn stm32_metapac_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String {
|
|
|
|
let mut result = String::new();
|
2021-09-05 20:03:52 +02:00
|
|
|
for (chip_name, cores) in names_and_cores {
|
|
|
|
if cores.len() > 1 {
|
|
|
|
for core_name in cores {
|
2021-11-23 23:49:06 +01:00
|
|
|
result += &format!("{}-{} = []\n", chip_name, core_name);
|
2021-09-05 20:03:52 +02:00
|
|
|
}
|
|
|
|
} else {
|
2021-09-11 20:04:57 +02:00
|
|
|
result += &format!("{} = []\n", chip_name);
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get contents before and after generated contents
|
|
|
|
///
|
|
|
|
/// # Panic
|
|
|
|
/// Panics when a separator cound not be not found
|
|
|
|
fn split_cargo_toml_contents(contents: &str) -> (&str, &str) {
|
|
|
|
let (before, remainder) = contents
|
|
|
|
.split_once(SEPARATOR_START)
|
|
|
|
.unwrap_or_else(|| panic!("missing \"{}\" tag", SEPARATOR_START));
|
|
|
|
let (_, after) = remainder
|
|
|
|
.split_once(SEPARATOR_END)
|
|
|
|
.unwrap_or_else(|| panic!("missing \"{}\" tag", SEPARATOR_END));
|
|
|
|
|
|
|
|
(before, after)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Generates new contents for Cargo.toml
|
|
|
|
///
|
|
|
|
/// # Panic
|
|
|
|
/// Panics when a separator cound not be not found
|
2021-09-11 20:04:57 +02:00
|
|
|
pub fn generate_cargo_toml_file(previous_text: &str, new_contents: &str) -> String {
|
2021-08-27 11:09:27 +02:00
|
|
|
let (before, after) = split_cargo_toml_contents(previous_text);
|
2021-09-11 20:04:57 +02:00
|
|
|
before.to_owned() + SEPARATOR_START + HELP + new_contents + SEPARATOR_END + after
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2021-09-05 20:03:52 +02:00
|
|
|
#[ignore]
|
2022-04-08 03:40:51 +02:00
|
|
|
fn stm32f407vg_yaml_file_exists() {
|
2021-09-05 20:03:52 +02:00
|
|
|
assert!(chip_names_and_cores()
|
|
|
|
.as_slice()
|
2021-08-27 11:09:27 +02:00
|
|
|
.into_iter()
|
2021-09-05 20:03:52 +02:00
|
|
|
.any(|(name, _)| { name == "stm32f407vg" }))
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn keeps_text_around_separators() {
|
|
|
|
let initial = "\
|
|
|
|
before
|
|
|
|
# BEGIN GENERATED FEATURES
|
|
|
|
# END GENERATED FEATURES
|
|
|
|
after
|
|
|
|
";
|
|
|
|
|
|
|
|
let expected = "\
|
|
|
|
before
|
|
|
|
# BEGIN GENERATED FEATURES
|
|
|
|
# Generated by stm32-gen-features. DO NOT EDIT.
|
|
|
|
a = [\"b\"]
|
|
|
|
# END GENERATED FEATURES
|
|
|
|
after
|
|
|
|
";
|
|
|
|
|
2021-09-11 20:04:57 +02:00
|
|
|
let new_contents = String::from("a = [\"b\"]\n");
|
|
|
|
assert_eq!(generate_cargo_toml_file(initial, &new_contents), expected);
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn does_not_generate_if_separators_are_missing() {
|
|
|
|
let initial = "\
|
|
|
|
before
|
|
|
|
# END GENERATED FEATURES
|
|
|
|
after
|
|
|
|
";
|
|
|
|
|
2021-09-11 20:04:57 +02:00
|
|
|
let new_contents = String::from("a = [\"b\"]\n");
|
|
|
|
generate_cargo_toml_file(initial, &new_contents);
|
2021-08-27 11:09:27 +02:00
|
|
|
}
|
|
|
|
}
|