diff --git a/Cargo.lock b/Cargo.lock index 3311e7ad8..2acc9118e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -984,6 +984,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "indoc" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b248f5224d1d606005e02c97f5aa4e88eeb230488bcc03bc9ca4d7991399f2b5" + [[package]] name = "ipnet" version = "2.9.0" @@ -2950,6 +2956,7 @@ dependencies = [ "chrono", "futures", "http 1.1.0", + "indoc", "parse_link_header", "reqwest", "rstest", diff --git a/wp_api/Cargo.toml b/wp_api/Cargo.toml index 1020f945b..ab1e1a8cd 100644 --- a/wp_api/Cargo.toml +++ b/wp_api/Cargo.toml @@ -10,6 +10,7 @@ name = "wp_api" [dependencies] base64 = { workspace = true } http = { workspace = true } +indoc = "2.0" url = "2.5" parse_link_header = "0.3" serde = { workspace = true } diff --git a/wp_api/src/lib.rs b/wp_api/src/lib.rs index 7089323e0..c9f0f3a47 100644 --- a/wp_api/src/lib.rs +++ b/wp_api/src/lib.rs @@ -21,7 +21,7 @@ mod unit_test_common; const CONTENT_TYPE_JSON: &str = "application/json"; -#[derive(uniffi::Object)] +#[derive(Debug, uniffi::Object)] pub struct WPApiHelper { api_endpoint: ApiEndpoint, site_url: Url, @@ -361,7 +361,7 @@ impl WPAuthentication { } } -#[derive(uniffi::Enum)] +#[derive(Debug, uniffi::Enum)] pub enum RequestMethod { GET, POST, diff --git a/wp_api/src/plugins.rs b/wp_api/src/plugins.rs index 5d7bd0438..429ef53a8 100644 --- a/wp_api/src/plugins.rs +++ b/wp_api/src/plugins.rs @@ -35,7 +35,7 @@ add_uniffi_exported_parser!(parse_create_plugin_response, PluginWithEditContext) add_uniffi_exported_parser!(parse_update_plugin_response, PluginWithEditContext); add_uniffi_exported_parser!(parse_delete_plugin_response, PluginDeleteResponse); -#[derive(Default, Debug, uniffi::Record)] +#[derive(Debug, Default, uniffi::Record)] pub struct PluginListParams { /// Limit results to those matching a string. pub search: Option, diff --git a/wp_api/src/request.rs b/wp_api/src/request.rs index dc3812461..5fff0369a 100644 --- a/wp_api/src/request.rs +++ b/wp_api/src/request.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use std::{collections::HashMap, fmt::Debug}; use url::Url; @@ -6,6 +6,7 @@ use crate::RequestMethod; pub mod endpoint; +// Has custom `Debug` trait implementation #[derive(uniffi::Record)] pub struct WPNetworkRequest { pub method: RequestMethod, @@ -19,6 +20,34 @@ pub struct WPNetworkRequest { pub body: Option>, } +impl WPNetworkRequest { + pub fn body_as_string(&self) -> Option { + self.body.as_ref().map(|b| body_as_string(b)) + } +} + +impl Debug for WPNetworkRequest { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut s = format!( + indoc::indoc! {" + WPNetworkRequest {{ + method: '{:?}', + url: '{}', + header_map: '{:?}', + body: '{:?}' + }} + "}, + self.method, + self.url, + self.header_map, + self.body_as_string() + ); + s.pop(); // Remove the new line at the end + write!(f, "{}", s) + } +} + +// Has custom `Debug` trait implementation #[derive(uniffi::Record)] pub struct WPNetworkResponse { pub body: Vec, @@ -45,7 +74,33 @@ impl WPNetworkResponse { } } } - None } + + pub fn body_as_string(&self) -> String { + body_as_string(&self.body) + } +} + +impl Debug for WPNetworkResponse { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let mut s = format!( + indoc::indoc! {" + WPNetworkResponse {{ + status_code: '{}', + header_map: '{:?}', + body: '{}' + }} + "}, + self.status_code, + self.header_map, + self.body_as_string() + ); + s.pop(); // Remove the new line at the end + write!(f, "{}", s) + } +} + +fn body_as_string(body: &[u8]) -> String { + String::from_utf8_lossy(body).to_string() } diff --git a/wp_api/src/request/endpoint.rs b/wp_api/src/request/endpoint.rs index 32f779191..b9b71b16d 100644 --- a/wp_api/src/request/endpoint.rs +++ b/wp_api/src/request/endpoint.rs @@ -48,6 +48,7 @@ impl ApiBaseUrl { } } +#[derive(Debug)] pub struct ApiEndpoint { pub base_url: ApiBaseUrl, pub users: UsersEndpoint, diff --git a/wp_api/src/request/endpoint/plugins_endpoint.rs b/wp_api/src/request/endpoint/plugins_endpoint.rs index 8e051bb72..a49947178 100644 --- a/wp_api/src/request/endpoint/plugins_endpoint.rs +++ b/wp_api/src/request/endpoint/plugins_endpoint.rs @@ -4,6 +4,7 @@ use crate::{plugins::PluginListParams, PluginSlug, SparsePluginField, WPContext} use super::{ApiBaseUrl, UrlExtension}; +#[derive(Debug)] pub struct PluginsEndpoint { api_base_url: ApiBaseUrl, } diff --git a/wp_api/src/request/endpoint/users_endpoint.rs b/wp_api/src/request/endpoint/users_endpoint.rs index 6027ee3ce..b0939191c 100644 --- a/wp_api/src/request/endpoint/users_endpoint.rs +++ b/wp_api/src/request/endpoint/users_endpoint.rs @@ -4,6 +4,7 @@ use crate::{SparseUserField, UserDeleteParams, UserId, UserListParams, WPContext use super::{ApiBaseUrl, UrlExtension}; +#[derive(Debug)] pub struct UsersEndpoint { api_base_url: ApiBaseUrl, } diff --git a/wp_api/src/users.rs b/wp_api/src/users.rs index a90880ceb..af31bda53 100644 --- a/wp_api/src/users.rs +++ b/wp_api/src/users.rs @@ -91,7 +91,7 @@ impl Default for WPApiParamUsersWho { } } -#[derive(Default, Debug, uniffi::Record)] +#[derive(Debug, Default, uniffi::Record)] pub struct UserListParams { /// Current page of the collection. /// Default: `1` @@ -184,7 +184,7 @@ impl UserListParams { } } -#[derive(Serialize, Debug, uniffi::Record)] +#[derive(Debug, Serialize, uniffi::Record)] pub struct UserCreateParams { /// Login name for the user. pub username: String, @@ -254,7 +254,7 @@ impl UserCreateParams { } } -#[derive(Default, Serialize, uniffi::Record)] +#[derive(Debug, Default, Serialize, uniffi::Record)] pub struct UserUpdateParams { /// Display name for the user. #[serde(skip_serializing_if = "Option::is_none")] diff --git a/wp_api/tests/integration_test_common.rs b/wp_api/tests/integration_test_common.rs index afd011b7a..78ea27f9c 100644 --- a/wp_api/tests/integration_test_common.rs +++ b/wp_api/tests/integration_test_common.rs @@ -106,6 +106,7 @@ impl AssertWpError for Result { } } +#[derive(Debug)] pub struct TestCredentials { pub site_url: String, pub admin_username: String, @@ -177,6 +178,7 @@ where .expect("Failed to restore wp-content/plugins"); } +#[derive(Debug)] pub struct AsyncWPNetworking { client: reqwest::Client, } diff --git a/wp_api/tests/wp_db.rs b/wp_api/tests/wp_db.rs index d22104320..7efec9bcb 100644 --- a/wp_api/tests/wp_db.rs +++ b/wp_api/tests/wp_db.rs @@ -17,6 +17,7 @@ where f(WordPressDb { conn }).await } +#[derive(Debug)] pub struct WordPressDb { conn: MySqlConnection, }