pray-core 1.27.0

Prayfile core library — package manager for the language placed before inference
Documentation
use crate::literal::LiteralValue;
use crate::source_root_locator::{
    host_manager_keywords, is_host_source_kind, validate_host_package_name, validate_host_subdir,
};
use crate::{PrayError, PrayResult};
use std::collections::BTreeMap;

pub(super) fn host_keyword_in(keywords: &BTreeMap<String, LiteralValue>) -> Option<&'static str> {
    host_manager_keywords()
        .iter()
        .copied()
        .find(|keyword| keywords.contains_key(*keyword))
}

pub(super) fn parse_host_kind_and_url(
    keywords: &BTreeMap<String, LiteralValue>,
    manager: &str,
    has_positional_url: bool,
) -> PrayResult<(String, String)> {
    reject_combined_source_keywords(keywords, manager, has_positional_url)?;
    let Some(LiteralValue::String(package_name)) = keywords.get(manager) else {
        return Err(PrayError::Parse {
            kind: "manifest",
            message: format!("{manager}: requires a package name string"),
        });
    };
    validate_host_package_name(manager, package_name)?;
    Ok((manager.to_string(), package_name.clone()))
}

pub(super) fn validate_parsed_host_subdir(kind: &str, subdir: &Option<String>) -> PrayResult<()> {
    if let Some(subdir) = subdir {
        if is_host_source_kind(kind) {
            validate_host_subdir(subdir)?;
        }
    }
    Ok(())
}

fn reject_combined_source_keywords(
    keywords: &BTreeMap<String, LiteralValue>,
    manager: &str,
    has_positional_url: bool,
) -> PrayResult<()> {
    if has_positional_url || keywords.contains_key("path") || keywords.contains_key("git") {
        return Err(PrayError::Parse {
            kind: "manifest",
            message: format!("{manager}: cannot combine with a URL, path:, or git:"),
        });
    }
    let others = host_manager_keywords()
        .iter()
        .copied()
        .filter(|keyword| *keyword != manager && keywords.contains_key(*keyword))
        .count();
    if others > 0 {
        return Err(PrayError::Parse {
            kind: "manifest",
            message: "source accepts only one host package keyword".to_string(),
        });
    }
    Ok(())
}