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

Update Rust style guide #3172

Merged
merged 5 commits into from
Apr 15, 2022
Merged

Update Rust style guide #3172

merged 5 commits into from
Apr 15, 2022

Conversation

wdanilo
Copy link
Member

@wdanilo wdanilo commented Nov 30, 2021

I've created a bunch of small updates to the style guide which describe things that we already use in the codebase. Is there anything missing, or is something not clear enough? If so, please write comments about that and I'll fix it.

@vitvakatu
Copy link
Contributor

Thank you, I love this changes!

I would like to have answers to the following questions:

  1. What can be placed inside a single section/subsection and what cannot? Can I place structure definition and its impl in separate subsections? Can I place a helper structure definition and a set of constants in a single section without subsections (see this)?
  2. What items should be documented with /// docstrings and what shouldn't? Should I document obvious-looking methods? (we have exactly 400 /// Constructor. in our codebase) Should I document structure fields or enum variants? Should I document module-private items? Should I document every public one?
  3. Is there some kind of policy for TODO and FIXME comments? What if I really-really want to add such comment with issue link?

Comment on lines 14 to 15
- **Use one import per line.**
Do not use the `use crate::mod_name::{name1, name2, name3}` syntax. For each entity use a separate `use` statement.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is enforced by the rustfmt, likely no need to include this here,

Comment on lines 20 to 21
- **Group related submodule and import statements**
Submodule and import statements should be divided into several groups separated by blank lines. Items in the groups should be sorted alphabetically (do not use group comments in your code):
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not what I've been already doing nor what I've been requesting in my reviews, so I think it actually warrants some discussion.
Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.

But the enso-org/ide is archived, right? That strongly suggests, that the guidelines there should not be used anymore.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ouch, wrong link.
The one I've meant is https://github.com/enso-org/enso/blob/develop/docs/style-guide/rust.md

Issue is that we have two Rust style guide documents.

Copy link
Member Author

Choose a reason for hiding this comment

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

The idea is to remove the second style guide ASAP and make this the only one.

Comment on lines 17 to 18
- **Don't use relative imports.**
Do not use `super::` nor `self::` imports. Use absolute imports or imports from local submodules only.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd add an exception for "inlined" modules, or at least for the "test" module.

Comment on lines 28 to 29
// Reexports.
pub use geometry::*;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd put reexports as the last group: sometimes you reexport an imported thing (not necessarily from a submodule). Additionally, we have then a clear place where imports ends, and visible names starts (reexports, and then definitions).

Copy link
Member Author

Choose a reason for hiding this comment

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

Publicly available things should be next to each other, so, re-exports should ld be next to sub-modules. Otherwise, you need to brwose different parts of the files in order to understand what are the publicly exposed entities in the file.

Copy link
Contributor

Choose a reason for hiding this comment

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

But still, the definitions are also publicly available. So in the current state you have:

  1. public submodules
  2. public reexports
  3. non-public imports
  4. public definitions

That's why I opted for moving 2 after 3.

On the other hand... imports might be treated as part of 4 actually, so let's stay with your ordering!

Comment on lines 31 to 43
// Prelude-like imports.
use crate::prelude::*;
use sprite_system::traits::*;

// Local-crate imports.
use crate::closure;
use crate::data::opt_vec::OptVec;
use crate::data::dirty;
use crate::system::web::group;

// External imports.
use nalgebra::Matrix4;
use nalgebra::Vector3;
Copy link
Contributor

Choose a reason for hiding this comment

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

What prelude-like means? Everything with star at the end?

By the way, once we have rust formatter, I wonder if we could keep imports in a single block, sorted alphabetically. Personally, I've never taken an advantage of having imports separated in blocks. But having a single block allows me to make use of Intellij auto-import feature + that makes mass-refactoring easier as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Let's chat about it. I think this needs a more interactive discussion.

Copy link
Contributor

Choose a reason for hiding this comment

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

Did you end up talking about this? If so, what was the result of the discussion?

Comment on lines 20 to 21
- **Group related submodule and import statements**
Submodule and import statements should be divided into several groups separated by blank lines. Items in the groups should be sorted alphabetically (do not use group comments in your code):
Copy link
Contributor

Choose a reason for hiding this comment

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

Also, this should be unified with https://github.com/enso-org/ide/blob/develop/docs/contributing/style-guide.md
Ideally, both style guides should be unified, as we have a single repository now.
Merging as-is would create a conflict between these documents.

But the enso-org/ide is archived, right? That strongly suggests, that the guidelines there should not be used anymore.

@wdanilo
Copy link
Member Author

wdanilo commented Dec 2, 2021

@vitvakatu, good points! I have a few more, based on yesterdays PR review, I plan to add these + answers to your questions somewhat today / tomorrow evening!

@MichaelMauderer
Copy link
Contributor

@wdanilo This has been open for a while without change. What is the state of the updates?

@vitvakatu
Copy link
Contributor

Another thing that I faced - we do not have any explicit requirements for code formatting inside macros (like FRP macros or f! macro) - although our codebase definitely preserves some "historical" code formatting there.

@wdanilo
Copy link
Member Author

wdanilo commented Apr 13, 2022

Everyone, I apologise for making this waiting so long. I addressed the comments.

@vitvakatu, regarding your points 1-3 (the first comment), let's create separate PRs with the changes there and let's discuss these things on Discord first.

Do not use `super::` nor `self::` imports in files (you can use them in localy defined modules). Use absolute imports or imports from local submodules only.

- **Use Enso Formatter to format your imports**
Run the `build/enso-formatter` script to format imports in all files before contributing your PR.
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add a copy-pastable command for running this from the project root?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that would be nice. I use cargo run -p enso-formatter for that. Even nicer would be to integrate it with our ./run lint tool, but I remember there were some problems with that.

Copy link
Contributor

@vitvakatu vitvakatu left a comment

Choose a reason for hiding this comment

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

I think the changes in this PR are rather subtle and do not answer a lot of practical questions around our code style, but let's discuss them separately as this PR is here for a few months now.


## Styling rules
## Code formatting in macros.
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same was as `rustfmt` would do it.
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
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same was as `rustfmt` would do it.
Unfortunately, `rustfmt` is not working inside of macros. Thus, this code should be manually formated in the same way as `rustfmt` would do it.

However, it's not clear how "rustfmt would do it" for non-Rust syntax, like our FRP definitions.

Do not use `super::` nor `self::` imports in files (you can use them in localy defined modules). Use absolute imports or imports from local submodules only.

- **Use Enso Formatter to format your imports**
Run the `build/enso-formatter` script to format imports in all files before contributing your PR.
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, that would be nice. I use cargo run -p enso-formatter for that. Even nicer would be to integrate it with our ./run lint tool, but I remember there were some problems with that.

@wdanilo wdanilo merged commit e8dfc6d into develop Apr 15, 2022
@wdanilo wdanilo deleted the wip/wdanilo/doc-fixes branch April 15, 2022 14:10
hubertp pushed a commit that referenced this pull request Apr 19, 2022
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.

5 participants