Skip to main content

lighty_loaders/utils/
query.rs

1use crate::types::version_metadata::Version;
2use crate::types::VersionInfo;
3use lighty_core::QueryError;
4use async_trait::async_trait;
5use std::hash::Hash;
6
7pub type Result<T> = std::result::Result<T, QueryError>;
8
9/// Generic loader manifest query interface.
10///
11/// Implementors describe a loader's manifest source and how to extract
12/// each sub-query (libraries, main class, etc.) from the raw payload.
13/// `ManifestRepository<F>` handles caching and concurrency on top.
14#[async_trait]
15pub trait Query: Send + Sync {
16    type Query: Eq + Hash + Clone + Send + Sync + 'static;
17
18    type Data: Clone + Send + Sync + 'static;
19
20    type Raw: Send + Sync + 'static;
21
22
23    /// Loader group name (`"vanilla"`, `"forge"`, `"custom"`, ...).
24    fn name() -> &'static str;
25
26    /// Fetches the raw manifest from its remote source.
27    async fn fetch_full_data<V: VersionInfo>(version: &V) -> Result<Self::Raw>;
28
29    /// Extracts a typed sub-query from the raw manifest.
30    async fn extract<V: VersionInfo>(version: &V, query: &Self::Query, raw: &Self::Raw) -> Result<Self::Data>;
31
32    /// Builds the full [`Version`] (all sub-queries merged) from the raw manifest.
33    async fn version_builder<V: VersionInfo>(version: &V, full_data: &Self::Raw) -> Result<Version>;
34}
35
36
37#[derive(Debug, Clone, PartialEq, Eq, Hash)]
38pub struct InstanceKey {
39    pub name: String,
40    pub minecraft_version: String,
41    pub loader_version: String,
42}
43
44impl InstanceKey {
45    pub fn of<V: VersionInfo>(version: &V) -> Self {
46        Self {
47            name: version.name().to_string(),
48            minecraft_version: version.minecraft_version().to_string(),
49            loader_version: version.loader_version().to_string(),
50        }
51    }
52}
53
54/// Cache key combining the instance and the sub-query discriminator.
55#[derive(Debug, Clone, PartialEq, Eq, Hash)]
56pub struct QueryKey<Q> {
57    pub instance: InstanceKey,
58    pub query: Q,
59}
60