From 8d5fff395f0ad0a2e335bd3c2684f76d188496d2 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 12:43:45 +0100 Subject: [PATCH 01/17] More explicit error for missing map file --- src/main.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index c28f5d9..01dc749 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod template; use anyhow::{Context, Result}; use clap::Parser; -use log::{debug, info}; +use log::{debug, error, info}; use simplelog::{ColorChoice, TermLogger, TerminalMode}; use std::{fs, path::PathBuf}; @@ -31,7 +31,13 @@ fn main() -> Result<()> { info!("Retrieving all functions..."); let api_map_file_path = config.input.cwd.unwrap_or(PathBuf::new()).join("lvgl.json"); - let api_map_file_content = fs::read_to_string(api_map_file_path)?; + let api_map_file_content = match fs::read_to_string(api_map_file_path.clone()) { + Ok(content) => content, + Err(error) => { + error!("Failed to read the API map file (lvgl.json) at {} : {}", api_map_file_path.display(), error); + return Err(error.into()); + } + }; let api_map = api_map::parse(&api_map_file_content)?; // debug!("Parsed API map: {:#?}", api_map); From 6a6cb37540d767d4598227374c42f66815c2d461 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 12:57:16 +0100 Subject: [PATCH 02/17] Creation of the general structure of enumeration processing --- src/process/enumeration.rs | 22 ++++++++++++++++++++++ src/process/mod.rs | 10 ++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/process/enumeration.rs diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs new file mode 100644 index 0000000..f0b6627 --- /dev/null +++ b/src/process/enumeration.rs @@ -0,0 +1,22 @@ + + +use crate::api_map::APIMap; + +#[derive(Debug, Clone)] +pub struct EnumerationMember { + pub identifier: String, + pub value: String, +} + +#[derive(Debug, Clone)] +pub struct Enumeration { + pub identifier: Option, + pub members: Vec, +} + +pub fn enumeration_processor(api_map: &APIMap) -> Vec +{ + let mut enumerations: Vec = vec![]; + + enumerations +} \ No newline at end of file diff --git a/src/process/mod.rs b/src/process/mod.rs index 0308c6e..22f2e9a 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -1,10 +1,10 @@ -use self::func::function_processor; -use crate::{api_map::APIMap, conf, process::namespace::namespace_generator}; +use crate::{api_map::APIMap, conf}; use log::debug; mod class; mod func; mod namespace; +mod enumeration; #[derive(Debug, Clone)] pub struct Namespace { @@ -34,8 +34,10 @@ pub struct Argument { pub fn make_hl_ast(api_map: APIMap, conf: &conf::Generation) { debug!("Generation config: {:#?}", conf); - let functions = function_processor(&api_map, &conf.functions); + let functions = func::function_processor(&api_map, &conf.functions); debug!("Functions: {functions:#?}"); - let namespaces = namespace_generator(&functions, &conf.namespaces); + let namespaces = namespace::namespace_generator(&functions, &conf.namespaces); debug!("Namespaces: {namespaces:#?}"); + let enumerations = enumeration::enumeration_processor(&api_map); + debug!("Enumerations: {enumerations:#?}"); } From 3843e0118bab43e7c9c1ed409dd026a1d6e8ed2a Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 13:33:48 +0100 Subject: [PATCH 03/17] Write basic processing --- src/process/enumeration.rs | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index f0b6627..7d058b6 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -1,22 +1,39 @@ - +use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; use crate::api_map::APIMap; #[derive(Debug, Clone)] pub struct EnumerationMember { pub identifier: String, - pub value: String, + pub value: Option, } #[derive(Debug, Clone)] pub struct Enumeration { - pub identifier: Option, + pub identifier: String, + pub r_type: String, pub members: Vec, } -pub fn enumeration_processor(api_map: &APIMap) -> Vec -{ - let mut enumerations: Vec = vec![]; +pub fn enumeration_processor(api_map: &APIMap) -> Vec { + let enumerations: Vec = api_map + .enums + .clone() + .par_iter() + .map(|enumeration| Enumeration { + identifier: enumeration.identifier.clone(), + r_type: enumeration.r#type.clone(), + members: enumeration + .members + .clone() + .into_iter() + .map(|member| EnumerationMember { + identifier: member.0, + value: member.1, + }) + .collect(), + }) + .collect(); enumerations -} \ No newline at end of file +} From 0ee8402b4fcd3a25dba7b775ad3180767cc0ed70 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 13:54:19 +0100 Subject: [PATCH 04/17] Add clone for member identifier and add option for enumeration identifier --- src/process/enumeration.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 7d058b6..e0025e3 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -10,7 +10,7 @@ pub struct EnumerationMember { #[derive(Debug, Clone)] pub struct Enumeration { - pub identifier: String, + pub identifier: Option, pub r_type: String, pub members: Vec, } @@ -21,15 +21,19 @@ pub fn enumeration_processor(api_map: &APIMap) -> Vec { .clone() .par_iter() .map(|enumeration| Enumeration { - identifier: enumeration.identifier.clone(), + identifier: if enumeration.identifier == "anonymous" { + None + } else { + Some(enumeration.identifier.clone()) + }, r_type: enumeration.r#type.clone(), members: enumeration .members .clone() .into_iter() .map(|member| EnumerationMember { - identifier: member.0, - value: member.1, + identifier: member.0.clone(), + value: member.1.clone() }) .collect(), }) From d68cedb7e6daba779f011c624f7e75b0fe3feab2 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 14:44:45 +0100 Subject: [PATCH 05/17] Add typedef processing in APIMap --- src/api_map.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/api_map.rs b/src/api_map.rs index d5d286b..5992da1 100644 --- a/src/api_map.rs +++ b/src/api_map.rs @@ -68,6 +68,13 @@ pub struct APIMap { pub enums: Vec, pub functions: Vec, pub structs: Vec, + pub typedefs: Vec, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Typedef { + pub identifier: String, + pub r#type: String, } #[derive(Debug, Clone, PartialEq)] @@ -112,6 +119,7 @@ pub fn parse(source_str: &str) -> Result { enums: json.process_enums(), functions: json.process_functions(), structs: json.process_structs(), + typedefs: json.process_typedefs(), }) } @@ -191,4 +199,17 @@ impl JSONRoot { }) .collect() } + + fn process_typedefs(&self) -> Vec { + self.typedefs + .clone() + .into_iter() + .map(|typedef| { + Typedef { + identifier: typedef.name.unwrap(), + r#type: typedef.r#type.unwrap().parse_as_type(), + } + }) + .collect() + } } From 5b07031a2e8e4045d2659d67f5539fce1fcc7a62 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Wed, 24 Jan 2024 20:17:27 +0100 Subject: [PATCH 06/17] Work on enumeration identifier --- src/process/enumeration.rs | 107 ++++++++++++++++++++++++++++++++++--- 1 file changed, 99 insertions(+), 8 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index e0025e3..3cc2c18 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -1,6 +1,7 @@ -use rayon::iter::{IntoParallelRefIterator, ParallelIterator}; +use log::warn; +use rayon::prelude::*; -use crate::api_map::APIMap; +use crate::api_map::{APIMap, Enum}; #[derive(Debug, Clone)] pub struct EnumerationMember { @@ -15,17 +16,107 @@ pub struct Enumeration { pub members: Vec, } +fn get_common_string(Strings: Vec) -> String { + let mut common_string = String::new(); + + let minimum_length = Strings.iter().map(|s| s.len()).min().unwrap(); + + for i in 0..minimum_length { + for string in &Strings { + if string.chars().nth(i).unwrap() != Strings[0].chars().nth(i).unwrap() { + return common_string; + } else { + common_string.push(string.chars().nth(i).unwrap()); + } + } + } + + common_string +} + +fn get_score_match(s1 : &String, s2 : &String) -> usize { + let mut score = 0; + let minimum_length = s1.len().min(s2.len()); + + // Insensitive to case + let s1 = s1.to_lowercase(); + let s2 = s2.to_lowercase(); + + for i in 0..minimum_length { + if s1.chars().nth(i).unwrap() == s2.chars().nth(i).unwrap() { + score += 1; + } + } + score +} + +fn get_enum_identifier(api_map: &APIMap, enumeration: &Enum) -> String { + if enumeration.identifier != "anonymous" { + return enumeration.identifier.clone(); + } + + let mut common_string = + get_common_string(enumeration.members.iter().map(|m| m.0.clone()).collect()); + + if common_string == "" { + warn!( + "Could not find common string for enum members: {:#?}", + enumeration.members + ); + return "anonymous".to_string(); + } + + // Looking for best match + + let mut best_match = String::new(); + let mut best_match_score = 0; + + for typedef in &api_map.typedefs { + let score = get_score_match(&common_string, &typedef.identifier); + if score > best_match_score { + best_match = typedef.identifier.clone(); + best_match_score = score; + } + } + + if best_match_score > 0 { + return best_match; + } + + + warn!( + "Could not find typedef match for enum members: {:#?}", + enumeration.members + ); + "anonymous".to_string() +} + +/// Convert a snake_case or SCREAMING_SNAKE_CASE string to PascalCase +fn convert_casing(input: &String) -> String { + let mut result = String::new(); + let mut capitalize_next = true; + + for c in input.chars() { + if c == '_' { + capitalize_next = true; + } else if capitalize_next { + result.push_str(&c.to_uppercase().to_string()); + capitalize_next = false; + } else { + result.push(c); // ? : Maybe this should be c.to_lowercase() + } + } + + result +} + pub fn enumeration_processor(api_map: &APIMap) -> Vec { let enumerations: Vec = api_map .enums .clone() .par_iter() .map(|enumeration| Enumeration { - identifier: if enumeration.identifier == "anonymous" { - None - } else { - Some(enumeration.identifier.clone()) - }, + identifier: Some(get_enum_identifier(api_map, enumeration)), r_type: enumeration.r#type.clone(), members: enumeration .members @@ -33,7 +124,7 @@ pub fn enumeration_processor(api_map: &APIMap) -> Vec { .into_iter() .map(|member| EnumerationMember { identifier: member.0.clone(), - value: member.1.clone() + value: member.1.clone(), }) .collect(), }) From bfec47a12c20705534ab4d2cf0d983bc88763eec Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Thu, 25 Jan 2024 00:23:50 +0100 Subject: [PATCH 07/17] Fix panic --- src/api_map.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/api_map.rs b/src/api_map.rs index 5992da1..b5c88a1 100644 --- a/src/api_map.rs +++ b/src/api_map.rs @@ -74,7 +74,7 @@ pub struct APIMap { #[derive(Debug, Clone, PartialEq)] pub struct Typedef { pub identifier: String, - pub r#type: String, + pub kind: String, } #[derive(Debug, Clone, PartialEq)] @@ -205,10 +205,14 @@ impl JSONRoot { .clone() .into_iter() .map(|typedef| { - Typedef { - identifier: typedef.name.unwrap(), - r#type: typedef.r#type.unwrap().parse_as_type(), - } + let t = Typedef { + identifier: typedef.name.clone().unwrap(), + kind: typedef.parse_as_type(), + }; + + println!("{} -> {}", typedef.parse_as_type(), typedef.name.clone().unwrap()); + + t }) .collect() } From 6a371bf325e822f21b5b275738a0909307966244 Mon Sep 17 00:00:00 2001 From: GGorAA Date: Sat, 27 Jan 2024 15:42:32 +0100 Subject: [PATCH 08/17] feat: remove typedefs from resulting API map --- src/api_map.rs | 19 ------------------- 1 file changed, 19 deletions(-) diff --git a/src/api_map.rs b/src/api_map.rs index 00b4c43..017d790 100644 --- a/src/api_map.rs +++ b/src/api_map.rs @@ -71,7 +71,6 @@ pub struct APIMap { pub enums: Vec, pub functions: Vec, pub structs: Vec, - pub typedefs: Vec, } #[derive(Debug, Clone, PartialEq)] @@ -130,7 +129,6 @@ pub fn parse(source_str: &str) -> Result { enums: json.process_enums(), functions: json.process_functions(), structs: json.process_structs(), - typedefs: json.process_typedefs(), }) } @@ -266,23 +264,6 @@ impl JSONRoot { }) .collect() } - - fn process_typedefs(&self) -> Vec { - self.typedefs - .clone() - .into_iter() - .map(|typedef| { - let t = Typedef { - identifier: typedef.name.clone().unwrap(), - kind: typedef.parse_as_type(), - }; - - println!("{} -> {}", typedef.parse_as_type(), typedef.name.clone().unwrap()); - - t - }) - .collect() - } } // TODO: Document From 9994325cf841fbf24c37ea50193a3953b871117a Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Sun, 28 Jan 2024 16:07:00 +0100 Subject: [PATCH 09/17] Updated version with new typedef flattening --- src/process/enumeration.rs | 94 +++++--------------------------------- 1 file changed, 12 insertions(+), 82 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 3cc2c18..d98c7e3 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -12,88 +12,13 @@ pub struct EnumerationMember { #[derive(Debug, Clone)] pub struct Enumeration { pub identifier: Option, - pub r_type: String, pub members: Vec, } -fn get_common_string(Strings: Vec) -> String { - let mut common_string = String::new(); - - let minimum_length = Strings.iter().map(|s| s.len()).min().unwrap(); - - for i in 0..minimum_length { - for string in &Strings { - if string.chars().nth(i).unwrap() != Strings[0].chars().nth(i).unwrap() { - return common_string; - } else { - common_string.push(string.chars().nth(i).unwrap()); - } - } - } - - common_string -} - -fn get_score_match(s1 : &String, s2 : &String) -> usize { - let mut score = 0; - let minimum_length = s1.len().min(s2.len()); - - // Insensitive to case - let s1 = s1.to_lowercase(); - let s2 = s2.to_lowercase(); - - for i in 0..minimum_length { - if s1.chars().nth(i).unwrap() == s2.chars().nth(i).unwrap() { - score += 1; - } - } - score -} - -fn get_enum_identifier(api_map: &APIMap, enumeration: &Enum) -> String { - if enumeration.identifier != "anonymous" { - return enumeration.identifier.clone(); - } - - let mut common_string = - get_common_string(enumeration.members.iter().map(|m| m.0.clone()).collect()); - - if common_string == "" { - warn!( - "Could not find common string for enum members: {:#?}", - enumeration.members - ); - return "anonymous".to_string(); - } - - // Looking for best match - - let mut best_match = String::new(); - let mut best_match_score = 0; - - for typedef in &api_map.typedefs { - let score = get_score_match(&common_string, &typedef.identifier); - if score > best_match_score { - best_match = typedef.identifier.clone(); - best_match_score = score; - } - } - - if best_match_score > 0 { - return best_match; - } - - - warn!( - "Could not find typedef match for enum members: {:#?}", - enumeration.members - ); - "anonymous".to_string() -} - -/// Convert a snake_case or SCREAMING_SNAKE_CASE string to PascalCase +/// Convert a snake_case or SCREAMING_SNALKE_CASE string to PascalCase fn convert_casing(input: &String) -> String { let mut result = String::new(); + result.reserve(input.len()); let mut capitalize_next = true; for c in input.chars() { @@ -110,25 +35,30 @@ fn convert_casing(input: &String) -> String { result } -pub fn enumeration_processor(api_map: &APIMap) -> Vec { +pub fn enumeration_processor(api_map: &APIMap) -> Vec { let enumerations: Vec = api_map .enums .clone() .par_iter() .map(|enumeration| Enumeration { - identifier: Some(get_enum_identifier(api_map, enumeration)), - r_type: enumeration.r#type.clone(), + identifier: if enumeration.identifier.is_some() { + Some(convert_casing(&enumeration.identifier.clone().unwrap())) + } else { + warn!("Enumeration identifier is None"); + None + }, members: enumeration .members .clone() .into_iter() .map(|member| EnumerationMember { - identifier: member.0.clone(), - value: member.1.clone(), + identifier: member.identifier.clone(), + value: member.value.clone(), }) .collect(), }) .collect(); + println!("enumerations: {:#?}", enumerations); enumerations } From 749a8859e54a9a0e01e5fe451cedd861280b77e9 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Sun, 28 Jan 2024 19:57:47 +0100 Subject: [PATCH 10/17] Remove common string for each member --- src/process/enumeration.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index d98c7e3..4349610 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -35,7 +35,32 @@ fn convert_casing(input: &String) -> String { result } -pub fn enumeration_processor(api_map: &APIMap) -> Vec { +pub fn remove_common_string(input: &String, identifier: &String) -> String { + println!("{} : {}", input, identifier); + let mut input = input.to_lowercase(); + + if input.starts_with("_") { + input = input.replacen("_", "", 1); + } + + let mut identifier = identifier.to_lowercase(); + + if identifier.starts_with("_") { + identifier = identifier.replacen("_", "", 1); + } + + let mut input_iter = input.chars().peekable(); + let mut identifier_iter = identifier.chars(); + + while input_iter.peek() == identifier_iter.next().as_ref() { + input_iter.next(); + + } + + input_iter.collect() +} + +pub fn enumeration_processor(api_map: &APIMap) -> Vec { let enumerations: Vec = api_map .enums .clone() @@ -52,7 +77,14 @@ pub fn enumeration_processor(api_map: &APIMap) -> Vec { .clone() .into_iter() .map(|member| EnumerationMember { - identifier: member.identifier.clone(), + identifier: convert_casing(&if enumeration.identifier.is_some() { + remove_common_string( + &member.identifier, + &enumeration.identifier.clone().unwrap(), + ) + } else { + member.identifier + }), value: member.value.clone(), }) .collect(), From 5376045603e9bba9f0c6e56e429864403388d364 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:16:22 +0100 Subject: [PATCH 11/17] Use anyhow::context instead of error macro --- src/main.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7dd6553..2fc23dc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,13 +31,10 @@ fn main() -> Result<()> { info!("Retrieving all functions..."); let api_map_file_path = config.input.cwd.unwrap_or(PathBuf::new()).join("lvgl.json"); - let api_map_file_content = match fs::read_to_string(api_map_file_path.clone()) { - Ok(content) => content, - Err(error) => { - error!("Failed to read the API map file (lvgl.json) at {} : {}", api_map_file_path.display(), error); - return Err(error.into()); - } - }; + let api_map_file_content = fs::read_to_string(api_map_file_path.clone()).context(format!( + "Failed to read the API map file at {}", + api_map_file_path.display() + ))?; let api_map = api_map::parse(&api_map_file_content)?; debug!("Parsed&processed API map: {:#?}", api_map); From b0400c23aca313ad698f24eb0571fa5518ee2142 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:19:21 +0100 Subject: [PATCH 12/17] Move enumeration structure into mod --- src/process/enumeration.rs | 13 +------------ src/process/mod.rs | 14 +++++++++++++- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 4349610..f7a5e26 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -1,20 +1,9 @@ use log::warn; use rayon::prelude::*; +use super::{Enumeration, EnumerationMember}; use crate::api_map::{APIMap, Enum}; -#[derive(Debug, Clone)] -pub struct EnumerationMember { - pub identifier: String, - pub value: Option, -} - -#[derive(Debug, Clone)] -pub struct Enumeration { - pub identifier: Option, - pub members: Vec, -} - /// Convert a snake_case or SCREAMING_SNALKE_CASE string to PascalCase fn convert_casing(input: &String) -> String { let mut result = String::new(); diff --git a/src/process/mod.rs b/src/process/mod.rs index 22f2e9a..dde91e1 100644 --- a/src/process/mod.rs +++ b/src/process/mod.rs @@ -2,9 +2,9 @@ use crate::{api_map::APIMap, conf}; use log::debug; mod class; +mod enumeration; mod func; mod namespace; -mod enumeration; #[derive(Debug, Clone)] pub struct Namespace { @@ -32,6 +32,18 @@ pub struct Argument { pub kind: String, } +#[derive(Debug, Clone)] +pub struct EnumerationMember { + pub identifier: String, + pub value: Option, +} + +#[derive(Debug, Clone)] +pub struct Enumeration { + pub identifier: Option, + pub members: Vec, +} + pub fn make_hl_ast(api_map: APIMap, conf: &conf::Generation) { debug!("Generation config: {:#?}", conf); let functions = func::function_processor(&api_map, &conf.functions); From 048eaf067287fdc8e197a31a460a2ab0a0e99fb8 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:20:02 +0100 Subject: [PATCH 13/17] Remove unnecessary use --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 2fc23dc..b4611e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ mod template; use anyhow::{Context, Result}; use clap::Parser; -use log::{debug, error, info}; +use log::{debug, info}; use simplelog::{ColorChoice, TermLogger, TerminalMode}; use std::{fs, path::PathBuf}; From 6f197042e8d4f8047d1719078eb8ec2b22be28d8 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:27:38 +0100 Subject: [PATCH 14/17] Use into_iter instead of par_iter --- src/process/enumeration.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index f7a5e26..9172191 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -53,7 +53,7 @@ pub fn enumeration_processor(api_map: &APIMap) -> Vec { let enumerations: Vec = api_map .enums .clone() - .par_iter() + .into_iter() .map(|enumeration| Enumeration { identifier: if enumeration.identifier.is_some() { Some(convert_casing(&enumeration.identifier.clone().unwrap())) From 9f9d34f5834e5f498f46ba07e73fc99045209ac0 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:39:56 +0100 Subject: [PATCH 15/17] Fix SCREAMING_SNAKE_CASE casing conversion --- src/process/enumeration.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 9172191..3e313ac 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -4,10 +4,10 @@ use super::{Enumeration, EnumerationMember}; use crate::api_map::{APIMap, Enum}; -/// Convert a snake_case or SCREAMING_SNALKE_CASE string to PascalCase +/// Convert a snake_case or SCREAMING_SNAKE_CASE string to PascalCase fn convert_casing(input: &String) -> String { let mut result = String::new(); - result.reserve(input.len()); + result.reserve(input.len()); // Pre-allocate memory to avoid re-allocations let mut capitalize_next = true; for c in input.chars() { @@ -17,15 +17,18 @@ fn convert_casing(input: &String) -> String { result.push_str(&c.to_uppercase().to_string()); capitalize_next = false; } else { - result.push(c); // ? : Maybe this should be c.to_lowercase() + match c.to_lowercase().next() { + Some(c) => result.push(c), + None => result.push(c) + } } } result } +/// Remove the common string from the beginning of the identifier pub fn remove_common_string(input: &String, identifier: &String) -> String { - println!("{} : {}", input, identifier); let mut input = input.to_lowercase(); if input.starts_with("_") { @@ -80,6 +83,5 @@ pub fn enumeration_processor(api_map: &APIMap) -> Vec { }) .collect(); - println!("enumerations: {:#?}", enumerations); enumerations } From a94ec7603c319fc669d87f21bd985aff51fc40ae Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:45:32 +0100 Subject: [PATCH 16/17] A little bit of formatting --- src/main.rs | 9 +++++---- src/process/enumeration.rs | 11 +++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index b4611e3..fc60d8a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -31,10 +31,11 @@ fn main() -> Result<()> { info!("Retrieving all functions..."); let api_map_file_path = config.input.cwd.unwrap_or(PathBuf::new()).join("lvgl.json"); - let api_map_file_content = fs::read_to_string(api_map_file_path.clone()).context(format!( - "Failed to read the API map file at {}", - api_map_file_path.display() - ))?; + let api_map_file_content = + fs::read_to_string(api_map_file_path.clone()).context(format!( + "Failed to read the API map file at {}", + api_map_file_path.display() + ))?; let api_map = api_map::parse(&api_map_file_content)?; debug!("Parsed&processed API map: {:#?}", api_map); diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 3e313ac..593780d 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -1,13 +1,13 @@ +use super::{Enumeration, EnumerationMember}; use log::warn; use rayon::prelude::*; -use super::{Enumeration, EnumerationMember}; use crate::api_map::{APIMap, Enum}; /// Convert a snake_case or SCREAMING_SNAKE_CASE string to PascalCase fn convert_casing(input: &String) -> String { let mut result = String::new(); - result.reserve(input.len()); // Pre-allocate memory to avoid re-allocations + result.reserve(input.len()); // Pre-allocate memory to avoid re-allocations let mut capitalize_next = true; for c in input.chars() { @@ -19,7 +19,7 @@ fn convert_casing(input: &String) -> String { } else { match c.to_lowercase().next() { Some(c) => result.push(c), - None => result.push(c) + None => result.push(c), } } } @@ -30,7 +30,7 @@ fn convert_casing(input: &String) -> String { /// Remove the common string from the beginning of the identifier pub fn remove_common_string(input: &String, identifier: &String) -> String { let mut input = input.to_lowercase(); - + if input.starts_with("_") { input = input.replacen("_", "", 1); } @@ -43,10 +43,9 @@ pub fn remove_common_string(input: &String, identifier: &String) -> String { let mut input_iter = input.chars().peekable(); let mut identifier_iter = identifier.chars(); - + while input_iter.peek() == identifier_iter.next().as_ref() { input_iter.next(); - } input_iter.collect() From b199ecfe5e7131878c36b58fa815d69cbcd25f74 Mon Sep 17 00:00:00 2001 From: Alix ANNERAUD Date: Mon, 29 Jan 2024 10:54:27 +0100 Subject: [PATCH 17/17] Remove unused use --- src/process/enumeration.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/process/enumeration.rs b/src/process/enumeration.rs index 593780d..8237c8e 100644 --- a/src/process/enumeration.rs +++ b/src/process/enumeration.rs @@ -1,8 +1,7 @@ use super::{Enumeration, EnumerationMember}; use log::warn; -use rayon::prelude::*; -use crate::api_map::{APIMap, Enum}; +use crate::api_map::APIMap; /// Convert a snake_case or SCREAMING_SNAKE_CASE string to PascalCase fn convert_casing(input: &String) -> String {