use a hashset

This commit is contained in:
Lucas Nogueira 2024-06-26 15:54:58 -03:00
parent 68b31edd4a
commit 778d316f34
No known key found for this signature in database
GPG Key ID: FFEA6C72E73482F1
4 changed files with 27 additions and 24 deletions

View File

@ -0,0 +1,5 @@
---
"tauri-utils": patch:breaking
---
`acl::Capability::permissions` is now a `HashSet` instead of a `Vec`.

View File

@ -4,7 +4,7 @@
//! End-user abstraction for selecting permissions a window has access to.
use std::{path::Path, str::FromStr};
use std::{collections::HashSet, hash::Hash, path::Path, str::FromStr};
use crate::{acl::Identifier, platform::Target};
use serde::{Deserialize, Serialize};
@ -13,7 +13,7 @@ use super::Scopes;
/// An entry for a permission value in a [`Capability`] can be either a raw permission [`Identifier`]
/// or an object that references a permission and extends its scope.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub enum PermissionEntry {
@ -29,6 +29,20 @@ pub enum PermissionEntry {
},
}
impl PartialEq for PermissionEntry {
fn eq(&self, other: &Self) -> bool {
self.identifier() == other.identifier()
}
}
impl Eq for PermissionEntry {}
impl Hash for PermissionEntry {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.identifier().hash(state);
}
}
impl PermissionEntry {
/// The identifier of the permission referenced in this entry.
pub fn identifier(&self) -> &Identifier {
@ -157,8 +171,7 @@ pub struct Capability {
/// "allow": [{ "path": "$HOME/test.txt" }]
/// }
/// ```
#[cfg_attr(feature = "schema", schemars(schema_with = "unique_permission"))]
pub permissions: Vec<PermissionEntry>,
pub permissions: HashSet<PermissionEntry>,
/// Limit which target platforms this capability applies to.
///
/// By default all platforms are targeted.
@ -170,21 +183,6 @@ pub struct Capability {
pub platforms: Option<Vec<Target>>,
}
#[cfg(feature = "schema")]
fn unique_permission(gen: &mut schemars::gen::SchemaGenerator) -> schemars::schema::Schema {
use schemars::schema;
schema::SchemaObject {
instance_type: Some(schema::InstanceType::Array.into()),
array: Some(Box::new(schema::ArrayValidation {
unique_items: Some(true),
items: Some(gen.subschema_for::<PermissionEntry>().into()),
..Default::default()
})),
..Default::default()
}
.into()
}
fn default_capability_local() -> bool {
true
}

View File

@ -17,7 +17,7 @@ const MAX_LEN_BASE: usize = 64;
const MAX_LEN_IDENTIFIER: usize = MAX_LEN_PREFIX + 1 + MAX_LEN_BASE;
/// Plugin identifier.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Identifier {
inner: String,
separator: Option<NonZeroU8>,

View File

@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashSet};
use std::fmt::{Debug, Display};
use std::sync::Arc;
@ -93,7 +93,7 @@ impl CapabilityBuilder {
local: true,
windows: Vec::new(),
webviews: Vec::new(),
permissions: Vec::new(),
permissions: HashSet::new(),
platforms: None,
})
}
@ -145,7 +145,7 @@ impl CapabilityBuilder {
/// Add a new permission to this capability.
pub fn permission(mut self, permission: impl Into<String>) -> Self {
let permission = permission.into();
self.0.permissions.push(PermissionEntry::PermissionRef(
self.0.permissions.insert(PermissionEntry::PermissionRef(
permission
.clone()
.try_into()
@ -191,7 +191,7 @@ impl CapabilityBuilder {
self
.0
.permissions
.push(PermissionEntry::ExtendedPermission { identifier, scope });
.insert(PermissionEntry::ExtendedPermission { identifier, scope });
self
}