Skip to content

Conversation

crazytonyli
Copy link
Contributor

What says in the title.

@crazytonyli crazytonyli enabled auto-merge (squash) June 14, 2024 01:58
request_executor: Arc<dyn RequestExecutor>,
) -> Result<Self, WpApiError> {
let api_base_url: Arc<ApiBaseUrl> = ApiBaseUrl::new(site_url.as_str())
let api_base_url: Arc<ApiBaseUrl> = TryInto::<ApiBaseUrl>::try_into(site_url.as_str())
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let api_base_url: Arc<ApiBaseUrl> = TryInto::<ApiBaseUrl>::try_into(site_url.as_str())
let api_base_url: Arc<ApiBaseUrl> = ApiBaseUrl::try_from(site_url.as_str())

This can be simplified as such ^, however I am not entirely sure if this is an improvement over the current API. The main issue I see is that ApiBaseUrl doesn't provide any way to construct itself besides the TryFrom which is not obvious without consulting the documentation. (Note that the ApiBaseUrl's fields are private, so it can't be constructed with struct literal syntax outside of its module which is why it's more important to think about how the consumers will construct it)

TryFrom is a good addition, but I suggest keeping the new function and calling TryFrom from it, just to cover the issue I mentioned above. If you agree with that, we can revert this line back to the original because it's a more natural way to construct the instance.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. The constructor is back in 5373cf1

Comment on lines 184 to 186
TryInto::<ApiBaseUrl>::try_into("https://example.com")
.unwrap()
.into()
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
TryInto::<ApiBaseUrl>::try_into("https://example.com")
.unwrap()
.into()
ApiBaseUrl::try_from("https://example.com").unwrap().into()

@crazytonyli crazytonyli requested a review from oguzkocer June 14, 2024 08:02
@crazytonyli crazytonyli merged commit a2d39dd into trunk Jun 14, 2024
@crazytonyli crazytonyli deleted the rust-api-base-url-try-from branch June 14, 2024 16:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants