mrapids 0.1.31

Your OpenAPI, but executable
Documentation
//! Collections module for grouping and executing multiple API requests
//!
//! This module provides functionality to:
//! - Define collections of API requests in YAML format
//! - Execute collections with various options
//! - Save and analyze results
//! - Use variables and dependencies between requests

// These exports are used by main.rs (binary crate)
#![allow(unused_imports)]
#![allow(dead_code)]

pub mod condition;
pub mod context;
pub mod dependency;
pub mod executor;
pub mod models;
pub mod parser;
pub mod reporter;
pub mod testing;
pub mod validator;

#[cfg(test)]
mod tests;

// Re-exports for main.rs
pub use executor::{CollectionExecutor, ExecutionOptions};
pub use parser::parse_collection;
pub use reporter::ConsoleReporter;
pub use validator::validate_collection;

use anyhow::Result;
use std::path::{Path, PathBuf};

/// List all available collections in the collections directory
pub fn list_collections(collections_dir: &Path) -> Result<Vec<PathBuf>> {
    let mut collections = Vec::new();

    if !collections_dir.exists() {
        return Ok(collections);
    }

    find_yaml_files(collections_dir, &mut collections)?;

    collections.sort();
    Ok(collections)
}

/// Recursively find YAML files in a directory
fn find_yaml_files(dir: &Path, collections: &mut Vec<PathBuf>) -> Result<()> {
    for entry in std::fs::read_dir(dir)? {
        let entry = entry?;
        let path = entry.path();

        if path.is_dir() {
            find_yaml_files(&path, collections)?;
        } else if path.is_file() {
            if let Some(ext) = path.extension() {
                if ext == "yaml" || ext == "yml" {
                    collections.push(path);
                }
            }
        }
    }
    Ok(())
}

/// Find a collection by name in the collections directory
pub fn find_collection(collections_dir: &Path, name: &str) -> Result<PathBuf> {
    let name = name.strip_prefix("collections/").unwrap_or(name);

    let direct_path = collections_dir.join(name);
    if direct_path.exists() && direct_path.is_file() {
        return Ok(direct_path);
    }

    if !name.ends_with(".yaml") && !name.ends_with(".yml") {
        for ext in &["yaml", "yml"] {
            let path = collections_dir.join(format!("{}.{}", name, ext));
            if path.exists() {
                return Ok(path);
            }
        }
    }

    if name.contains('/') {
        return Err(crate::core::api::ApiError::ValidationError(format!(
            "Collection '{}' not found in {:?}",
            name, collections_dir
        ))
        .into());
    }

    for ext in &["yaml", "yml"] {
        let path = collections_dir.join(format!("{}.{}", name, ext));
        if path.exists() {
            return Ok(path);
        }
    }

    Err(crate::core::api::ApiError::ValidationError(format!(
        "Collection '{}' not found in {:?}",
        name, collections_dir
    ))
    .into())
}