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

Refactor navigation helpers #465

Merged
merged 4 commits into from
Nov 18, 2017

Conversation

jacwah
Copy link
Contributor

@jacwah jacwah commented Oct 6, 2017

This is my attempt at a piece of #458.

  • Code duplication between previous() and next() have been removed.
  • Finding a chapter and rendering it have been split to separate functions.

There are probably tons of potential improvements, but it still feels a lot cleaner than before 😊.

@azerupi
Copy link
Contributor

azerupi commented Oct 7, 2017

The changes look good at first glance. I am going to do a more in-depth review soon.
Thanks for the PR! :)

Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan left a comment

Choose a reason for hiding this comment

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

It's really nice to see big chunks of copy-pasta being removed!

It may be a good idea to add a test or two in to make sure the refactoring doesn't accidentally introduce any regressions into the rendered output though. What are your thoughts?

base_path: &String,
current_path: &String,
current_item: &StringMap,
previous_item: StringMap,
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any reason why we can't take previous_item by reference here? It feels a bit odd that we're taking the current item by reference but then moving the previous item.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was probably influenced by the fact that the call site already has previous_item available. This way we avoid a second clone. When looking at Target::find in isolation, that doesn't make a lot of though, as you point out.

t.render(r, &mut local_rc)
})?;
if let Some(previous) = previous {
if let Some(item) = target.find(&base_path, &path, &item, previous)? {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 This looks so much nicer than the massive block we had before!

r: &Handlebars,
rc: &mut RenderContext,
chapter: &StringMap,
) -> Result<(), RenderError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

How hard would it be to write a test which checks this handlebars helper? Would it be possible to mock up a quick template then run the handlebars helper and assert that the rendered output looks like we expect it to?

This makes more sense for find as an interface, though it causes a
second clone in some cases. Maybe rustc is smart here?
@jacwah
Copy link
Contributor Author

jacwah commented Nov 13, 2017

@Michael-F-Bryan I changed the signature of Target::find and added some tests for the helpers. Do you have any other types of tests in mind?

Copy link
Contributor

@Michael-F-Bryan Michael-F-Bryan left a comment

Choose a reason for hiding this comment

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

Looks good to me 👍

I've made a note on maybe breaking the assert_eq!() statement out into something like let should_be = ...; let got = ...; assert_eq!(got, should_be);, but that's a personal choice thing so feel free to ignore it 😃

h.register_helper("previous", Box::new(previous));
h.register_helper("next", Box::new(next));

assert_eq!(
Copy link
Contributor

Choose a reason for hiding this comment

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

I usually break this out into a couple lines. So at the top of the test I'll assign what I expect to a should_be variable, then assign what we actually get to got. Then at the bottom you can write assert_eq!(got, should_be); (e.g. config.rs).

It's definitely a personal taste thing, but it's nice when you can look at the top and figure out what the expected output should be at a glance without needing to look for all assert_eq!() calls.

@Michael-F-Bryan
Copy link
Contributor

Do you have any other types of tests in mind?

The tests you've written look good to me. They're clear, concise, and test most common scenarios 👍

I restarted the Mac travis build. It looks like it was some spurious Travis issue that failed the build and not anything to do with you. Once travis gives the green light I reckon this is ready to merge.

@Michael-F-Bryan
Copy link
Contributor

The failed build for OSX/beta is completely unrelated to this PR and seems to be some sort of permissions issue with our .travis.yml. I encountered the exact same issue when trying to merge #422 and have opened an issue (#490) to figure out the root cause.

In the meantime I think it's fine to merge this PR. Nice work @jacwah!

@Michael-F-Bryan Michael-F-Bryan merged commit 3d5eb48 into rust-lang:master Nov 18, 2017
@Michael-F-Bryan Michael-F-Bryan mentioned this pull request Nov 18, 2017
6 tasks
Ruin0x11 pushed a commit to Ruin0x11/mdBook that referenced this pull request Aug 30, 2020
* Refactor navigation helpers

* Target::find: take previous_item by reference

This makes more sense for find as an interface, though it causes a
second clone in some cases. Maybe rustc is smart here?

* Test next and previous navigation helpers

* Add more next/previous tests
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.

3 participants