Skip to main content

elemental_driver/families/installer/source/
spec.rs

1use std::fmt::Debug;
2
3use elemental_core::minecraft::MinecraftVersionId;
4use elemental_infra::SafeRelativePath;
5
6use crate::{
7    catalog::LoaderRelease,
8    error::{Error, Result},
9    families::installer::InstallerMavenArtifactSpec,
10    loader_version::LoaderVersionId,
11    maven::MavenCoordinate,
12    url::{Origin, OriginEndpoints},
13};
14
15pub trait MavenSpec: Clone + Debug + Send + Sync + 'static {
16    type Origin: Origin;
17
18    const SOURCE_NAME: &'static str;
19    const MAVEN_ORIGIN: Self::Origin;
20    const GROUP: &'static str;
21    const ARTIFACT: &'static str;
22
23    fn version(game_version: &MinecraftVersionId, loader_version: &LoaderVersionId) -> String {
24        format!("{}-{}", game_version.as_str(), loader_version.as_str())
25    }
26
27    fn coordinate(version: &str) -> Result<MavenCoordinate> {
28        let notation = format!("{}:{}:{version}", Self::GROUP, Self::ARTIFACT);
29        MavenCoordinate::parse(&notation).ok_or_else(|| Error::InvalidNotation {
30            label: Self::SOURCE_NAME.to_owned(),
31            notation,
32        })
33    }
34
35    fn metadata_path() -> Result<String> {
36        let directory = MavenCoordinate::artifact_directory_path_for(Self::GROUP, Self::ARTIFACT)
37            .ok_or_else(|| Error::InvalidNotation {
38            label: Self::SOURCE_NAME.to_owned(),
39            notation: format!("{}:{}", Self::GROUP, Self::ARTIFACT),
40        })?;
41        Ok(format!("/{directory}/maven-metadata.xml"))
42    }
43
44    fn installer_coordinate(version: &str) -> Result<MavenCoordinate> {
45        let coordinate = Self::coordinate(version)?;
46        coordinate
47            .with_classifier("installer")
48            .ok_or_else(|| Error::InvalidNotation {
49                label: Self::SOURCE_NAME.to_owned(),
50                notation: coordinate.to_string(),
51            })
52    }
53
54    fn installer_artifact(
55        endpoints: &OriginEndpoints<Self::Origin>,
56        game_version: &MinecraftVersionId,
57        loader_version: &LoaderVersionId,
58    ) -> Result<InstallerMavenArtifactSpec> {
59        let version = Self::version(game_version, loader_version);
60        let coordinate = Self::installer_coordinate(&version)?;
61        let artifact_path = coordinate.artifact_path();
62        Ok(InstallerMavenArtifactSpec {
63            coordinate: coordinate.to_string(),
64            download_url: endpoints.resolve(Self::MAVEN_ORIGIN, &artifact_path)?,
65            relative_path: SafeRelativePath::parse(artifact_path)?,
66        })
67    }
68
69    fn release(version: String) -> Option<(MinecraftVersionId, LoaderRelease)>;
70
71    fn rewrite_upstream(
72        endpoints: &OriginEndpoints<Self::Origin>,
73        raw_url: &str,
74    ) -> Result<String> {
75        endpoints.rewrite_known_or_keep(raw_url)
76    }
77}