Skip to content

Conversation

oguzkocer
Copy link
Contributor

@oguzkocer oguzkocer commented May 23, 2024

Builds on #108.

Rust API Guidelines - Debuggability states:

All public types implement Debug (C-DEBUG)
If there are exceptions, they are rare.

I've gone through all our public types and derived Debug trait. If there are any that doesn't derive it, it's just because I missed it. I've also added custom implementations for WPNetworkRequest & WPNetworkResponse types, so body can be printed as a String and in a nice-ish format.

It adds the indoc crate to make it easier to work with multiline strings. I wouldn't add the crate just for this implementation, but I think we'll utilize it more. It's also a tiny crate.

Here are some examples:

println!(
    "{:?}",
    api().create_user_request(&UserCreateParams::new(
        "foo".to_string(),
        "foo@example.com".to_string(),
        "strong_foo".to_string(),
    ))
);
---
WPNetworkRequest {
    method: 'POST',
    url: 'http://localhost/wp-json/wp/v2/users',
    header_map: '{"accept": "application/json", "content-type": "application/json", "Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTo1TU9rSnlCVVo3OHdUSVdES2FhcHpGS1k="}',
    body: 'Some("{\"username\":\"foo\",\"email\":\"foo@example.com\",\"password\":\"strong_foo\"}")'
}
println!(
    "{:?}",
    api().retrieve_user_request(FIRST_USER_ID, WPContext::Edit)
);
---
WPNetworkRequest {
    method: 'GET',
    url: 'http://localhost/wp-json/wp/v2/users/1?context=edit',
    header_map: '{"accept": "application/json", "Authorization": "Basic dGVzdEBleGFtcGxlLmNvbTo1TU9rSnlCVVo3OHdUSVdES2FhcHpGS1k="}',
    body: 'None'
}
println!(
    "{:?}",
    api()
        .retrieve_user_request(FIRST_USER_ID, WPContext::Edit)
        .execute()
        .await
);
---
Ok(WPNetworkResponse {
    status_code: '200',
    header_map: 'None',
    body: '{"id":1,"username":"test@example.com","name":"test@example.com","first_name":"","last_name":"","email":"test@example.com","url":"http:\/\/localhost","description":"","link":"http:\/\/localhost\/author\/testexample-com\/","locale":"en_US","nickname":"test@example.com","slug":"testexample-com","roles":["administrator"],"registered_date":"2024-05-23T17:20:09+00:00","capabilities":{"switch_themes":true,"edit_themes":true,"activate_plugins":true,"edit_plugins":true,"edit_users":true,"edit_files":true,"manage_options":true,"moderate_comments":true,"manage_categories":true,"manage_links":true,"upload_files":true,"import":true,"unfiltered_html":true,"edit_posts":true,"edit_others_posts":true,"edit_published_posts":true,"publish_posts":true,"edit_pages":true,"read":true,"level_10":true,"level_9":true,"level_8":true,"level_7":true,"level_6":true,"level_5":true,"level_4":true,"level_3":true,"level_2":true,"level_1":true,"level_0":true,"edit_others_pages":true,"edit_published_pages":true,"publish_pages":true,"delete_pages":true,"delete_others_pages":true,"delete_published_pages":true,"delete_posts":true,"delete_others_posts":true,"delete_published_posts":true,"delete_private_posts":true,"edit_private_posts":true,"read_private_posts":true,"delete_private_pages":true,"edit_private_pages":true,"read_private_pages":true,"delete_users":true,"create_users":true,"unfiltered_upload":true,"edit_dashboard":true,"update_plugins":true,"delete_plugins":true,"install_plugins":true,"update_themes":true,"install_themes":true,"update_core":true,"list_users":true,"remove_users":true,"promote_users":true,"edit_theme_options":true,"delete_themes":true,"export":true,"administrator":true},"extra_capabilities":{"administrator":true},"avatar_urls":{"24":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=24&d=mm&r=g","48":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=48&d=mm&r=g","96":"http:\/\/2.gravatar.com\/avatar\/55502f40dc8b7c769880b10874abc9d0?s=96&d=mm&r=g"},"meta":{"persisted_preferences":[]},"_links":{"self":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/users\/1"}],"collection":[{"href":"http:\/\/localhost\/wp-json\/wp\/v2\/users"}]}}'
})

@oguzkocer oguzkocer added the Rust label May 23, 2024
@oguzkocer oguzkocer added this to the 0.1 milestone May 23, 2024
@oguzkocer oguzkocer requested review from crazytonyli and jkmassel May 23, 2024 19:21
Copy link
Contributor

@jkmassel jkmassel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about suggesting a DRY thing for the multi-line strings, but they're rare enough that I think we can go back and do it later.

Base automatically changed from request-module to trunk May 23, 2024 19:41
@oguzkocer oguzkocer force-pushed the all-public-types-implement-debug-trait branch from 8952243 to e83ec87 Compare May 23, 2024 19:45
@oguzkocer oguzkocer enabled auto-merge (squash) May 23, 2024 19:48
@oguzkocer oguzkocer merged commit 329ca83 into trunk May 23, 2024
@oguzkocer oguzkocer deleted the all-public-types-implement-debug-trait branch May 23, 2024 20:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants