openfunctions-rs 0.1.0

A universal framework for creating and managing LLM tools and agents
Documentation
//! Parsers for extracting tool definitions from source code.
//!
//! This module provides parsers for various programming languages to extract
//! structured `ToolDefinition`s from comments in source files. This allows
//! developers to define their tools inline with their code.

use crate::core::tool::ToolLanguage;
use crate::models::ToolDefinition;
use anyhow::Result;

pub mod bash;
pub mod javascript;
pub mod python;

/// A parser for extracting tool definitions from source code.
///
/// `ToolParser` delegates to a language-specific parser based on the
/// provided `ToolLanguage`.
pub struct ToolParser {
    language: ToolLanguage,
}

impl ToolParser {
    /// Creates a new `ToolParser` for the given language.
    pub fn new(language: ToolLanguage) -> Self {
        Self { language }
    }

    /// Parses the given source code and extracts a `ToolDefinition`.
    ///
    /// # Arguments
    ///
    /// * `source` - The source code to parse as a string slice.
    ///
    /// # Returns
    ///
    /// A `Result` containing the parsed `ToolDefinition` or an error if
    /// parsing fails.
    pub fn parse(&self, source: &str) -> Result<ToolDefinition> {
        match self.language {
            ToolLanguage::Bash => bash::parse(source),
            ToolLanguage::JavaScript => javascript::parse(source),
            ToolLanguage::Python => python::parse(source),
        }
    }
}