diff options
author | Côme ALLART <come.allart@etu.emse.fr> | 2021-09-11 20:04:57 +0200 |
---|---|---|
committer | Côme ALLART <come.allart@etu.emse.fr> | 2021-09-11 20:04:57 +0200 |
commit | 99ccf18160d9bb94071ae7e9cedb7760ab83eab6 (patch) | |
tree | 63f3716661b6e445d8b71909909e689426f004b8 /stm32-gen-features | |
parent | addee8778df6fb185a0a23658eef215ff2415d6e (diff) | |
download | embassy-99ccf18160d9bb94071ae7e9cedb7760ab83eab6.zip |
fix(gen-features): keep data files order
Diffstat (limited to 'stm32-gen-features')
-rw-r--r-- | stm32-gen-features/src/lib.rs | 47 | ||||
-rw-r--r-- | stm32-gen-features/src/main.rs | 4 |
2 files changed, 20 insertions, 31 deletions
diff --git a/stm32-gen-features/src/lib.rs b/stm32-gen-features/src/lib.rs index 683d0d4e..597d78b0 100644 --- a/stm32-gen-features/src/lib.rs +++ b/stm32-gen-features/src/lib.rs @@ -1,6 +1,6 @@ //! FIXME discuss about which errors to print and when to panic -use std::{collections::HashMap, iter::FilterMap, path::Path, slice::Iter}; +use std::{iter::FilterMap, path::Path, slice::Iter}; const SUPPORTED_FAMILIES: [&str; 8] = [ "stm32f0", @@ -89,21 +89,19 @@ fn chip_cores(path: &Path) -> Vec<String> { /// /// # Panic /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type -pub fn embassy_stm32_needed_data( - names_and_cores: &[(String, Vec<String>)], -) -> HashMap<String, Vec<String>> { - let mut result = HashMap::new(); +pub fn embassy_stm32_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String { + let mut result = String::new(); for (chip_name, cores) in names_and_cores.supported() { if cores.len() > 1 { for core_name in cores.iter() { - let key = format!("{}_{}", chip_name, core_name); - let value = vec![format!("stm32-metapac/{}_{}", chip_name, core_name)]; - result.insert(key, value); + result += &format!( + "{chip}_{core} = [ \"stm32-metapac/{chip}_{core}\" ]\n", + chip = chip_name, + core = core_name + ); } } else { - let key = chip_name.to_string(); - let value = vec![format!("stm32-metapac/{}", chip_name)]; - result.insert(key, value); + result += &format!("{chip} = [ \"stm32-metapac/{chip}\" ]\n", chip = chip_name); } } result @@ -116,18 +114,15 @@ pub fn embassy_stm32_needed_data( /// /// # Panic /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type -pub fn stm32_metapac_needed_data( - names_and_cores: &[(String, Vec<String>)], -) -> HashMap<String, Vec<String>> { - let mut result = HashMap::new(); +pub fn stm32_metapac_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String { + let mut result = String::new(); for (chip_name, cores) in names_and_cores { if cores.len() > 1 { for core_name in cores { - let key = format!("{}_{}", chip_name, core_name); - result.insert(key, vec![]); + result += &format!("{}_{} = []\n", chip_name, core_name); } } else { - result.insert(chip_name.clone(), vec![]); + result += &format!("{} = []\n", chip_name); } } result @@ -152,13 +147,9 @@ fn split_cargo_toml_contents(contents: &str) -> (&str, &str) { /// /// # Panic /// Panics when a separator cound not be not found -pub fn generate_cargo_toml_file( - previous_text: &str, - new_contents: &HashMap<String, Vec<String>>, -) -> String { +pub fn generate_cargo_toml_file(previous_text: &str, new_contents: &str) -> String { let (before, after) = split_cargo_toml_contents(previous_text); - let generated_content = toml::to_string(new_contents).unwrap(); - before.to_owned() + SEPARATOR_START + HELP + &generated_content + SEPARATOR_END + after + before.to_owned() + SEPARATOR_START + HELP + new_contents + SEPARATOR_END + after } #[cfg(test)] @@ -203,8 +194,8 @@ a = [\"b\"] after "; - let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); - assert_eq!(generate_cargo_toml_file(initial, &map), expected); + let new_contents = String::from("a = [\"b\"]\n"); + assert_eq!(generate_cargo_toml_file(initial, &new_contents), expected); } #[test] @@ -216,7 +207,7 @@ before after "; - let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); - generate_cargo_toml_file(initial, &map); + let new_contents = String::from("a = [\"b\"]\n"); + generate_cargo_toml_file(initial, &new_contents); } } diff --git a/stm32-gen-features/src/main.rs b/stm32-gen-features/src/main.rs index 9edae446..72a90752 100644 --- a/stm32-gen-features/src/main.rs +++ b/stm32-gen-features/src/main.rs @@ -1,5 +1,3 @@ -use std::collections::HashMap; - use gen_features::{ chip_names_and_cores, embassy_stm32_needed_data, generate_cargo_toml_file, stm32_metapac_needed_data, @@ -21,7 +19,7 @@ fn main() { /// /// Update the content between "# BEGIN GENERATED FEATURES" and "# END GENERATED FEATURES" /// with the given content -fn update_cargo_file(path: &str, new_contents: &HashMap<String, Vec<String>>) { +fn update_cargo_file(path: &str, new_contents: &str) { let previous_text = std::fs::read_to_string(path).unwrap(); let new_text = generate_cargo_toml_file(&previous_text, new_contents); std::fs::write(path, new_text).unwrap(); |