Skip to main content

dort_core/
tools.rs

1//! Tool version policy and package planning.
2pub mod binaries;
3pub mod declaration;
4pub mod inspection;
5pub mod install;
6pub mod install_preview;
7pub mod planning;
8use crate::model::Provider;
9use semver::Version;
10use serde::{Deserialize, Serialize};
11
12/// A provider-enforceable request, using the existing manifest version grammar.
13#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
14#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)]
15pub enum VersionPolicy {
16    ProviderDefault {},
17    /// Select the current release when installing a missing package, not an upgrade request.
18    Latest {},
19    Exact {
20        version: String,
21    },
22}
23
24/// Comparison only: this is neither proof of installation nor an execution approval.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
26#[serde(rename_all = "snake_case")]
27pub enum VersionMatch {
28    Satisfied,
29    Mismatch,
30    /// Missing, malformed or unsupported observations must not become upgrade/install guesses.
31    Unverifiable,
32}
33
34#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
35#[error("this package manager cannot install that version rule")]
36pub struct UnsupportedVersionPolicy;
37
38impl VersionPolicy {
39    pub fn parse(
40        provider: Provider,
41        requested: Option<&str>,
42    ) -> Result<Self, UnsupportedVersionPolicy> {
43        let Some(requested) = requested else {
44            return Ok(Self::ProviderDefault {});
45        };
46        if requested == "latest" {
47            return Ok(Self::Latest {});
48        }
49        if provider == Provider::Npm && Version::parse(requested).is_ok() {
50            return Ok(Self::Exact {
51                version: requested.into(),
52            });
53        }
54        Err(UnsupportedVersionPolicy)
55    }
56
57    /// Compare provider-reported metadata without running a binary or consulting a registry.
58    ///
59    /// The caller must establish the provider/package/executable identity separately. In
60    /// particular, a PATH binary or a successful comparison is not proof of Dort ownership.
61    /// Exact requests include build metadata. Unverifiable observations are not mismatches.
62    pub fn matches_version(&self, observed: Option<&str>) -> VersionMatch {
63        let Some(observed) = observed.filter(|version| valid_observed_version(version)) else {
64            return VersionMatch::Unverifiable;
65        };
66        match self {
67            Self::ProviderDefault {} | Self::Latest {} => VersionMatch::Satisfied,
68            Self::Exact { version } => match (Version::parse(version), Version::parse(observed)) {
69                (Ok(requested), Ok(installed)) => comparison(requested == installed),
70                _ => VersionMatch::Unverifiable,
71            },
72        }
73    }
74}
75
76fn comparison(satisfied: bool) -> VersionMatch {
77    if satisfied {
78        VersionMatch::Satisfied
79    } else {
80        VersionMatch::Mismatch
81    }
82}
83
84fn valid_observed_version(version: &str) -> bool {
85    !version.is_empty()
86        && version.len() <= 256
87        && version.as_bytes()[0].is_ascii_alphanumeric()
88        && version
89            .bytes()
90            .all(|byte| byte.is_ascii_alphanumeric() || b".:+_~-".contains(&byte))
91}