Skip to content

Commit

Permalink
wip: new command traits and generated code
Browse files Browse the repository at this point in the history
  • Loading branch information
chippers committed Dec 18, 2023
1 parent 77ae621 commit e60d8a9
Show file tree
Hide file tree
Showing 10 changed files with 310 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/tauri-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ pub fn generate_context(items: TokenStream) -> TokenStream {
#[proc_macro_attribute]
pub fn default_runtime(attributes: TokenStream, input: TokenStream) -> TokenStream {
let attributes = parse_macro_input!(attributes as runtime::Attributes);
let input = parse_macro_input!(input as DeriveInput);
let input = parse_macro_input!(input as runtime::Input);
runtime::default_runtime(attributes, input).into()
}

Expand Down
43 changes: 37 additions & 6 deletions core/tauri-macros/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,42 @@
// SPDX-License-Identifier: MIT

use proc_macro2::TokenStream;
use quote::quote;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{parse_quote, DeriveInput, GenericParam, Ident, Token, Type, TypeParam};
use syn::{parse_quote, DeriveInput, GenericParam, Ident, ItemTrait, Token, Type, TypeParam};

#[derive(Clone)]
pub(crate) enum Input {
Derive(DeriveInput),
Trait(ItemTrait),
}

impl Parse for Input {
fn parse(input: ParseStream) -> syn::Result<Self> {
input
.parse::<DeriveInput>()
.map(Self::Derive)
.or_else(|_| input.parse::<ItemTrait>().map(Self::Trait))
}
}

impl Input {
fn last_param_mut(&mut self) -> Option<&mut GenericParam> {
match self {
Input::Derive(d) => d.generics.params.last_mut(),
Input::Trait(t) => t.generics.params.last_mut(),
}
}
}

impl ToTokens for Input {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Input::Derive(d) => d.to_tokens(tokens),
Input::Trait(t) => t.to_tokens(tokens),
}
}
}

/// The default runtime type to enable when the provided feature is enabled.
pub(crate) struct Attributes {
Expand All @@ -24,13 +57,11 @@ impl Parse for Attributes {
}
}

pub(crate) fn default_runtime(attributes: Attributes, input: DeriveInput) -> TokenStream {
pub(crate) fn default_runtime(attributes: Attributes, input: Input) -> TokenStream {
// create a new copy to manipulate for the wry feature flag
let mut wry = input.clone();
let wry_runtime = wry
.generics
.params
.last_mut()
.last_param_mut()
.expect("default_runtime requires the item to have at least 1 generic parameter");

// set the default value of the last generic parameter to the provided runtime type
Expand Down
3 changes: 2 additions & 1 deletion core/tauri-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ getrandom = { version = "0.2", optional = true, features = [ "std" ] }
serialize-to-javascript = { version = "=0.1.1", optional = true }
ctor = "0.2"
json5 = { version = "0.4", optional = true }
toml = { version = "0.8", default-features = true }
toml = { version = "0.8", features = ["parse"] }
json-patch = "1.2"
glob = { version = "0.3", optional = true }
walkdir = { version = "2", optional = true }
Expand All @@ -39,6 +39,7 @@ semver = "1"
infer = "0.15"
dunce = "1"
log = "0.4.20"
matchit = "0.7"

[target."cfg(target_os = \"linux\")".dependencies]
heck = "0.4"
Expand Down
35 changes: 35 additions & 0 deletions core/tauri-utils/src/acl/capability.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::acl::PermissionId;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CapabilityId {
inner: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]

pub struct CapabilitySet {
inner: Vec<Capability>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Capability {
identifier: CapabilityId,
description: String,
#[serde(default)]
context: CapabilityContext,
windows: Vec<String>,
permissions: Vec<PermissionId>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CapabilityContext {
Local,
Remote { dangerous_remote: Vec<String> },
}

impl Default for CapabilityContext {
fn default() -> Self {
Self::Local
}
}
12 changes: 10 additions & 2 deletions core/tauri-utils/src/acl/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use serde::{Deserialize, Serialize};
use std::num::NonZeroU64;

pub use self::{identifier::*, value::*};

mod identifier;
pub mod capability;
pub mod identifier;
pub mod plugin;
mod value;
pub mod resolved;
pub mod value;

/// Allowed and denied commands inside a permission.
///
Expand Down Expand Up @@ -42,6 +45,11 @@ pub struct InlinedPermission {
scopes: Scopes,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PermissionId {
inner: String,
}

/// A permission.
#[derive(Debug)]
pub struct Permission {
Expand Down
45 changes: 45 additions & 0 deletions core/tauri-utils/src/acl/resolved.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use matchit::Router;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// TODO: the current proof of concept uses [`matchit`], but we should use a simplified
/// router router that __only__ supports static and wildcard values (without wildcard params)
pub struct WindowMatcher {
inner: Router<String>,
}

pub struct WindowMatch {
inner: BTreeMap<String, ResolvedCommand>,
}

pub enum ResolvedCommand {
Deny,
Allow(ResolvedScopes<serde_json::Value>),
}

pub enum ScopeKind<'de, T>
where
T: Serialize + Deserialize<'de>,
{
Allow(&'de T),
Deny(&'de T),
}

pub struct ResolvedScopes<T>
where
T: Serialize,
for<'de> T: Deserialize<'de>,
{
allow: Vec<T>,
deny: Vec<T>,
}

impl<T> ResolvedScopes<T>
where
T: Serialize + DeserializeOwned,
for<'de> T: Deserialize<'de>,
{
}

pub struct Resolved {}
18 changes: 16 additions & 2 deletions core/tauri-utils/src/acl/value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::collections::BTreeMap;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

/// A valid ACL number.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialOrd, PartialEq)]
#[serde(untagged)]
pub enum Number {
/// Represents an [`i64`].
Int(i64),
Expand All @@ -25,7 +29,8 @@ impl From<f64> for Number {
}

/// All supported ACL values.
#[derive(Debug)]
#[derive(Debug, Serialize, Deserialize, Clone, PartialOrd, PartialEq)]
#[serde(untagged)]
pub enum Value {
/// Represents a [`bool`].
Bool(bool),
Expand All @@ -43,6 +48,15 @@ pub enum Value {
Map(BTreeMap<String, Value>),
}

impl Value {
/// TODO: implement [`serde::Deserializer`] directly to avoid serializing then deserializing
pub fn deserialize<T: DeserializeOwned>(&self) -> Option<T> {
toml::to_string(self)
.ok()
.and_then(|s| toml::from_str(&s).ok())
}
}

impl From<bool> for Value {
#[inline(always)]
fn from(value: bool) -> Self {
Expand Down
1 change: 1 addition & 0 deletions core/tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ infer = { version = "0.15", optional = true }
png = { version = "0.17", optional = true }
ico = { version = "0.3.0", optional = true }
http-range = { version = "0.1.5", optional = true }
static_assertions = "1.1.0"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\", target_os = \"windows\", target_os = \"macos\"))".dependencies]
muda = { version = "0.11", default-features = false, features = [ "serde" ] }
Expand Down
Loading

0 comments on commit e60d8a9

Please sign in to comment.