src5_rs/core/parser/
cairo_trait.rs

1// Module for handling Cairo traits
2use cairo_lang_compiler::db::RootDatabase;
3use cairo_lang_syntax::node::kind::SyntaxKind;
4use cairo_lang_syntax::node::SyntaxNode;
5
6use super::cairo_function::get_functions_from_trait_body;
7use super::cairo_function::CairoNonGenericFunction;
8use super::utils::find_children;
9
10#[derive(Debug)]
11pub struct CairoNonGenericTrait {
12    pub name: String,
13    pub functions: Vec<CairoNonGenericFunction>,
14}
15
16pub fn get_non_generic_traits(
17    db: &RootDatabase,
18    syntax_tree: &SyntaxNode,
19) -> Vec<CairoNonGenericTrait> {
20    let mut no_generic_traits = Vec::new();
21    for node in syntax_tree.descendants(db) {
22        if SyntaxKind::ItemTrait == node.kind(db) {
23            // Check if has no generic types
24            if find_children(db, &node, SyntaxKind::OptionWrappedGenericParamListEmpty).is_some() {
25                // Look up the Trait name
26                let id_node = find_children(db, &node, SyntaxKind::TerminalIdentifier).unwrap();
27                let trait_name = id_node.get_text_without_trivia(db);
28
29                // Look up the Trait functions
30                let trait_body = find_children(db, &node, SyntaxKind::TraitBody).unwrap();
31                let functions = get_functions_from_trait_body(db, &trait_body);
32
33                no_generic_traits.push(CairoNonGenericTrait {
34                    name: trait_name,
35                    functions,
36                });
37            }
38        }
39    }
40    no_generic_traits
41}