Query System

The Query trait is the core mechanism for implementing new loaders. It defines how to fetch, extract, and cache loader data.

Query Trait Definition

Export: lighty_loaders::utils::query::Query

use async_trait::async_trait;
use std::hash::Hash;
use std::time::Duration;

#[async_trait]
pub trait Query: Send + Sync {
    /// Type of query (enum defining what to fetch)
    type Query: Eq + Hash + Clone + Send + Sync + 'static;

    /// Type of extracted data (usually VersionMetaData)
    type Data: Clone + Send + Sync + 'static;

    /// Type of raw data from API (JSON or typed struct)
    type Raw: Send + Sync + 'static;

    /// Loader name (e.g., "vanilla", "fabric")
    fn name() -> &'static str;

    /// Fetch raw manifest from external source
    async fn fetch_full_data<V: VersionInfo>(version: &V) -> Result<Self::Raw>;

    /// Extract specific data from raw manifest
    async fn extract<V: VersionInfo>(
        version: &V,
        query: &Self::Query,
        raw: &Self::Raw
    ) -> Result<Self::Data>;

    /// Global TTL for cache (default: 1 hour)
    fn cache_ttl() -> Duration {
        Duration::from_secs(3600)
    }

    /// Per-query TTL (optional override)
    fn cache_ttl_for_query(_query: &Self::Query) -> Duration {
        Self::cache_ttl()
    }

    /// Build complete Version metadata
    async fn version_builder<V: VersionInfo>(
        version: &V,
        full_data: &Self::Raw
    ) -> Result<Version>;
}

Implementing a Custom Query

Example: Custom Mod Loader

Let's implement a complete custom loader called "MyModLoader".

Step 1: Define Module Structure

Create src/loaders/my_loader/ with:

  • mod.rs - Module exports

  • my_loader.rs - Query implementation

  • my_loader_metadata.rs - Data structures

Step 2: Define Metadata Types

Step 3: Implement Query

Step 4: Add to Loader Enum

In src/types/loader.rs, add your loader:

Step 5: Implement LoaderExtensions Dispatch

In src/types/loader_extensions.rs, add dispatch:

Step 6: Add Feature Flag

In Cargo.toml:

Step 7: Module Exports

Usage

Merging with Vanilla

If your loader extends Vanilla (like Fabric/Quilt), merge metadata:

Data Flow Diagrams

VersionInfo Data Flow

spinner

LoaderExtensions Data Flow

spinner

Implementing Custom VersionInfo

You can also implement VersionInfo for your own types:

Usage:

Event Integration

Emit events during fetching (optional):

Testing Your Query

Last updated