pipeline_script/plugin/
method.rs

1use crate::ast::NodeTrait;
2use crate::core::engine::Engine;
3use crate::plugin::Plugin;
4use crate::postprocessor::{Stage, Visitor};
5
6pub struct MethodPlugin;
7struct MethodVisitor;
8impl Visitor for MethodVisitor {
9    fn stage(&self) -> Stage {
10        Stage::AfterTypeInfer
11    }
12    fn match_id(&self, id: &str) -> bool {
13        id == "Expr:FnCall" || id == "Function"
14    }
15    fn visit(&self, node: &mut (impl NodeTrait + ?Sized)) -> crate::postprocessor::VisitResult {
16        if node.get_id().starts_with("Function") {
17            let data = node.get_data("binding_struct");
18            if data.is_none() {
19                return crate::postprocessor::VisitResult::Continue;
20            }
21            let data = data.unwrap();
22            let binding = data.as_str();
23            return match binding {
24                None => crate::postprocessor::VisitResult::Continue,
25                Some(binding) => {
26                    if binding.is_empty() {
27                        return crate::postprocessor::VisitResult::Continue;
28                    }
29                    node.set_data(
30                        "name",
31                        format!(
32                            "{binding}.{}",
33                            node.get_data("name").unwrap().as_str().unwrap()
34                        )
35                        .into(),
36                    );
37                    crate::postprocessor::VisitResult::Continue
38                }
39            };
40        }
41        let is_method = node.get_data("is_method").unwrap().as_bool();
42        match is_method {
43            None => crate::postprocessor::VisitResult::Continue,
44            Some(b) => {
45                if b {
46                    let data = node.get_data("binding_struct").unwrap();
47                    let name = data.as_str().unwrap();
48                    let children = node.get_children();
49                    let first_child = children.first().unwrap();
50                    let data = first_child.get_data("type").unwrap();
51                    let ty = data.as_type().unwrap();
52                    let struct_name = ty.get_struct_name().unwrap();
53                    node.set_data("name", format!("{struct_name}.{name}").into());
54                }
55                crate::postprocessor::VisitResult::Continue
56            }
57        }
58    }
59}
60impl Plugin for MethodPlugin {
61    fn apply(self, e: &mut Engine) {
62        e.register_visitor(MethodVisitor)
63    }
64}