plugin.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright 2019-2023 Tauri Programme within The Commons Conservancy
  2. // SPDX-License-Identifier: Apache-2.0
  3. // SPDX-License-Identifier: MIT
  4. //! Plugin manifest types.
  5. use serde::{Deserialize, Serialize};
  6. use std::{
  7. collections::HashMap,
  8. ops::{Deref, DerefMut},
  9. };
  10. const DEFAULT_CAPABILITY_ID: &str = "default";
  11. /// Scope type definition.
  12. #[derive(Debug, Serialize, Deserialize)]
  13. pub enum ScopeType {
  14. /// String type.
  15. String,
  16. }
  17. /// Scope of a given capability.
  18. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  19. pub struct CapabilityScope {
  20. /// Explicitly allow something.
  21. #[serde(default)]
  22. pub allowed: Vec<serde_json::Value>,
  23. /// Explicitly deny something. Takes precedence over [`Self::allowed`].
  24. #[serde(default)]
  25. pub blocked: Vec<serde_json::Value>,
  26. }
  27. /// A capability defines a set of features and scope enabled for the plugin.
  28. #[derive(Debug, Clone, Default, Serialize, Deserialize)]
  29. pub struct Capability {
  30. /// The identifier of the capability. Must be unique.
  31. #[serde(default)]
  32. pub id: String,
  33. /// The component this capability refers to.
  34. ///
  35. /// Currently the possible values are plugin names.
  36. ///
  37. /// When no value is set, it referes to the application itself.
  38. pub component: Option<String>,
  39. /// Describes the capability in a human readable format.
  40. pub description: String,
  41. /// List of features enabled by this capability.
  42. #[serde(default)]
  43. pub features: Vec<String>,
  44. /// Scope defined by this capability. Only applies to the given features.
  45. #[serde(default)]
  46. pub scope: CapabilityScope,
  47. }
  48. /// Plugin manifest.
  49. #[derive(Debug, Serialize, Deserialize)]
  50. pub struct Manifest {
  51. /// Plugin name.
  52. #[serde(skip_serializing_if = "String::is_empty", default)]
  53. pub plugin: String,
  54. /// Default capability.
  55. pub default_capability: Option<Capability>,
  56. /// List of capabilities defined by the plugin.
  57. pub capabilities: Vec<Capability>,
  58. /// List of features defined by the plugin.
  59. pub features: Vec<String>,
  60. /// Scope types.
  61. pub scope_type: Vec<ScopeType>,
  62. }
  63. impl Manifest {
  64. /// Creates a new empty plugin manifest.
  65. pub fn new(plugin: impl Into<String>) -> Self {
  66. Self {
  67. plugin: plugin.into(),
  68. default_capability: None,
  69. capabilities: Vec::new(),
  70. features: Vec::new(),
  71. scope_type: Vec::new(),
  72. }
  73. }
  74. /// Sets the plugin's default capability set.
  75. pub fn default_capability(mut self, default_capability: impl AsRef<str>) -> Self {
  76. let mut capability: Capability = serde_json::from_str(default_capability.as_ref())
  77. .expect("failed to deserialize default capability");
  78. assert!(
  79. capability.id.is_empty(),
  80. "default capability cannot have an identifier"
  81. );
  82. capability.id = DEFAULT_CAPABILITY_ID.into();
  83. self.default_capability.replace(capability);
  84. self
  85. }
  86. /// Appends a capability JSON. See [`Capability`].
  87. pub fn capability(mut self, capability: impl AsRef<str>) -> Self {
  88. let capability: Capability =
  89. serde_json::from_str(capability.as_ref()).expect("failed to deserialize default capability");
  90. assert!(
  91. !capability.id.is_empty(),
  92. "capability must have an identifier"
  93. );
  94. self.capabilities.push(capability);
  95. self
  96. }
  97. /// Appends the given feature on the list of plugin's features.
  98. pub fn feature(mut self, feature: impl Into<String>) -> Self {
  99. self.features.push(feature.into());
  100. self
  101. }
  102. /// Appends the given list of features.
  103. pub fn features<I: IntoIterator<Item = S>, S: Into<String>>(mut self, features: I) -> Self {
  104. for feature in features {
  105. self = self.feature(feature);
  106. }
  107. self
  108. }
  109. /// Appends the given scope type.
  110. pub fn scope_type(mut self, ty: ScopeType) -> Self {
  111. self.scope_type.push(ty);
  112. self
  113. }
  114. }
  115. /// A collection mapping a plugin name to its manifest.
  116. #[derive(Debug, Default, Deserialize, Serialize)]
  117. pub struct ManifestMap(HashMap<String, Manifest>);
  118. impl Deref for ManifestMap {
  119. type Target = HashMap<String, Manifest>;
  120. fn deref(&self) -> &Self::Target {
  121. &self.0
  122. }
  123. }
  124. impl DerefMut for ManifestMap {
  125. fn deref_mut(&mut self) -> &mut Self::Target {
  126. &mut self.0
  127. }
  128. }
  129. impl From<HashMap<String, Manifest>> for ManifestMap {
  130. fn from(value: HashMap<String, Manifest>) -> Self {
  131. Self(value)
  132. }
  133. }
  134. impl ManifestMap {
  135. /// Finds the capability with the given identifier.
  136. pub fn find_capability(&self, id: &str) -> Option<(String, Capability)> {
  137. for (plugin, manifest) in &self.0 {
  138. if id == format!("{DEFAULT_CAPABILITY_ID}-{plugin}") {
  139. return Some((
  140. plugin.clone(),
  141. manifest.default_capability.clone().unwrap_or_default(),
  142. ));
  143. }
  144. for capability in &manifest.capabilities {
  145. if capability.id == id {
  146. return Some((plugin.clone(), capability.clone()));
  147. }
  148. }
  149. }
  150. None
  151. }
  152. }