use bindgen::callbacks::{Token, TokenKind};
use std::collections::HashSet;
#[derive(Debug)]
pub struct DeriveCastedConstant {
targets: HashSet<String>,
}
impl DeriveCastedConstant {
pub fn new() -> Self {
DeriveCastedConstant {
targets: HashSet::new(),
}
}
pub fn target(mut self, name: &str) -> Self {
self.targets.insert(name.to_string());
self
}
}
impl bindgen::callbacks::ParseCallbacks for DeriveCastedConstant {
fn modify_macro(&self, _name: &str, _tokens: &mut Vec<Token>) {
if self.targets.contains(_name) {
let position_cast = _tokens.windows(3).position(|window| match window {
[Token {
kind: TokenKind::Punctuation,
raw: left_parenthesis,
}, Token {
kind: TokenKind::Keyword,
..
}, Token {
kind: TokenKind::Punctuation,
raw: right_parenthesis,
}] => **left_parenthesis == *b"(" && **right_parenthesis == *b")",
_ => false,
});
if let Some(pos) = position_cast {
*_tokens = [&_tokens[..pos], &_tokens[pos + 3..]].concat();
}
}
}
}