Skip to main content

graphics_bindgen/
function.rs

1use quote::format_ident;
2use syn::{FnArg, Ident, Pat};
3
4pub fn is_public_input(input: &&FnArg) -> bool {
5    // If input ident start with two underscores, consider it private
6    if let FnArg::Typed(pat_type) = input
7        && let Pat::Ident(pat_ident) = &*pat_type.pat
8    {
9        return !pat_ident.ident.to_string().starts_with("__");
10    }
11    true
12}
13
14pub fn split_inputs<'a>(
15    inputs: &'a [&'a FnArg],
16) -> Result<(&'a [&'a FnArg], &'a [&'a FnArg]), String> {
17    let index = inputs.iter().position(is_public_input).unwrap_or(0);
18
19    Ok(inputs.split_at(index))
20}
21
22const NAMES: [(&str, &str); 1] = [("obj", "object")];
23
24pub fn get_function_identifier(prefix: &str, identifier: &Ident) -> Ident {
25    let identifier = identifier.to_string();
26    let identifier = identifier.strip_prefix("lv_").unwrap_or(&identifier);
27
28    // - Replace names
29    let identifier = identifier
30        .split("_")
31        .map(|part| match NAMES.iter().find(|(old, _)| *old == part) {
32            Some((_, new)) => new.to_string(),
33            None => part.to_string(),
34        })
35        .collect::<Vec<String>>()
36        .join("_");
37
38    format_ident!("{}{}", prefix, identifier)
39}