Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enumeration processing: HL-AST stage #10

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/api_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ pub struct APIMap {
pub structs: Vec<Struct>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct Typedef {
pub identifier: String,
pub kind: String,
}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct Enum {
pub identifier: Option<String>,
Expand Down
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +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)?;
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);
Expand Down
85 changes: 85 additions & 0 deletions src/process/enumeration.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
use super::{Enumeration, EnumerationMember};
use log::warn;

use crate::api_map::APIMap;

/// 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
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 {
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 {
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<Enumeration> {
let enumerations: Vec<Enumeration> = api_map
.enums
.clone()
.into_iter()
.map(|enumeration| Enumeration {
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: convert_casing(&if enumeration.identifier.is_some() {
remove_common_string(
&member.identifier,
&enumeration.identifier.clone().unwrap(),
)
} else {
member.identifier
}),
value: member.value.clone(),
})
.collect(),
})
.collect();

enumerations
}
22 changes: 18 additions & 4 deletions src/process/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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 enumeration;
mod func;
mod namespace;

Expand Down Expand Up @@ -32,10 +32,24 @@ pub struct Argument {
pub kind: String,
}

#[derive(Debug, Clone)]
pub struct EnumerationMember {
pub identifier: String,
pub value: Option<String>,
}

#[derive(Debug, Clone)]
pub struct Enumeration {
pub identifier: Option<String>,
pub members: Vec<EnumerationMember>,
}

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);
AlixANNERAUD marked this conversation as resolved.
Show resolved Hide resolved
debug!("Namespaces: {namespaces:#?}");
let enumerations = enumeration::enumeration_processor(&api_map);
debug!("Enumerations: {enumerations:#?}");
}