pray-core 1.27.0

Prayfile core library — package manager for the language placed before inference
Documentation
use crate::manifest::ManifestSource;
use crate::resolve_context::ResolveOptions;
use crate::source_root_locator::{
    host_prayers_directory, locate_host_package, CargoPackages, HostPackageLocation,
};
use crate::PrayResult;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

pub(crate) struct HostSourceSet<'a> {
    project_root: &'a Path,
    options: &'a ResolveOptions,
    cargo_packages: RefCell<Option<CargoPackages>>,
    locations: RefCell<BTreeMap<(String, String), HostPackageLocation>>,
    roots: RefCell<BTreeMap<String, PathBuf>>,
    versions: RefCell<BTreeMap<String, String>>,
}

impl<'a> HostSourceSet<'a> {
    pub fn new(project_root: &'a Path, options: &'a ResolveOptions) -> Self {
        Self {
            project_root,
            options,
            cargo_packages: RefCell::default(),
            locations: RefCell::default(),
            roots: RefCell::default(),
            versions: RefCell::default(),
        }
    }

    pub fn prayers_root(&self, source: &ManifestSource) -> PrayResult<PathBuf> {
        if let Some(root) = self.roots.borrow().get(&source.name) {
            return Ok(root.clone());
        }
        let mut locations = self.locations.borrow_mut();
        let key = (source.kind.clone(), source.url.clone());
        let location = match locations.entry(key) {
            std::collections::btree_map::Entry::Occupied(entry) => entry.into_mut(),
            std::collections::btree_map::Entry::Vacant(entry) => entry.insert(locate_host_package(
                self.project_root,
                &source.kind,
                &source.url,
                self.options,
                &mut self.cargo_packages.borrow_mut(),
            )?),
        };
        let root = host_prayers_directory(
            location,
            &source.kind,
            &source.url,
            source.subdir.as_deref(),
        )?;
        self.roots
            .borrow_mut()
            .insert(source.name.clone(), root.clone());
        if let Some(version) = &location.version {
            self.versions
                .borrow_mut()
                .insert(source.name.clone(), version.clone());
        }
        Ok(root)
    }

    pub fn into_versions(self) -> BTreeMap<String, String> {
        self.versions.into_inner()
    }
}