瀏覽代碼

refactor!(core): return result in `Webview/WebviewWindow::url` getter (#9647)

* refactor!(core): return result in `Webview/WebviewWindow::url` getter

* clippy

* Apply suggestions from code review
Amr Bashir 1 年之前
父節點
當前提交
783ef0f2d3

+ 6 - 0
.changes/url-result-runtime.md

@@ -0,0 +1,6 @@
+---
+"tauri-runtime": "patch"
+"tauri-runtime-wry": "patch"
+---
+
+Changed `WebviewDispatch::url` getter to return a result.

+ 5 - 0
.changes/url-result.md

@@ -0,0 +1,5 @@
+---
+"tauri": "patch:breaking"
+---
+
+Changed `WebviewWindow::url` and `Webview::url` getter to return a result.

+ 2 - 2
core/tauri-runtime-wry/src/lib.rs

@@ -1190,7 +1190,7 @@ pub enum WebviewMessage {
   SetAutoResize(bool),
   SetZoom(f64),
   // Getters
-  Url(Sender<Result<Url>>),
+  Url(Sender<Result<String>>),
   Bounds(Sender<Result<tauri_runtime::Rect>>),
   Position(Sender<Result<PhysicalPosition<i32>>>),
   Size(Sender<Result<PhysicalSize<u32>>>),
@@ -1305,7 +1305,7 @@ impl<T: UserEvent> WebviewDispatch<T> for WryWebviewDispatcher<T> {
 
   // Getters
 
-  fn url(&self) -> Result<Url> {
+  fn url(&self) -> Result<String> {
     webview_getter!(self, WebviewMessage::Url)?
   }
 

+ 1 - 1
core/tauri-runtime/src/lib.rs

@@ -445,7 +445,7 @@ pub trait WebviewDispatch<T: UserEvent>: Debug + Clone + Send + Sync + Sized + '
   // GETTERS
 
   /// Returns the webview's current URL.
-  fn url(&self) -> Result<Url>;
+  fn url(&self) -> Result<String>;
 
   /// Returns the webview's bounds.
   fn bounds(&self) -> Result<Rect>;

+ 2 - 7
core/tauri/src/test/mock_runtime.rs

@@ -502,13 +502,8 @@ impl<T: UserEvent> WebviewDispatch<T> for MockWebviewDispatcher {
     Ok(())
   }
 
-  fn url(&self) -> Result<url::Url> {
-    self
-      .url
-      .lock()
-      .unwrap()
-      .parse()
-      .map_err(|_| Error::FailedToReceiveMessage)
+  fn url(&self) -> Result<String> {
+    Ok(self.url.lock().unwrap().clone())
   }
 
   fn bounds(&self) -> Result<tauri_runtime::Rect> {

+ 6 - 3
core/tauri/src/webview/mod.rs

@@ -1073,9 +1073,12 @@ fn main() {
   }
 
   /// Returns the current url of the webview.
-  // TODO: in v2, change this type to Result
-  pub fn url(&self) -> Url {
-    self.webview.dispatcher.url().unwrap()
+  pub fn url(&self) -> crate::Result<Url> {
+    self
+      .webview
+      .dispatcher
+      .url()
+      .map(|url| url.parse().map_err(crate::Error::InvalidUrl))?
   }
 
   /// Navigates the webview to the defined url.

+ 1 - 2
core/tauri/src/webview/webview_window.rs

@@ -1605,8 +1605,7 @@ impl<R: Runtime> WebviewWindow<R> {
   }
 
   /// Returns the current url of the webview.
-  // TODO: in v2, change this type to Result
-  pub fn url(&self) -> Url {
+  pub fn url(&self) -> crate::Result<Url> {
     self.webview.url()
   }
 

+ 22 - 0
examples/api/src-tauri/Cargo.lock

@@ -4002,6 +4002,17 @@ dependencies = [
  "syn 2.0.61",
 ]
 
+[[package]]
+name = "windows-implement"
+version = "0.56.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f6fc35f58ecd95a9b71c4f2329b911016e6bec66b3f2e6a4aad86bd2e99e2f9b"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.52",
+]
+
 [[package]]
 name = "windows-interface"
 version = "0.56.0"
@@ -4013,6 +4024,17 @@ dependencies = [
  "syn 2.0.61",
 ]
 
+[[package]]
+name = "windows-interface"
+version = "0.56.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08990546bf4edef8f431fa6326e032865f27138718c587dc21bc0265bbcb57cc"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn 2.0.52",
+]
+
 [[package]]
 name = "windows-result"
 version = "0.1.1"