lighty_java/
distribution.rs1use lighty_core::system::{ARCHITECTURE, OS};
2use crate::errors::{DistributionError, DistributionResult};
3use serde::{Deserialize, Serialize};
4
5#[derive(Deserialize, Serialize, Clone)]
6#[serde(tag = "type", content = "value")]
7pub enum DistributionSelection {
8 #[serde(rename = "automatic")]
9 Automatic(String), #[serde(rename = "custom")]
11 Custom(String),
12 #[serde(rename = "manual")]
13 Manual(JavaDistribution),
14}
15
16impl Default for DistributionSelection {
17 fn default() -> Self {
18 DistributionSelection::Automatic(String::new())
19 }
20}
21
22#[derive(Deserialize, Serialize, Clone)]
23pub enum JavaDistribution {
24 #[serde(rename = "temurin")]
25 Temurin,
26 #[serde(rename = "graalvm")]
27 GraalVM,
28}
29
30impl Default for JavaDistribution {
31 fn default() -> Self {
32 JavaDistribution::Temurin
34 }
35}
36
37impl JavaDistribution {
38 pub fn get_url(&self, jre_version: &u32) -> DistributionResult<String> {
39 let os_arch = ARCHITECTURE.get_simple_name()?;
40 let archive_type = OS.get_archive_type()?;
41
42 Ok(match self {
43 JavaDistribution::Temurin => {
44 let os_name = OS.get_adoptium_name()?;
45 format!(
46 "https://api.adoptium.net/v3/binary/latest/{}/ga/{}/{}/jre/hotspot/normal/eclipse?project=jdk",
47 jre_version, os_name, os_arch
48 )
49 }
50 JavaDistribution::GraalVM => {
51 let os_name = OS.get_graal_name()?;
52
53 if jre_version > &17 {
54 format!(
55 "https://download.oracle.com/graalvm/{}/latest/graalvm-jdk-{}_{}-{}_bin.{}",
56 jre_version, jre_version, os_name, os_arch, archive_type
57 )
58 } else if jre_version == &17 {
59 format!(
61 "https://download.oracle.com/graalvm/17/archive/graalvm-jdk-17.0.12_{}-{}_bin.{}",
62 os_name, os_arch, archive_type
63 )
64 } else {
65 return Err(DistributionError::UnsupportedVersion {
66 version: *jre_version,
67 distribution: "GraalVM".to_string(),
68 });
69 }
70 }
71 })
72 }
73
74 pub fn get_name(&self) -> &str {
75 match self {
76 JavaDistribution::Temurin => "temurin",
77 JavaDistribution::GraalVM => "graalvm",
78 }
79 }
80
81 pub fn supports_version(&self, version: u32) -> bool {
82 match self {
83 JavaDistribution::Temurin => true, JavaDistribution::GraalVM => version >= 17, }
86 }
87}