diff options
| author | Côme ALLART <[email protected]> | 2021-09-11 20:04:57 +0200 |
|---|---|---|
| committer | Côme ALLART <[email protected]> | 2021-09-11 20:04:57 +0200 |
| commit | 99ccf18160d9bb94071ae7e9cedb7760ab83eab6 (patch) | |
| tree | 63f3716661b6e445d8b71909909e689426f004b8 /stm32-gen-features/src | |
| parent | addee8778df6fb185a0a23658eef215ff2415d6e (diff) | |
fix(gen-features): keep data files order
Diffstat (limited to 'stm32-gen-features/src')
| -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 683d0d4ed..597d78b07 100644 --- a/stm32-gen-features/src/lib.rs +++ b/stm32-gen-features/src/lib.rs | |||
| @@ -1,6 +1,6 @@ | |||
| 1 | //! FIXME discuss about which errors to print and when to panic | 1 | //! FIXME discuss about which errors to print and when to panic |
| 2 | 2 | ||
| 3 | use std::{collections::HashMap, iter::FilterMap, path::Path, slice::Iter}; | 3 | use std::{iter::FilterMap, path::Path, slice::Iter}; |
| 4 | 4 | ||
| 5 | const SUPPORTED_FAMILIES: [&str; 8] = [ | 5 | const SUPPORTED_FAMILIES: [&str; 8] = [ |
| 6 | "stm32f0", | 6 | "stm32f0", |
| @@ -89,21 +89,19 @@ fn chip_cores(path: &Path) -> Vec<String> { | |||
| 89 | /// | 89 | /// |
| 90 | /// # Panic | 90 | /// # Panic |
| 91 | /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type | 91 | /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type |
| 92 | pub fn embassy_stm32_needed_data( | 92 | pub fn embassy_stm32_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String { |
| 93 | names_and_cores: &[(String, Vec<String>)], | 93 | let mut result = String::new(); |
| 94 | ) -> HashMap<String, Vec<String>> { | ||
| 95 | let mut result = HashMap::new(); | ||
| 96 | for (chip_name, cores) in names_and_cores.supported() { | 94 | for (chip_name, cores) in names_and_cores.supported() { |
| 97 | if cores.len() > 1 { | 95 | if cores.len() > 1 { |
| 98 | for core_name in cores.iter() { | 96 | for core_name in cores.iter() { |
| 99 | let key = format!("{}_{}", chip_name, core_name); | 97 | result += &format!( |
| 100 | let value = vec![format!("stm32-metapac/{}_{}", chip_name, core_name)]; | 98 | "{chip}_{core} = [ \"stm32-metapac/{chip}_{core}\" ]\n", |
| 101 | result.insert(key, value); | 99 | chip = chip_name, |
| 100 | core = core_name | ||
| 101 | ); | ||
| 102 | } | 102 | } |
| 103 | } else { | 103 | } else { |
| 104 | let key = chip_name.to_string(); | 104 | result += &format!("{chip} = [ \"stm32-metapac/{chip}\" ]\n", chip = chip_name); |
| 105 | let value = vec![format!("stm32-metapac/{}", chip_name)]; | ||
| 106 | result.insert(key, value); | ||
| 107 | } | 105 | } |
| 108 | } | 106 | } |
| 109 | result | 107 | result |
| @@ -116,18 +114,15 @@ pub fn embassy_stm32_needed_data( | |||
| 116 | /// | 114 | /// |
| 117 | /// # Panic | 115 | /// # Panic |
| 118 | /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type | 116 | /// Panics if a file contains yaml syntax errors or if a value does not have a consistent type |
| 119 | pub fn stm32_metapac_needed_data( | 117 | pub fn stm32_metapac_needed_data(names_and_cores: &[(String, Vec<String>)]) -> String { |
| 120 | names_and_cores: &[(String, Vec<String>)], | 118 | let mut result = String::new(); |
| 121 | ) -> HashMap<String, Vec<String>> { | ||
| 122 | let mut result = HashMap::new(); | ||
| 123 | for (chip_name, cores) in names_and_cores { | 119 | for (chip_name, cores) in names_and_cores { |
| 124 | if cores.len() > 1 { | 120 | if cores.len() > 1 { |
| 125 | for core_name in cores { | 121 | for core_name in cores { |
| 126 | let key = format!("{}_{}", chip_name, core_name); | 122 | result += &format!("{}_{} = []\n", chip_name, core_name); |
| 127 | result.insert(key, vec![]); | ||
| 128 | } | 123 | } |
| 129 | } else { | 124 | } else { |
| 130 | result.insert(chip_name.clone(), vec![]); | 125 | result += &format!("{} = []\n", chip_name); |
| 131 | } | 126 | } |
| 132 | } | 127 | } |
| 133 | result | 128 | result |
| @@ -152,13 +147,9 @@ fn split_cargo_toml_contents(contents: &str) -> (&str, &str) { | |||
| 152 | /// | 147 | /// |
| 153 | /// # Panic | 148 | /// # Panic |
| 154 | /// Panics when a separator cound not be not found | 149 | /// Panics when a separator cound not be not found |
| 155 | pub fn generate_cargo_toml_file( | 150 | pub fn generate_cargo_toml_file(previous_text: &str, new_contents: &str) -> String { |
| 156 | previous_text: &str, | ||
| 157 | new_contents: &HashMap<String, Vec<String>>, | ||
| 158 | ) -> String { | ||
| 159 | let (before, after) = split_cargo_toml_contents(previous_text); | 151 | let (before, after) = split_cargo_toml_contents(previous_text); |
| 160 | let generated_content = toml::to_string(new_contents).unwrap(); | 152 | before.to_owned() + SEPARATOR_START + HELP + new_contents + SEPARATOR_END + after |
| 161 | before.to_owned() + SEPARATOR_START + HELP + &generated_content + SEPARATOR_END + after | ||
| 162 | } | 153 | } |
| 163 | 154 | ||
| 164 | #[cfg(test)] | 155 | #[cfg(test)] |
| @@ -203,8 +194,8 @@ a = [\"b\"] | |||
| 203 | after | 194 | after |
| 204 | "; | 195 | "; |
| 205 | 196 | ||
| 206 | let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); | 197 | let new_contents = String::from("a = [\"b\"]\n"); |
| 207 | assert_eq!(generate_cargo_toml_file(initial, &map), expected); | 198 | assert_eq!(generate_cargo_toml_file(initial, &new_contents), expected); |
| 208 | } | 199 | } |
| 209 | 200 | ||
| 210 | #[test] | 201 | #[test] |
| @@ -216,7 +207,7 @@ before | |||
| 216 | after | 207 | after |
| 217 | "; | 208 | "; |
| 218 | 209 | ||
| 219 | let map = HashMap::from([(String::from("a"), vec![String::from("b")])]); | 210 | let new_contents = String::from("a = [\"b\"]\n"); |
| 220 | generate_cargo_toml_file(initial, &map); | 211 | generate_cargo_toml_file(initial, &new_contents); |
| 221 | } | 212 | } |
| 222 | } | 213 | } |
diff --git a/stm32-gen-features/src/main.rs b/stm32-gen-features/src/main.rs index 9edae4463..72a907522 100644 --- a/stm32-gen-features/src/main.rs +++ b/stm32-gen-features/src/main.rs | |||
| @@ -1,5 +1,3 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | |||
| 3 | use gen_features::{ | 1 | use gen_features::{ |
| 4 | chip_names_and_cores, embassy_stm32_needed_data, generate_cargo_toml_file, | 2 | chip_names_and_cores, embassy_stm32_needed_data, generate_cargo_toml_file, |
| 5 | stm32_metapac_needed_data, | 3 | stm32_metapac_needed_data, |
| @@ -21,7 +19,7 @@ fn main() { | |||
| 21 | /// | 19 | /// |
| 22 | /// Update the content between "# BEGIN GENERATED FEATURES" and "# END GENERATED FEATURES" | 20 | /// Update the content between "# BEGIN GENERATED FEATURES" and "# END GENERATED FEATURES" |
| 23 | /// with the given content | 21 | /// with the given content |
| 24 | fn update_cargo_file(path: &str, new_contents: &HashMap<String, Vec<String>>) { | 22 | fn update_cargo_file(path: &str, new_contents: &str) { |
| 25 | let previous_text = std::fs::read_to_string(path).unwrap(); | 23 | let previous_text = std::fs::read_to_string(path).unwrap(); |
| 26 | let new_text = generate_cargo_toml_file(&previous_text, new_contents); | 24 | let new_text = generate_cargo_toml_file(&previous_text, new_contents); |
| 27 | std::fs::write(path, new_text).unwrap(); | 25 | std::fs::write(path, new_text).unwrap(); |
