Skip to main content

vs_core/service/
current.rs

1//! Services for resolving the currently active tool version.
2
3use crate::{App, CoreError, CurrentTool};
4
5impl App {
6    /// Resolves the active version for a specific plugin.
7    pub fn current_tool(&self, plugin_name: &str) -> Result<Option<CurrentTool>, CoreError> {
8        let resolved = self.resolve_configured_tool_version(plugin_name)?;
9        let Some(resolved) = resolved else {
10            return Ok(None);
11        };
12        if self
13            .load_installed_runtime(&resolved.plugin, &resolved.version)?
14            .is_none()
15        {
16            return Ok(None);
17        }
18        Ok(Some(CurrentTool {
19            plugin: resolved.plugin,
20            version: resolved.version,
21            scope: resolved.scope,
22            source: resolved.source,
23        }))
24    }
25
26    /// Resolves all active tools for the current context.
27    pub fn current_tools(&self) -> Result<Vec<CurrentTool>, CoreError> {
28        let mut current = Vec::new();
29        for configured in self.collect_current_tools()? {
30            if self
31                .load_installed_runtime(&configured.plugin, &configured.version)?
32                .is_some()
33            {
34                current.push(configured);
35            }
36        }
37        Ok(current)
38    }
39
40    /// Returns all known plugins with their current version when available.
41    pub fn current_tool_statuses(&self) -> Result<Vec<(String, Option<String>)>, CoreError> {
42        let mut names = self
43            .added_plugins()?
44            .into_iter()
45            .map(|entry| entry.name)
46            .collect::<std::collections::BTreeSet<_>>();
47        names.extend(
48            self.list_installed_versions()?
49                .into_iter()
50                .map(|installed| installed.plugin),
51        );
52
53        let mut statuses = Vec::new();
54        for plugin in names {
55            let current = self.current_tool(&plugin)?;
56            statuses.push((plugin, current.map(|tool| tool.version)));
57        }
58        Ok(statuses)
59    }
60}