Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a result for ResourceManager::get_resource #282

Merged
merged 7 commits into from
Dec 2, 2022
Merged

Return a result for ResourceManager::get_resource #282

merged 7 commits into from
Dec 2, 2022

Conversation

adriantombu
Copy link
Contributor

See #278 for more context

@adriantombu
Copy link
Contributor Author

@gregtatum just creating a draft here to get early feedback and see if I'm going in the right direction ^^

@gregtatum gregtatum self-requested a review November 9, 2022 18:59
Copy link
Member

@gregtatum gregtatum left a comment

Choose a reason for hiding this comment

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

Yeah this seems to be in the right direction. I did a quick read-through with a few comments. It will need tests. I have some I wrote on some local patches working on some code coverage (that's where this issue came from) but feel free to add your own, or wait a day or two until I get mine online to base it off of.

@@ -22,6 +22,7 @@ fluent-fallback = { version = "0.6.0", path = "../fluent-fallback" }
unic-langid = "0.9"
elsa = "1.5"
futures = "0.3"
thiserror = "1.0.37"
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: The version number should match the other thiserror version.

Copy link
Contributor Author

@adriantombu adriantombu Nov 10, 2022

Choose a reason for hiding this comment

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

Is this ok if we start using workspace dependencies to simplify the versions across the crates? See this commit a75bf08. It was released in Rust 1.64.0 a few months ago, it's pretty handy.

Copy link
Member

Choose a reason for hiding this comment

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

This might be better as a separate PR. I don't know if the Firefox vendoring process works with workspace, and we would need to block on verifying that it would work. I wouldn't want to break the Firefox integration.

fluent-resmgr/src/resource_manager.rs Show resolved Hide resolved

let mut errors = vec![];
let msg = bundle.get_message("hello-world").expect("Message exists");
let pattern = msg.value().expect("Message has a value");
let value = bundle.format_pattern(&pattern, None, &mut errors);
Copy link
Member

Choose a reason for hiding this comment

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

Question: These & changes seem unrelated drive-by fixes. I'm assuming since the pattern here is already a reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I did a few clippy quick fixes, I thought it wouldn't hurt much 😄

@adriantombu adriantombu marked this pull request as ready for review November 22, 2022 11:20
@adriantombu
Copy link
Contributor Author

@gregtatum I just rebased and fixed the tests you wrote to make them work with my changes. Do you see anything else to do on this?

@gregtatum gregtatum self-requested a review November 29, 2022 15:14
Copy link
Member

@gregtatum gregtatum left a comment

Choose a reason for hiding this comment

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

Thanks for the updates on the patch! This is real close!

I'm marking request changes for a few suggestions and nitpicks that I had.

I also found one issue, where the get_bundles method had a change in behavior.

Once you are ready for a re-review please click the little "refresh" icon next to my name in the review, as this will re-trigger review for me. I missed the comments that you had left in my notifications deluge. Setting the review status will make sure that it's in my queue to take a look at.

Cargo.toml Outdated
@@ -13,3 +13,6 @@ members = [
exclude = [
"fluent-cli",
]

[workspace.dependencies]
thiserror = "1.0"
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: Per my comment earlier, I'd prefer to still use the old way until we can:

  1. Verify that Firefox vendoring still works.
  2. Apply these changes to the entire repo.

Feel free to file a follow-up PR with this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, I'll provide another PR for this so that we are not blocked by it!

fluent-resmgr/src/resource_manager.rs Show resolved Hide resolved
let res = self.get_resource(res_id, &locales[0].to_string());
bundle.add_resource(res).unwrap();

resource_ids.iter().for_each(|res_id| {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: I don't think anywhere else has used the for_each. I think it would be simpler and more idiomatic to use a for loop here.

bundle.add_resource(res).unwrap();

resource_ids.iter().for_each(|res_id| {
let _ = self
Copy link
Member

Choose a reason for hiding this comment

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

Nit: let _ isn't needed here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Clippy was unhappy about it, but I switched to a match to avoid this ugly let _

});
});

if errors.iter().len() > 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Nit: This could be simplified to errors.is_empty().

})
) -> Result<impl Iterator<Item = FluentBundle<&FluentResource>>, Vec<ResourceManagerError>>
{
let mut ptr = 0;
Copy link
Member

Choose a reason for hiding this comment

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

Nit: idx is clearer choice here compared to ptr. I think this may have been missed in a rebase.

let mut errors: Vec<ResourceManagerError> = vec![];
let mut res = vec![];

while let Some(locale) = locales.get(ptr) {
Copy link
Member

Choose a reason for hiding this comment

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

Issue: This changes the behavior of this function from being lazy (where the bundles are loaded on each iteration), to fully evaluating the bundles. The intent for this iterator is to only evaluate the bundle on iteration.

Rather than returning Result<Iterator> this should return a:

impl Iterator<Item = Result< ... >>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wooops, sorry about that 🙈

fluent-resmgr/src/resource_manager.rs Show resolved Hide resolved
@@ -27,7 +27,7 @@ include = [
[dependencies]
serde = { version = "1.0", features = ["derive"], optional = true }
serde_json = { version = "1.0", optional = true }
thiserror = "1.0"
thiserror.workspace = true
Copy link
Member

Choose a reason for hiding this comment

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

Suggestion: See above on the workspace feedback.

Copy link
Member

@gregtatum gregtatum left a comment

Choose a reason for hiding this comment

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

Thanks for the contribution!

@gregtatum gregtatum merged commit f2e4359 into projectfluent:main Dec 2, 2022
@adriantombu adriantombu deleted the task/278-result-instead-of-panic branch December 2, 2022 22:04
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