Skip to main content

elemental_driver/drivers/liteloader/source/
client.rs

1use elemental_schema::fabric::ProfileJson;
2
3use crate::{
4    error::Result,
5    http::{HttpSource, fetch_json},
6};
7
8use super::super::manifest::{LiteLoaderManifest, collect_releases, select_build};
9use super::super::profile::build_profile_json;
10use super::LiteLoaderRelease;
11use super::endpoints::LiteLoaderEndpoints;
12
13#[derive(Debug, Clone)]
14pub struct LiteLoaderSource {
15    inner: HttpSource<LiteLoaderEndpoints>,
16}
17
18impl Default for LiteLoaderSource {
19    fn default() -> Self {
20        Self::new(LiteLoaderEndpoints::default())
21    }
22}
23
24impl LiteLoaderSource {
25    pub fn new(endpoints: LiteLoaderEndpoints) -> Self {
26        Self {
27            inner: HttpSource::new(endpoints, "liteloader source"),
28        }
29    }
30
31    pub fn with_client(endpoints: LiteLoaderEndpoints, client: reqwest::Client) -> Self {
32        Self {
33            inner: HttpSource::with_client(endpoints, client),
34        }
35    }
36
37    pub fn endpoints(&self) -> &LiteLoaderEndpoints {
38        self.inner.endpoints()
39    }
40
41    pub async fn releases(&self) -> Result<Vec<LiteLoaderRelease>> {
42        let manifest = self.manifest().await?;
43        Ok(collect_releases(manifest))
44    }
45
46    pub async fn profile_json(
47        &self,
48        game_version: &str,
49        loader_version: &str,
50    ) -> Result<ProfileJson> {
51        let manifest = self.manifest().await?;
52        let selected = select_build(&manifest, game_version, loader_version)?;
53        build_profile_json(self.inner.client()?, self.endpoints(), &selected).await
54    }
55
56    async fn manifest(&self) -> Result<LiteLoaderManifest> {
57        let url = self.endpoints().versions_manifest_url()?;
58        fetch_json(self.inner.client()?, url.as_str(), "liteloader source").await
59    }
60}