Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 501ab47

Browse files
committedMay 21, 2024
Adds PluginSlug & PluginWpOrgDirectorySlug
1 parent 280d5cc commit 501ab47

File tree

5 files changed

+74
-39
lines changed

5 files changed

+74
-39
lines changed
 

‎wp_api/src/endpoint/plugins_endpoint.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use url::Url;
22

3-
use crate::{plugins::PluginListParams, ApiBaseUrl, WPContext};
3+
use crate::{plugins::PluginListParams, ApiBaseUrl, PluginSlug, WPContext};
44

55
pub struct PluginsEndpoint {
66
api_base_url: ApiBaseUrl,
@@ -15,7 +15,7 @@ impl PluginsEndpoint {
1515
self.plugins_base_url()
1616
}
1717

18-
pub fn delete(&self, plugin: &str) -> Url {
18+
pub fn delete(&self, plugin: &PluginSlug) -> Url {
1919
self.plugins_url_with_slug(plugin)
2020
}
2121

@@ -29,25 +29,25 @@ impl PluginsEndpoint {
2929
url
3030
}
3131

32-
pub fn retrieve(&self, context: WPContext, plugin: &str) -> Url {
32+
pub fn retrieve(&self, context: WPContext, plugin: &PluginSlug) -> Url {
3333
let mut url = self.plugins_url_with_slug(plugin);
3434
url.query_pairs_mut()
3535
.append_pair("context", context.as_str());
3636
url
3737
}
3838

39-
pub fn update(&self, plugin: &str) -> Url {
39+
pub fn update(&self, plugin: &PluginSlug) -> Url {
4040
self.plugins_url_with_slug(plugin)
4141
}
4242

4343
fn plugins_base_url(&self) -> Url {
4444
self.api_base_url.by_appending("plugins")
4545
}
4646

47-
fn plugins_url_with_slug(&self, plugin: &str) -> Url {
47+
fn plugins_url_with_slug(&self, plugin: &PluginSlug) -> Url {
4848
self.api_base_url
4949
// The '/' character has to be preserved and not get encoded
50-
.by_extending(["plugins"].into_iter().chain(plugin.split('/')))
50+
.by_extending(["plugins"].into_iter().chain(plugin.slug.split('/')))
5151
}
5252
}
5353

@@ -66,19 +66,19 @@ mod tests {
6666
}
6767

6868
#[rstest]
69-
#[case("hello-dolly/hello", "/plugins/hello-dolly/hello")]
69+
#[case("hello-dolly/hello".into(), "/plugins/hello-dolly/hello")]
7070
#[case(
71-
"classic-editor/classic-editor",
71+
"classic-editor/classic-editor".into(),
7272
"/plugins/classic-editor/classic-editor"
7373
)]
74-
#[case("foo/bar%baz", "/plugins/foo/bar%25baz")]
75-
#[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")]
74+
#[case("foo/bar%baz".into(), "/plugins/foo/bar%25baz")]
75+
#[case("foo/です".into(), "/plugins/foo/%E3%81%A7%E3%81%99")]
7676
fn delete_plugin(
7777
plugins_endpoint: PluginsEndpoint,
78-
#[case] plugin_slug: &str,
78+
#[case] plugin_slug: PluginSlug,
7979
#[case] expected_path: &str,
8080
) {
81-
validate_endpoint(plugins_endpoint.delete(plugin_slug), expected_path);
81+
validate_endpoint(plugins_endpoint.delete(&plugin_slug), expected_path);
8282
}
8383

8484
#[rstest]
@@ -96,47 +96,47 @@ mod tests {
9696

9797
#[rstest]
9898
#[case(
99-
"hello-dolly/hello",
99+
"hello-dolly/hello".into(),
100100
WPContext::View,
101101
"/plugins/hello-dolly/hello?context=view"
102102
)]
103103
#[case(
104-
"classic-editor/classic-editor",
104+
"classic-editor/classic-editor".into(),
105105
WPContext::Embed,
106106
"/plugins/classic-editor/classic-editor?context=embed"
107107
)]
108-
#[case("foo/bar%baz", WPContext::Edit, "/plugins/foo/bar%25baz?context=edit")]
108+
#[case("foo/bar%baz".into(), WPContext::Edit, "/plugins/foo/bar%25baz?context=edit")]
109109
#[case(
110-
"foo/です",
110+
"foo/です".into(),
111111
WPContext::View,
112112
"/plugins/foo/%E3%81%A7%E3%81%99?context=view"
113113
)]
114114
fn retrieve_plugin(
115115
plugins_endpoint: PluginsEndpoint,
116-
#[case] plugin_slug: &str,
116+
#[case] plugin_slug: PluginSlug,
117117
#[case] context: WPContext,
118118
#[case] expected_path: &str,
119119
) {
120120
validate_endpoint(
121-
plugins_endpoint.retrieve(context, plugin_slug),
121+
plugins_endpoint.retrieve(context, &plugin_slug),
122122
expected_path,
123123
);
124124
}
125125

