Jelajahi Sumber

move types to tauri-utils

Lucas Nogueira 2 tahun lalu
induk
melakukan
8467f138bb

+ 3 - 21
core/tauri-build/src/lib.rs

@@ -17,9 +17,10 @@ pub use anyhow::Result;
 use cargo_toml::Manifest;
 use heck::AsShoutySnakeCase;
 
-use serde::{Deserialize, Serialize};
+use serde::Deserialize;
 use tauri_utils::{
-  config::{Config, Namespace},
+  config::Config,
+  namespace::{MemberResolution, NamespaceLockFile},
   resources::{external_binaries, resource_relpath, ResourcePaths},
 };
 
@@ -519,25 +520,6 @@ pub fn try_build(attributes: Attributes) -> Result<()> {
   Ok(())
 }
 
-#[derive(Serialize)]
-struct MemberResolution {
-  member: String,
-  capabilities: HashMap<String, ResolvedCapability>,
-}
-
-#[derive(Default, Serialize)]
-struct ResolvedCapability {
-  features: Vec<String>,
-}
-
-#[derive(Serialize)]
-struct NamespaceLockFile {
-  version: u8,
-  namespaces: Vec<Namespace>,
-  plugins: plugin::ManifestMap,
-  resolution: Vec<MemberResolution>,
-}
-
 #[derive(Deserialize)]
 struct CargoMetadata {
   workspace_root: PathBuf,

+ 6 - 111
core/tauri-build/src/plugin.rs

@@ -1,4 +1,8 @@
-use serde::{Deserialize, Serialize};
+// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+pub use tauri_utils::plugin::{Manifest, ManifestMap, ScopeType};
 
 use std::{
   collections::HashMap,
@@ -8,92 +12,6 @@ use std::{
 };
 
 const PLUGIN_METADATA_KEY: &str = "PLUGIN_MANIFEST_PATH";
-const DEFAULT_CAPABILITY_ID: &str = "default";
-
-#[derive(Debug, Serialize, Deserialize)]
-pub enum ScopeType {
-  String,
-}
-
-#[derive(Debug, Clone, Default, Serialize, Deserialize)]
-pub struct CapabilityScope {
-  #[serde(default)]
-  allowed: Vec<serde_json::Value>,
-  #[serde(default)]
-  blocked: Vec<serde_json::Value>,
-}
-
-#[derive(Debug, Clone, Default, Serialize, Deserialize)]
-pub struct Capability {
-  #[serde(default)]
-  pub(crate) id: String,
-  pub(crate) description: String,
-  #[serde(default)]
-  pub(crate) features: Vec<String>,
-  #[serde(default)]
-  pub(crate) scope: CapabilityScope,
-}
-
-#[derive(Debug, Serialize, Deserialize)]
-pub struct Manifest {
-  plugin: String,
-  default_capability: Option<Capability>,
-  capabilities: Vec<Capability>,
-  features: Vec<String>,
-  scope_type: Vec<ScopeType>,
-}
-
-impl Manifest {
-  pub fn new(plugin: impl Into<String>) -> Self {
-    Self {
-      plugin: plugin.into(),
-      default_capability: None,
-      capabilities: Vec::new(),
-      features: Vec::new(),
-      scope_type: Vec::new(),
-    }
-  }
-
-  pub fn default_capability(mut self, default_capability: impl AsRef<str>) -> Self {
-    let mut capability: Capability = serde_json::from_str(default_capability.as_ref())
-      .expect("failed to deserialize default capability");
-    assert!(
-      capability.id.is_empty(),
-      "default capability cannot have an specific identifier"
-    );
-    capability.id = DEFAULT_CAPABILITY_ID.into();
-    self.default_capability.replace(capability);
-    self
-  }
-
-  pub fn capability(mut self, capability: impl AsRef<str>) -> Self {
-    let capability: Capability =
-      serde_json::from_str(capability.as_ref()).expect("failed to deserialize default capability");
-    assert!(
-      !capability.id.is_empty(),
-      "capability must have an specific identifier"
-    );
-    self.capabilities.push(capability);
-    self
-  }
-
-  pub fn feature(mut self, feature: impl Into<String>) -> Self {
-    self.features.push(feature.into());
-    self
-  }
-
-  pub fn features<I: IntoIterator<Item = S>, S: Into<String>>(mut self, features: I) -> Self {
-    for feature in features {
-      self = self.feature(feature);
-    }
-    self
-  }
-
-  pub fn scope_type(mut self, ty: ScopeType) -> Self {
-    self.scope_type.push(ty);
-    self
-  }
-}
 
 pub fn set_manifest(manifest: Manifest) {
   let manifest_str = serde_json::to_string(&manifest).expect("failed to serialize plugin manifest");
@@ -108,29 +26,6 @@ pub fn set_manifest(manifest: Manifest) {
   println!("cargo:{PLUGIN_METADATA_KEY}={}", manifest_path.display());
 }
 
-#[derive(Serialize)]
-pub(crate) struct ManifestMap(HashMap<String, Manifest>);
-
-impl ManifestMap {
-  pub fn find_capability(&self, id: &str) -> Option<(String, Capability)> {
-    for (plugin, manifest) in &self.0 {
-      if id == format!("{DEFAULT_CAPABILITY_ID}-{plugin}") {
-        return Some((
-          plugin.clone(),
-          manifest.default_capability.clone().unwrap_or_default(),
-        ));
-      }
-      for capability in &manifest.capabilities {
-        if capability.id == id {
-          return Some((plugin.clone(), capability.clone()));
-        }
-      }
-    }
-
-    None
-  }
-}
-
 pub(crate) fn manifests() -> ManifestMap {
   let mut manifests = HashMap::new();
 
@@ -151,5 +46,5 @@ pub(crate) fn manifests() -> ManifestMap {
     }
   }
 
-  ManifestMap(manifests)
+  manifests.into()
 }

+ 2 - 0
core/tauri-utils/src/lib.rs

@@ -26,7 +26,9 @@ pub mod config;
 pub mod html;
 pub mod io;
 pub mod mime_type;
+pub mod namespace;
 pub mod platform;
+pub mod plugin;
 /// Prepare application resources and sidecars.
 #[cfg(feature = "resources")]
 pub mod resources;

+ 40 - 0
core/tauri-utils/src/namespace.rs

@@ -0,0 +1,40 @@
+// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+//! Namespace lock file and utilities for the runtime authority.
+
+use serde::{Deserialize, Serialize};
+
+use std::collections::HashMap;
+
+use crate::{config::Namespace, plugin::ManifestMap};
+
+/// Resolved data associated with a member.
+#[derive(Deserialize, Serialize)]
+pub struct MemberResolution {
+  /// Member id.
+  pub member: String,
+  /// Resolved capabilities.
+  pub capabilities: HashMap<String, ResolvedCapability>,
+}
+
+/// A resolved capability.
+#[derive(Default, Deserialize, Serialize)]
+pub struct ResolvedCapability {
+  /// List of features enabled.
+  pub features: Vec<String>,
+}
+
+/// Lock file of the namespaces configuration.
+#[derive(Deserialize, Serialize)]
+pub struct NamespaceLockFile {
+  /// Lock file version.
+  pub version: u8,
+  /// Configured namespaces.
+  pub namespaces: Vec<Namespace>,
+  /// Collection of plugins and their manifests.
+  pub plugins: ManifestMap,
+  /// Resolved data.
+  pub resolution: Vec<MemberResolution>,
+}

+ 149 - 0
core/tauri-utils/src/plugin.rs

@@ -0,0 +1,149 @@
+// Copyright 2019-2023 Tauri Programme within The Commons Conservancy
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-License-Identifier: MIT
+
+//! Plugin manifest types.
+
+use serde::{Deserialize, Serialize};
+
+use std::collections::HashMap;
+
+const DEFAULT_CAPABILITY_ID: &str = "default";
+
+/// Scope type definition.
+#[derive(Debug, Serialize, Deserialize)]
+pub enum ScopeType {
+  /// String type.
+  String,
+}
+
+/// Scope of a given capability.
+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
+pub struct CapabilityScope {
+  /// Explicitly allow something.
+  #[serde(default)]
+  pub allowed: Vec<serde_json::Value>,
+  /// Explicitly deny something. Takes precedence over [`Self::allowed`].
+  #[serde(default)]
+  pub blocked: Vec<serde_json::Value>,
+}
+
+/// A capability defines a set of features and scope enabled for the plugin.
+#[derive(Debug, Clone, Default, Serialize, Deserialize)]
+pub struct Capability {
+  /// The identifier of the capability. Must be unique.
+  #[serde(default)]
+  pub id: String,
+  /// Describes the capability in a human readable format.
+  pub description: String,
+  /// List of features enabled by this capability.
+  #[serde(default)]
+  pub features: Vec<String>,
+  /// Scope defined by this capability. Only applies to the given features.
+  #[serde(default)]
+  pub scope: CapabilityScope,
+}
+
+/// Plugin manifest.
+#[derive(Debug, Serialize, Deserialize)]
+pub struct Manifest {
+  /// Plugin name.
+  pub plugin: String,
+  /// Default capability.
+  pub default_capability: Option<Capability>,
+  /// List of capabilities defined by the plugin.
+  pub capabilities: Vec<Capability>,
+  /// List of features defined by the plugin.
+  pub features: Vec<String>,
+  /// Scope types.
+  pub scope_type: Vec<ScopeType>,
+}
+
+impl Manifest {
+  /// Creates a new empty plugin manifest.
+  pub fn new(plugin: impl Into<String>) -> Self {
+    Self {
+      plugin: plugin.into(),
+      default_capability: None,
+      capabilities: Vec::new(),
+      features: Vec::new(),
+      scope_type: Vec::new(),
+    }
+  }
+
+  /// Sets the plugin's default capability set.
+  pub fn default_capability(mut self, default_capability: impl AsRef<str>) -> Self {
+    let mut capability: Capability = serde_json::from_str(default_capability.as_ref())
+      .expect("failed to deserialize default capability");
+    assert!(
+      capability.id.is_empty(),
+      "default capability cannot have an specific identifier"
+    );
+    capability.id = DEFAULT_CAPABILITY_ID.into();
+    self.default_capability.replace(capability);
+    self
+  }
+
+  /// Appends a capability JSON. See [`Capability`].
+  pub fn capability(mut self, capability: impl AsRef<str>) -> Self {
+    let capability: Capability =
+      serde_json::from_str(capability.as_ref()).expect("failed to deserialize default capability");
+    assert!(
+      !capability.id.is_empty(),
+      "capability must have an specific identifier"
+    );
+    self.capabilities.push(capability);
+    self
+  }
+
+  /// Appends the given feature on the list of plugin's features.
+  pub fn feature(mut self, feature: impl Into<String>) -> Self {
+    self.features.push(feature.into());
+    self
+  }
+
+  /// Appends the given list of features.
+  pub fn features<I: IntoIterator<Item = S>, S: Into<String>>(mut self, features: I) -> Self {
+    for feature in features {
+      self = self.feature(feature);
+    }
+    self
+  }
+
+  /// Appends the given scope type.
+  pub fn scope_type(mut self, ty: ScopeType) -> Self {
+    self.scope_type.push(ty);
+    self
+  }
+}
+
+/// A collection mapping a plugin name to its manifest.
+#[derive(Deserialize, Serialize)]
+pub struct ManifestMap(HashMap<String, Manifest>);
+
+impl From<HashMap<String, Manifest>> for ManifestMap {
+  fn from(value: HashMap<String, Manifest>) -> Self {
+    Self(value)
+  }
+}
+
+impl ManifestMap {
+  /// Finds the capability with the given identifier.
+  pub fn find_capability(&self, id: &str) -> Option<(String, Capability)> {
+    for (plugin, manifest) in &self.0 {
+      if id == format!("{DEFAULT_CAPABILITY_ID}-{plugin}") {
+        return Some((
+          plugin.clone(),
+          manifest.default_capability.clone().unwrap_or_default(),
+        ));
+      }
+      for capability in &manifest.capabilities {
+        if capability.id == id {
+          return Some((plugin.clone(), capability.clone()));
+        }
+      }
+    }
+
+    None
+  }
+}