elemental_core/runtime/
resolver.rs1use std::sync::Arc;
2
3use tokio::sync::OnceCell;
4
5use crate::runtime::{
6 distribution::Distribution,
7 error::{Error, Result},
8 provider::RuntimeProviderList,
9 providers::default_providers,
10};
11
12#[derive(Clone)]
13pub struct RuntimeResolver {
14 providers: RuntimeProviderList,
15 cache: Arc<OnceCell<Vec<Distribution>>>,
16}
17
18impl std::fmt::Debug for RuntimeResolver {
19 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20 f.debug_struct("RuntimeResolver")
21 .field("providers", &self.providers.len())
22 .finish_non_exhaustive()
23 }
24}
25
26impl RuntimeResolver {
27 pub fn new(providers: RuntimeProviderList) -> Self {
28 Self {
29 providers,
30 cache: Arc::new(OnceCell::new()),
31 }
32 }
33
34 pub fn with_default_providers() -> Self {
35 Self::new(default_providers())
36 }
37
38 pub async fn distributions(&self) -> Vec<Distribution> {
39 self.cache
40 .get_or_init(|| async {
41 Distribution::from_providers::<Vec<_>>(self.providers.clone()).await
42 })
43 .await
44 .clone()
45 }
46
47 async fn find_by_java_major(&self, major_version: usize) -> Option<Distribution> {
48 self.distributions()
49 .await
50 .into_iter()
51 .find(|distribution| distribution.matches_java_major_version(major_version))
52 }
53
54 pub async fn resolve(&self, required_major_version: usize) -> Result<Distribution> {
55 self.find_by_java_major(required_major_version)
56 .await
57 .ok_or(Error::RuntimeNotFound {
58 major_version: required_major_version,
59 })
60 }
61}