126126
#[rstest]
127-
#[case("hello-dolly/hello", "/plugins/hello-dolly/hello")]
127+
#[case("hello-dolly/hello".into(), "/plugins/hello-dolly/hello")]
128128
#[case(
129-
"classic-editor/classic-editor",
129+
"classic-editor/classic-editor".into(),
130130
"/plugins/classic-editor/classic-editor"
131131
)]
132-
#[case("foo/bar%baz", "/plugins/foo/bar%25baz")]
133-
#[case("foo/です", "/plugins/foo/%E3%81%A7%E3%81%99")]
132+
#[case("foo/bar%baz".into(), "/plugins/foo/bar%25baz")]
133+
#[case("foo/です".into(), "/plugins/foo/%E3%81%A7%E3%81%99")]
134134
fn update_plugin(
135135
plugins_endpoint: PluginsEndpoint,
136-
#[case] plugin_slug: &str,
136+
#[case] plugin_slug: PluginSlug,
137137
#[case] expected_path: &str,
138138
) {
139-
validate_endpoint(plugins_endpoint.update(plugin_slug), expected_path);
139+
validate_endpoint(plugins_endpoint.update(&plugin_slug), expected_path);
140140
}
141141

142142
#[fixture]

‎wp_api/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,11 @@ impl WPApiHelper {
251251
}
252252
}
253253

