openfunctions-rs 0.1.0

A universal framework for creating and managing LLM tools and agents
Documentation
//! Loader utilities for tools and agents
//!
//! This module provides a simple and convenient loader for deserializing
//! data from common file formats like YAML and JSON.

use anyhow::Result;

/// A loader for deserializing data from files.
///
/// `Loader` provides a set of methods to easily load and parse files
/// into specified data structures.
pub struct Loader;

impl Loader {
    /// Creates a new `Loader` instance.
    pub fn new() -> Self {
        Self
    }

    /// Loads and deserializes a YAML file into a specified type.
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the YAML file.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to deserialize the YAML content into. It must implement
    ///   `serde::de::DeserializeOwned`.
    pub async fn load_yaml<T>(&self, path: &std::path::Path) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let content = tokio::fs::read_to_string(path).await?;
        Ok(serde_yaml::from_str(&content)?)
    }

    /// Loads and deserializes a JSON file into a specified type.
    ///
    /// # Arguments
    ///
    /// * `path` - A path to the JSON file.
    ///
    /// # Type Parameters
    ///
    /// * `T` - The type to deserialize the JSON content into. It must implement
    ///   `serde::de::DeserializeOwned`.
    pub async fn load_json<T>(&self, path: &std::path::Path) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let content = tokio::fs::read_to_string(path).await?;
        Ok(serde_json::from_str(&content)?)
    }
}

impl Default for Loader {
    /// Creates a new `Loader` instance with default settings.
    fn default() -> Self {
        Self::new()
    }
}