openfunctions_rs/parser/mod.rs
1//! Parsers for extracting tool definitions from source code.
2//!
3//! This module provides parsers for various programming languages to extract
4//! structured `ToolDefinition`s from comments in source files. This allows
5//! developers to define their tools inline with their code.
6
7use crate::core::tool::ToolLanguage;
8use crate::models::ToolDefinition;
9use anyhow::Result;
10
11pub mod bash;
12pub mod javascript;
13pub mod python;
14
15/// A parser for extracting tool definitions from source code.
16///
17/// `ToolParser` delegates to a language-specific parser based on the
18/// provided `ToolLanguage`.
19pub struct ToolParser {
20 language: ToolLanguage,
21}
22
23impl ToolParser {
24 /// Creates a new `ToolParser` for the given language.
25 pub fn new(language: ToolLanguage) -> Self {
26 Self { language }
27 }
28
29 /// Parses the given source code and extracts a `ToolDefinition`.
30 ///
31 /// # Arguments
32 ///
33 /// * `source` - The source code to parse as a string slice.
34 ///
35 /// # Returns
36 ///
37 /// A `Result` containing the parsed `ToolDefinition` or an error if
38 /// parsing fails.
39 pub fn parse(&self, source: &str) -> Result<ToolDefinition> {
40 match self.language {
41 ToolLanguage::Bash => bash::parse(source),
42 ToolLanguage::JavaScript => javascript::parse(source),
43 ToolLanguage::Python => python::parse(source),
44 }
45 }
46}