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/// Available Java distributions
20#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
21pub enum JavaDistribution {
22    #[serde(rename = "temurin")]
23    Temurin,
24    #[serde(rename = "graalvm")]
25    GraalVM,
26    #[serde(rename = "zulu")]
27    Zulu,
28    #[serde(rename = "liberica")]
29    Liberica,
30}
31
32impl Default for JavaDistribution {
33    fn default() -> Self {
34        // Temurin is the default as it supports all Java versions
35        JavaDistribution::Temurin
36    }
37}
38
39impl JavaDistribution {
40    /// Returns the canonical name of this distribution
41    pub fn get_name(&self) -> &str {
42        match self {
43            JavaDistribution::Temurin => "temurin",
44            JavaDistribution::GraalVM => "graalvm",
45            JavaDistribution::Zulu => "zulu",
46            JavaDistribution::Liberica => "liberica",
47        }
48    }
49
50    /// Returns whether this distribution publishes `version` for the
51    /// current platform.
52    ///
53    /// Known gaps:
54    /// - Temurin: no Java 8 for macOS ARM64 (Apple Silicon released after
55    ///   Java 8 reached EOL).
56    /// - GraalVM: Java 17+ only.
57    pub fn supports_version(&self, version: u8) -> bool {
58        use lighty_core::system::{Architecture, OperatingSystem, ARCHITECTURE, OS};
59
60        match self {
61            JavaDistribution::Temurin => {
62                // No Java 8 for macOS ARM64
63                !(version == 8 && OS == OperatingSystem::OSX && ARCHITECTURE == Architecture::AARCH64)
64            }
65            JavaDistribution::GraalVM => version >= 17,
66            JavaDistribution::Zulu | JavaDistribution::Liberica => true,
67        }
68    }
69
70    /// Returns a fallback distribution for `(self, version)` if `self`
71    /// does not support that version on the current platform.
72    ///
73    /// Returns `None` when no fallback is needed (`self` is supported).
74    /// Fallback candidates are tried in order: Zulu → Liberica → Temurin
75    /// (decreasing platform coverage).
76    pub fn get_fallback(&self, version: u8) -> Option<JavaDistribution> {
77        if self.supports_version(version) {
78            return None;
79        }
80
81        // Zulu/Liberica/Temurin in decreasing platform coverage order.
82        let candidates = [
83            JavaDistribution::Zulu,
84            JavaDistribution::Liberica,
85            JavaDistribution::Temurin,
86        ];
87
88        candidates.into_iter().find(|d| d.supports_version(version))
89    }
90
91    /// Gets download URL for the distribution
92    pub async fn get_download_url(&self, jre_version: &u8) -> DistributionResult<String> {
93        distribution::get_download_url(self, jre_version).await
94    }
95}