1use collections::HashMap;
 2use extension::{
 3    DownloadFileCapability, ExtensionCapability, NpmInstallPackageCapability, ProcessExecCapability,
 4};
 5use settings::Settings;
 6use std::sync::Arc;
 7
 8#[derive(Debug, Default, Clone)]
 9pub struct ExtensionSettings {
10    /// The extensions that should be automatically installed by Zed.
11    ///
12    /// This is used to make functionality provided by extensions (e.g., language support)
13    /// available out-of-the-box.
14    ///
15    /// Default: { "html": true }
16    pub auto_install_extensions: HashMap<Arc<str>, bool>,
17    pub auto_update_extensions: HashMap<Arc<str>, bool>,
18    pub granted_capabilities: Vec<ExtensionCapability>,
19}
20
21impl ExtensionSettings {
22    /// Returns whether the given extension should be auto-installed.
23    pub fn should_auto_install(&self, extension_id: &str) -> bool {
24        self.auto_install_extensions
25            .get(extension_id)
26            .copied()
27            .unwrap_or(true)
28    }
29
30    pub fn should_auto_update(&self, extension_id: &str) -> bool {
31        self.auto_update_extensions
32            .get(extension_id)
33            .copied()
34            .unwrap_or(true)
35    }
36}
37
38impl Settings for ExtensionSettings {
39    fn from_settings(content: &settings::SettingsContent) -> Self {
40        Self {
41            auto_install_extensions: content.extension.auto_install_extensions.clone(),
42            auto_update_extensions: content.extension.auto_update_extensions.clone(),
43            granted_capabilities: content
44                .extension
45                .granted_extension_capabilities
46                .clone()
47                .unwrap_or_default()
48                .into_iter()
49                .map(|capability| match capability {
50                    settings::ExtensionCapabilityContent::ProcessExec { command, args } => {
51                        ExtensionCapability::ProcessExec(ProcessExecCapability { command, args })
52                    }
53                    settings::ExtensionCapabilityContent::DownloadFile { host, path } => {
54                        ExtensionCapability::DownloadFile(DownloadFileCapability { host, path })
55                    }
56                    settings::ExtensionCapabilityContent::NpmInstallPackage { package } => {
57                        ExtensionCapability::NpmInstallPackage(NpmInstallPackageCapability {
58                            package,
59                        })
60                    }
61                })
62                .collect(),
63        }
64    }
65}