254-
pub fn retrieve_plugin_request(&self, context: WPContext, plugin: &str) -> WPNetworkRequest {
254+
pub fn retrieve_plugin_request(
255+
&self,
256+
context: WPContext,
257+
plugin: &PluginSlug,
258+
) -> WPNetworkRequest {
255259
WPNetworkRequest {
256260
method: RequestMethod::GET,
257261
url: self.api_endpoint.plugins.retrieve(context, plugin).into(),
@@ -262,7 +266,7 @@ impl WPApiHelper {
262266

263267
pub fn update_plugin_request(
264268
&self,
265-
plugin: &str,
269+
plugin: &PluginSlug,
266270
params: PluginUpdateParams,
267271
) -> WPNetworkRequest {
268272
WPNetworkRequest {
@@ -273,7 +277,7 @@ impl WPApiHelper {
273277
}
274278
}
275279

276-
pub fn delete_plugin_request(&self, plugin: &str) -> WPNetworkRequest {
280+
pub fn delete_plugin_request(&self, plugin: &PluginSlug) -> WPNetworkRequest {
277281
WPNetworkRequest {
278282
method: RequestMethod::DELETE,
279283
url: self.api_endpoint.plugins.delete(plugin).into(),

‎wp_api/src/plugins.rs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl PluginListParams {
5454
#[derive(Debug, Serialize, uniffi::Record)]
5555
pub struct PluginCreateParams {
5656
/// WordPress.org plugin directory slug.
57-
pub slug: String,
57+
pub slug: PluginWpOrgDirectorySlug,
5858
/// The plugin activation status.
5959
pub status: PluginStatus,
6060
}
@@ -72,7 +72,7 @@ pub struct PluginUpdateParams {
7272
#[derive(Debug, Serialize, Deserialize, uniffi::Record, WPContextual)]
7373
pub struct SparsePlugin {
7474
#[WPContext(edit, embed, view)]
75-
pub plugin: Option<String>,
75+
pub plugin: Option<PluginSlug>,
7676
#[WPContext(edit, embed, view)]
7777
pub status: Option<PluginStatus>,
7878
#[WPContext(edit, embed, view)]
@@ -99,6 +99,34 @@ pub struct PluginDeleteResponse {
9999
pub previous: PluginWithEditContext,
100100
}
101101

102+
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
103+
#[serde(transparent)]
104+
pub struct PluginSlug {
105+
pub slug: String,
106+
}
107+
108+
impl From<&str> for PluginSlug {
109+
fn from(value: &str) -> Self {
110+
Self {
111+
slug: value.to_string(),
112+
}
113+
}
114+
}
115+
116+
#[derive(Debug, Serialize, Deserialize, uniffi::Record)]
117+
#[serde(transparent)]
118+
pub struct PluginWpOrgDirectorySlug {
119+
pub slug: String,
120+
}
121+
122+
impl From<&str> for PluginWpOrgDirectorySlug {
123+
fn from(value: &str) -> Self {
124+
Self {
125+
slug: value.to_string(),
126+
}
127+
}
128+
}
129+
102130
#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, uniffi::Enum)]
103131
pub enum PluginStatus {
104132
#[serde(rename = "active")]

‎wp_networking/tests/test_plugins_immut.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rstest::*;
2-
use wp_api::{generate, plugins::PluginListParams, plugins::PluginStatus, WPContext};
2+
use wp_api::{generate, plugins::PluginListParams, plugins::PluginStatus, PluginSlug, WPContext};
33

44
use crate::test_helpers::{
55
api, WPNetworkRequestExecutor, WPNetworkResponseParser, CLASSIC_EDITOR_PLUGIN_SLUG,
@@ -53,24 +53,24 @@ async fn test_plugin_list_params_parametrized(
5353
}
5454

5555
#[rstest]
56-
#[case(CLASSIC_EDITOR_PLUGIN_SLUG, "WordPress Contributors")]
57-
#[case(HELLO_DOLLY_PLUGIN_SLUG, "Matt Mullenweg")]
56+
#[case(CLASSIC_EDITOR_PLUGIN_SLUG.into(), "WordPress Contributors")]
57+
#[case(HELLO_DOLLY_PLUGIN_SLUG.into(), "Matt Mullenweg")]
5858
#[trace]
5959
#[tokio::test]
60-
async fn retrieve_plugin(
61-
#[case] plugin_slug: &str,
60+
async fn retrieve_plugin_with_edit_context(
61+
#[case] plugin_slug: PluginSlug,
6262
#[case] expected_author: &str,
6363
#[values(WPContext::Edit, WPContext::Embed, WPContext::View)] context: WPContext,
6464
) {
6565
let parsed_response = api()
66-
.retrieve_plugin_request(context, plugin_slug)
66+
.retrieve_plugin_request(context, &plugin_slug)
6767
.execute()
6868
.await
6969
.unwrap()
7070
.parse(wp_api::parse_retrieve_plugin_response_with_edit_context);
7171
assert!(
7272
parsed_response.is_ok(),
73-
"Retrieve plugin failed!\nContext: {:?}\nPlugin: {}\nResponse was: '{:?}'",
73+
"Retrieve plugin failed!\nContext: {:?}\nPlugin: {:?}\nResponse was: '{:?}'",
7474
context,
7575
plugin_slug,
7676
parsed_response

‎wp_networking/tests/test_plugins_mut.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ async fn create_plugin() {
1414
wp_db::run_and_restore(|mut _db| async move {
1515
let status = PluginStatus::Active;
1616
let params = PluginCreateParams {
17-
slug: WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS.to_string(),
17+
slug: WP_ORG_PLUGIN_SLUG_CLASSIC_WIDGETS.into(),
1818
status,
1919
};
2020
let created_plugin = api()
@@ -36,7 +36,10 @@ async fn update_plugin() {
3636
wp_db::run_and_restore(|mut _db| async move {
3737
let status = PluginStatus::Active;
3838
let updated_plugin = api()
39-
.update_plugin_request(HELLO_DOLLY_PLUGIN_SLUG, PluginUpdateParams { status })
39+
.update_plugin_request(
40+
&HELLO_DOLLY_PLUGIN_SLUG.into(),
41+
PluginUpdateParams { status },
42+
)
4043
.execute()
4144
.await
4245
.unwrap()
@@ -53,7 +56,7 @@ async fn delete_plugin() {
5356
run_and_restore_wp_content_plugins(|| {
5457
wp_db::run_and_restore(|mut _db| async move {
5558
let deleted_plugin = api()
56-
.delete_plugin_request(CLASSIC_EDITOR_PLUGIN_SLUG)
59+
.delete_plugin_request(&CLASSIC_EDITOR_PLUGIN_SLUG.into())
5760
.execute()
5861
.await
5962
.unwrap()

0 commit comments

Comments
 (0)
Please sign in to comment.