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