ligen_python_parser/interface/
validator.rs

1use crate::prelude::*;
2use ligen::ir::{Interface, Function, Identifier};
3use ligen::parser::{ParserConfig, Validator};
4
5#[derive(Default)]
6pub struct InterfaceValidator {}
7
8impl InterfaceValidator {
9    pub fn new() -> Self {
10        Default::default()
11    }
12
13    fn validate_constructor(&self, interface: &mut Interface, _config: &ParserConfig) -> Result<()> {
14        let indices = interface.methods.iter().enumerate().filter_map(|(i, method)| {
15            if method.identifier == "__init__" {
16                Some(i)
17            } else {
18                None
19            }
20        }).collect::<Vec<_>>();
21        for index in indices {
22            let method = interface.methods.remove(index);
23            let mut function = Function::from(method);
24            function.identifier = Identifier::from("new");
25            function.inputs.remove(0);
26            function.output = Some(interface.identifier.clone().into());
27            interface.functions.push(function);
28        }
29        Ok(())
30    }
31}
32
33impl Validator for InterfaceValidator {
34    type Input = Interface;
35    fn validate(&self, interface: &mut Interface, config: &ParserConfig) -> Result<()> {
36        self.validate_constructor(interface, config)?;
37        Ok(())
38    }
39}