mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-06-06 13:53:54 +02:00
feat(fs): enhance scope config to accept string as well (#1053)
* feat(fs): enhance scope config to accept string as well * clippy * Update protocol.rs * regression resolving path variables * add change file --------- Co-authored-by: Lucas Nogueira <lucas@tauri.app>
This commit is contained in:
@@ -15,11 +15,11 @@ rustdoc-args = [ "--cfg", "docsrs" ]
|
||||
[build-dependencies]
|
||||
tauri-plugin = { workspace = true, features = [ "build" ] }
|
||||
schemars = { workspace = true }
|
||||
serde = { workspace = true }
|
||||
|
||||
[dependencies]
|
||||
serde = { workspace = true }
|
||||
serde_json = { workspace = true }
|
||||
schemars = { workspace = true }
|
||||
serde_repr = "0.1"
|
||||
tauri = { workspace = true }
|
||||
thiserror = { workspace = true }
|
||||
|
||||
+28
-2
@@ -2,12 +2,38 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use std::{fs::create_dir_all, path::Path};
|
||||
use std::{
|
||||
fs::create_dir_all,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
#[path = "src/scope.rs"]
|
||||
#[allow(dead_code)]
|
||||
mod scope;
|
||||
|
||||
/// FS scope entry.
|
||||
#[derive(schemars::JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
#[allow(unused)]
|
||||
enum FsScopeEntry {
|
||||
/// FS scope path.
|
||||
Value(PathBuf),
|
||||
Object {
|
||||
/// FS scope path.
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
// Ensure scope entry is kept up to date
|
||||
impl From<FsScopeEntry> for scope::EntryRaw {
|
||||
fn from(value: FsScopeEntry) -> Self {
|
||||
match value {
|
||||
FsScopeEntry::Value(path) => scope::EntryRaw::Value(path),
|
||||
FsScopeEntry::Object { path } => scope::EntryRaw::Object { path },
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const BASE_DIR_VARS: &[&str] = &[
|
||||
"AUDIO",
|
||||
"CACHE",
|
||||
@@ -163,6 +189,6 @@ permissions = [
|
||||
|
||||
tauri_plugin::Builder::new(COMMANDS)
|
||||
.global_api_script_path("./api-iife.js")
|
||||
.global_scope_schema(schemars::schema_for!(scope::Entry))
|
||||
.global_scope_schema(schemars::schema_for!(FsScopeEntry))
|
||||
.build();
|
||||
}
|
||||
|
||||
+22
-22
@@ -11,9 +11,6 @@
|
||||
html_favicon_url = "https://github.com/tauri-apps/tauri/raw/dev/app-icon.png"
|
||||
)]
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use serde::Deserialize;
|
||||
use tauri::{
|
||||
ipc::ScopeObject,
|
||||
plugin::{Builder as PluginBuilder, TauriPlugin},
|
||||
@@ -33,6 +30,28 @@ pub use scope::{Event as ScopeEvent, Scope};
|
||||
|
||||
type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
// implement ScopeObject here instead of in the scope module because it is also used on the build script
|
||||
// and we don't want to add tauri as a build dependency
|
||||
impl ScopeObject for scope::Entry {
|
||||
type Error = Error;
|
||||
fn deserialize<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
raw: Value,
|
||||
) -> std::result::Result<Self, Self::Error> {
|
||||
let entry = serde_json::from_value(raw.into()).map(|raw| {
|
||||
let path = match raw {
|
||||
scope::EntryRaw::Value(path) => path,
|
||||
scope::EntryRaw::Object { path } => path,
|
||||
};
|
||||
Self { path }
|
||||
})?;
|
||||
|
||||
Ok(Self {
|
||||
path: app.path().parse(entry.path)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FsExt<R: Runtime> {
|
||||
fn fs_scope(&self) -> &Scope;
|
||||
fn try_fs_scope(&self) -> Option<&Scope>;
|
||||
@@ -48,25 +67,6 @@ impl<R: Runtime, T: Manager<R>> FsExt<R> for T {
|
||||
}
|
||||
}
|
||||
|
||||
impl ScopeObject for scope::Entry {
|
||||
type Error = Error;
|
||||
fn deserialize<R: Runtime>(
|
||||
app: &AppHandle<R>,
|
||||
raw: Value,
|
||||
) -> std::result::Result<Self, Self::Error> {
|
||||
#[derive(Deserialize)]
|
||||
struct EntryRaw {
|
||||
path: PathBuf,
|
||||
}
|
||||
|
||||
let entry = serde_json::from_value::<EntryRaw>(raw.into())?;
|
||||
|
||||
Ok(Self {
|
||||
path: app.path().parse(entry.path)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R, Option<config::Config>> {
|
||||
PluginBuilder::<R, Option<config::Config>>::new("fs")
|
||||
.invoke_handler(tauri::generate_handler![
|
||||
|
||||
+11
-1
@@ -11,7 +11,17 @@ use std::{
|
||||
},
|
||||
};
|
||||
|
||||
#[derive(Debug, schemars::JsonSchema)]
|
||||
use serde::Deserialize;
|
||||
|
||||
#[doc(hidden)]
|
||||
#[derive(Deserialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum EntryRaw {
|
||||
Value(PathBuf),
|
||||
Object { path: PathBuf },
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Entry {
|
||||
pub path: PathBuf,
|
||||
}
|
||||
|
||||
+9
-10
@@ -8,11 +8,11 @@ mod scope;
|
||||
|
||||
const COMMANDS: &[&str] = &["fetch", "fetch_cancel", "fetch_send", "fetch_read_body"];
|
||||
|
||||
/// HTTP scope entry object definition.
|
||||
#[allow(unused)]
|
||||
/// HTTP scope entry.
|
||||
#[derive(schemars::JsonSchema)]
|
||||
#[serde(untagged)]
|
||||
enum ScopeEntry {
|
||||
#[allow(unused)]
|
||||
enum HttpScopeEntry {
|
||||
/// A URL that can be accessed by the webview when using the HTTP APIs.
|
||||
/// Wildcards can be used following the URL pattern standard.
|
||||
///
|
||||
@@ -28,7 +28,6 @@ enum ScopeEntry {
|
||||
///
|
||||
/// - "https://myapi.service.com/users/*": allows access to any URLs that begins with "https://myapi.service.com/users/"
|
||||
Value(String),
|
||||
|
||||
Object {
|
||||
/// A URL that can be accessed by the webview when using the HTTP APIs.
|
||||
/// Wildcards can be used following the URL pattern standard.
|
||||
@@ -48,12 +47,12 @@ enum ScopeEntry {
|
||||
},
|
||||
}
|
||||
|
||||
// ensure scope entry is up to date
|
||||
impl From<ScopeEntry> for scope::Entry {
|
||||
fn from(value: ScopeEntry) -> Self {
|
||||
// Ensure scope entry is kept up to date
|
||||
impl From<HttpScopeEntry> for scope::Entry {
|
||||
fn from(value: HttpScopeEntry) -> Self {
|
||||
let url = match value {
|
||||
ScopeEntry::Value(url) => url,
|
||||
ScopeEntry::Object { url } => url,
|
||||
HttpScopeEntry::Value(url) => url,
|
||||
HttpScopeEntry::Object { url } => url,
|
||||
};
|
||||
|
||||
scope::Entry {
|
||||
@@ -69,6 +68,6 @@ impl From<ScopeEntry> for scope::Entry {
|
||||
fn main() {
|
||||
tauri_plugin::Builder::new(COMMANDS)
|
||||
.global_api_script_path("./api-iife.js")
|
||||
.global_scope_schema(schemars::schema_for!(ScopeEntry))
|
||||
.global_scope_schema(schemars::schema_for!(HttpScopeEntry))
|
||||
.build();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user