Skip to main content

codetether_rlm/oracle/tree_sitter_oracle/
errors.rs

1use anyhow::Result;
2
3use super::{ErrorPatternCounts, TreeSitterOracle};
4
5const RESULT_QUERY: &str = r#"(generic_type type: (type_identifier) @name (#eq? @name "Result"))"#;
6const TRY_QUERY: &str = r#"(try_expression)"#;
7const MATCH_QUERY: &str = r#"(match_expression)"#;
8const UNWRAP_QUERY: &str = r#"(call_expression function: (field_expression field: (field_identifier) @method (#eq? @method "unwrap")))"#;
9const EXPECT_QUERY: &str = r#"(call_expression function: (field_expression field: (field_identifier) @method (#eq? @method "expect")))"#;
10
11impl TreeSitterOracle {
12    /// Count error handling patterns.
13    pub fn count_error_patterns(&mut self) -> Result<ErrorPatternCounts> {
14        Ok(ErrorPatternCounts {
15            result_types: self.query(RESULT_QUERY)?.matches.len(),
16            try_operators: self.query(TRY_QUERY)?.matches.len(),
17            unwrap_calls: self.query(UNWRAP_QUERY)?.matches.len(),
18            expect_calls: self.query(EXPECT_QUERY)?.matches.len(),
19            match_expressions: self.query(MATCH_QUERY)?.matches.len(),
20        })
21    }
22}