lighty_java/lib.rs
1// Copyright (c) 2025 Hamadi
2// Licensed under the MIT License
3
4//! Lighty Java - Java Runtime Management
5//!
6//! This crate provides functionality for managing Java Runtime Environments (JRE)
7//! including downloading, installing, and executing Java processes.
8//!
9//! ## Features
10//! - Support for multiple Java distributions (Temurin, GraalVM, Zulu, Liberica)
11//! - Cross-platform JRE download and installation
12//! - Java process execution with I/O streaming
13//! - File size verification for download integrity
14//!
15//! ## License
16//! This implementation is original work licensed under MIT.
17//! It does not derive from GPL-licensed code.
18//!
19//! ## Clean Room Implementation
20//! The distribution management system was implemented from scratch using only
21//! publicly documented APIs from Adoptium, Oracle, Azul, and Foojay.
22
23mod distribution;
24pub mod jre_downloader;
25pub mod runtime;
26pub mod errors;
27
28use serde::{Deserialize, Serialize};
29
30pub use errors::{
31 JreError, JreResult,
32 JavaRuntimeError, JavaRuntimeResult,
33 DistributionError, DistributionResult,
34};
35
36// ============================================================================
37// Public Types
38// ============================================================================
39
40/// Selection method for Java distribution
41#[derive(Deserialize, Serialize, Clone)]
42#[serde(tag = "type", content = "value")]
43pub enum DistributionSelection {
44 #[serde(rename = "automatic")]
45 Automatic(String),
46 #[serde(rename = "custom")]
47 Custom(String),
48 #[serde(rename = "manual")]
49 Manual(JavaDistribution),
50}
51
52impl Default for DistributionSelection {
53 fn default() -> Self {
54 DistributionSelection::Automatic(String::new())
55 }
56}
57
58/// Available Java distributions
59#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
60pub enum JavaDistribution {
61 #[serde(rename = "temurin")]
62 Temurin,
63 #[serde(rename = "graalvm")]
64 GraalVM,
65 #[serde(rename = "zulu")]
66 Zulu,
67 #[serde(rename = "liberica")]
68 Liberica,
69}
70
71impl Default for JavaDistribution {
72 fn default() -> Self {
73 // Temurin is the default as it supports all Java versions
74 JavaDistribution::Temurin
75 }
76}
77
78impl JavaDistribution {
79 /// Returns the canonical name of this distribution
80 pub fn get_name(&self) -> &str {
81 match self {
82 JavaDistribution::Temurin => "temurin",
83 JavaDistribution::GraalVM => "graalvm",
84 JavaDistribution::Zulu => "zulu",
85 JavaDistribution::Liberica => "liberica",
86 }
87 }
88
89 /// Checks if this distribution supports the given Java version
90 pub fn supports_version(&self, version: u8) -> bool {
91 match self {
92 JavaDistribution::Temurin => true, // Supports all versions (8, 11, 17, 21, etc.)
93 JavaDistribution::GraalVM => version >= 17, // Only 17+ (JDK only, no JRE)
94 JavaDistribution::Zulu => true, // Supports all versions
95 JavaDistribution::Liberica => true, // Supports all versions
96 }
97 }
98
99 /// Gets download URL for the distribution
100 ///
101 /// Queries the respective API or builds direct download URLs for each distribution.
102 pub async fn get_download_url(&self, jre_version: &u8) -> DistributionResult<String> {
103 distribution::get_download_url(self, jre_version).await
104 }
105
106}