Skip to main content

lighty_java/
lib.rs

1// Copyright (c) 2025 Hamadi
2// Licensed under the MIT License
3
4//! Lighty Java - JRE download, install, and process execution.
5
6mod distribution;
7pub mod jre_downloader;
8pub mod runtime;
9pub mod errors;
10
11use serde::{Deserialize, Serialize};
12
13pub use errors::{
14    JreError, JreResult,
15    JavaRuntimeError, JavaRuntimeResult,
16    DistributionError, DistributionResult,
17};
18
19/// Selection method for Java distribution
20#[derive(Deserialize, Serialize, Clone)]
21#[serde(tag = "type", content = "value")]
22pub enum DistributionSelection {
23    #[serde(rename = "automatic")]
24    Automatic(String),
25    #[serde(rename = "custom")]
26    Custom(String),
27    #[serde(rename = "manual")]
28    Manual(JavaDistribution),
29}
30
31impl Default for DistributionSelection {
32    fn default() -> Self {
33        DistributionSelection::Automatic(String::new())
34    }
35}
36
37/// Available Java distributions
38#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
39pub enum JavaDistribution {
40    #[serde(rename = "temurin")]
41    Temurin,
42    #[serde(rename = "graalvm")]
43    GraalVM,
44    #[serde(rename = "zulu")]
45    Zulu,
46    #[serde(rename = "liberica")]
47    Liberica,
48}
49
50impl Default for JavaDistribution {
51    fn default() -> Self {
52        // Temurin is the default as it supports all Java versions
53        JavaDistribution::Temurin
54    }
55}
56
57impl JavaDistribution {
58    /// Returns the canonical name of this distribution
59    pub fn get_name(&self) -> &str {
60        match self {
61            JavaDistribution::Temurin => "temurin",
62            JavaDistribution::GraalVM => "graalvm",
63            JavaDistribution::Zulu => "zulu",
64            JavaDistribution::Liberica => "liberica",
65        }
66    }
67
68    /// Returns whether this distribution publishes `version` for the
69    /// current platform.
70    ///
71    /// Known gaps:
72    /// - Temurin: no Java 8 for macOS ARM64 (Apple Silicon released after
73    ///   Java 8 reached EOL).
74    /// - GraalVM: Java 17+ only.
75    pub fn supports_version(&self, version: u8) -> bool {
76        use lighty_core::system::{Architecture, OperatingSystem, ARCHITECTURE, OS};
77
78        match self {
79            JavaDistribution::Temurin => {
80                // No Java 8 for macOS ARM64
81                !(version == 8 && OS == OperatingSystem::OSX && ARCHITECTURE == Architecture::AARCH64)
82            }
83            JavaDistribution::GraalVM => version >= 17,
84            JavaDistribution::Zulu | JavaDistribution::Liberica => true,
85        }
86    }
87
88    /// Returns a fallback distribution for `(self, version)` if `self`
89    /// does not support that version on the current platform.
90    ///
91    /// Returns `None` when no fallback is needed (`self` is supported).
92    /// Fallback candidates are tried in order: Zulu → Liberica → Temurin
93    /// (decreasing platform coverage).
94    pub fn get_fallback(&self, version: u8) -> Option<JavaDistribution> {
95        if self.supports_version(version) {
96            return None;
97        }
98
99        // Zulu/Liberica/Temurin in decreasing platform coverage order.
100        let candidates = [
101            JavaDistribution::Zulu,
102            JavaDistribution::Liberica,
103            JavaDistribution::Temurin,
104        ];
105
106        candidates.into_iter().find(|d| d.supports_version(version))
107    }
108
109    /// Gets download URL for the distribution
110    pub async fn get_download_url(&self, jre_version: &u8) -> DistributionResult<String> {
111        distribution::get_download_url(self, jre_version).await
112    }
113}