Skip to content

feat: add ScopeObjectMatch trait for easy scope validation#11132

Merged
amrbashir merged 6 commits intodevfrom
feat/scope-object-matches
Sep 25, 2024
Merged

feat: add ScopeObjectMatch trait for easy scope validation#11132
amrbashir merged 6 commits intodevfrom
feat/scope-object-matches

Conversation

@chippers
Copy link
Member

This is a DX improvement for developers that are adding scopes to their commands (likely in plugins).

If the validation of a scope can be represented as a bool, the type can implement ScopeObjectMatch to enable a helper method on CommandScope that automatically checks that all deny scopes are not matched and enforcing allow scopes if they exist.

turns code like:

 #[command]
 pub(crate) async fn read_text<R: Runtime>(
     scope: CommandScope<Scope>,
     clipboard: State<'_, Clipboard<R>>,
 ) -> Result<String> {
    let text = clipboard.read_text()?;

     // first make sure the text doesn't match any existing deny scope
     if Scope::any(scope.denies(), &text) {
         return Err(crate::error::Error::Scope);
     }

     // ensure the text matches at least 1 allow scope, only if they exist
     let allow = scope.allows();
     if !allow.is_empty() && !Scope::any(allow, &text) {
         return Err(crate::error::Error::Scope);
     }

     Ok(text)
 }

into

#[command]
pub(crate) async fn read_text<R: Runtime>(
    scope: CommandScope<Scope>,
    clipboard: State<'_, Clipboard<R>>,
) -> Result<String> {
    let text = clipboard.read_text()?;
    if scope.matches(&text) {
        Ok(text)
    } else {
        Err(crate::error::Error::Scope)
    }
}

which removes the chances for developers to make simple logic errors when validating the deny and allow scopes - especially if they are using it multiple times in many commands.

@chippers chippers requested a review from tweidinger September 25, 2024 14:34
@chippers chippers self-assigned this Sep 25, 2024
@chippers chippers requested a review from a team as a code owner September 25, 2024 14:34
@github-actions
Copy link
Contributor

github-actions bot commented Sep 25, 2024

Package Changes Through 6729c30

There are 10 changes which include @tauri-apps/api with prerelease, tauri-bundler with prerelease, tauri-cli with prerelease, tauri with prerelease, tauri-runtime-wry with prerelease, tauri-runtime with prerelease, tauri-utils with prerelease, @tauri-apps/cli with prerelease, tauri-build with prerelease, tauri-codegen with prerelease

Planned Package Versions

The following package releases are the planned based on the context of changes in this pull request.

package current next
@tauri-apps/api 2.0.0-rc.5 2.0.0-rc.6
tauri-utils 2.0.0-rc.12 2.0.0-rc.13
tauri-bundler 2.0.1-rc.13 2.0.1-rc.14
tauri-runtime 2.0.0-rc.12 2.0.0-rc.13
tauri-runtime-wry 2.0.0-rc.13 2.0.0-rc.14
tauri-codegen 2.0.0-rc.12 2.0.0-rc.13
tauri-macros 2.0.0-rc.11 2.0.0-rc.12
tauri-plugin 2.0.0-rc.12 2.0.0-rc.13
tauri-build 2.0.0-rc.12 2.0.0-rc.13
tauri 2.0.0-rc.15 2.0.0-rc.16
@tauri-apps/cli 2.0.0-rc.16 2.0.0-rc.17
tauri-cli 2.0.0-rc.16 2.0.0-rc.17

Add another change file through the GitHub UI by following this link.


Read about change files or the docs at github.com/jbolda/covector

@chippers chippers marked this pull request as draft September 25, 2024 14:36
@chippers chippers marked this pull request as ready for review September 25, 2024 15:42
@amrbashir amrbashir merged commit 5621174 into dev Sep 25, 2024
@amrbashir amrbashir changed the title add ScopeObjectMatch for easy scope validation feat: add ScopeObjectMatch trait for easy scope validation Sep 25, 2024
@amrbashir amrbashir deleted the feat/scope-object-matches branch September 25, 2024 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants