Skip to main content

ai_agent/utils/plugins/
plugin_identifier.rs

1// Source: ~/claudecode/openclaudecode/src/utils/plugins/pluginIdentifier.ts
2#![allow(dead_code)]
3
4use super::schemas::PluginScope;
5
6/// Parsed plugin identifier with name and optional marketplace.
7pub struct ParsedPluginIdentifier {
8    pub name: String,
9    pub marketplace: Option<String>,
10}
11
12/// Parse a plugin identifier string into name and marketplace components.
13pub fn parse_plugin_identifier(plugin: &str) -> ParsedPluginIdentifier {
14    if let Some(at_pos) = plugin.find('@') {
15        let name = plugin[..at_pos].to_string();
16        let marketplace = plugin[at_pos + 1..].to_string();
17        ParsedPluginIdentifier {
18            name,
19            marketplace: Some(marketplace),
20        }
21    } else {
22        ParsedPluginIdentifier {
23            name: plugin.to_string(),
24            marketplace: None,
25        }
26    }
27}
28
29/// Build a plugin ID from name and marketplace.
30pub fn build_plugin_id(name: &str, marketplace: Option<&str>) -> String {
31    match marketplace {
32        Some(m) => format!("{}@{}", name, m),
33        None => name.to_string(),
34    }
35}
36
37/// Check if a marketplace name is an official marketplace.
38pub fn is_official_marketplace_name(marketplace: Option<&str>) -> bool {
39    match marketplace {
40        Some(m) => {
41            let lowercase = m.to_lowercase();
42            super::schemas::allowed_official_marketplace_names()
43                .iter()
44                .any(|&s| s == lowercase)
45        }
46        None => false,
47    }
48}
49
50/// Map from installable plugin scope to editable setting source.
51pub fn scope_to_setting_source(
52    scope: &PluginScope,
53) -> Result<&'static str, Box<dyn std::error::Error + Send + Sync>> {
54    match scope {
55        PluginScope::User => Ok("userSettings"),
56        PluginScope::Project => Ok("projectSettings"),
57        PluginScope::Local => Ok("localSettings"),
58        PluginScope::Managed => Err("Cannot install plugins to managed scope".into()),
59    }
60}
61
62/// Convert an editable setting source to its corresponding plugin scope.
63pub fn setting_source_to_scope(source: &str) -> Option<PluginScope> {
64    match source {
65        "userSettings" => Some(PluginScope::User),
66        "projectSettings" => Some(PluginScope::Project),
67        "localSettings" => Some(PluginScope::Local),
68        "policySettings" => Some(PluginScope::Managed),
69        _ => None,
70    }
71}