Skip to content

Simplify generated code module structure #138

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

Merged
merged 4 commits into from
Sep 8, 2020

Conversation

swallez
Copy link
Member

@swallez swallez commented Sep 7, 2020

Remove the generated directory and the various re-exports.

  • namespace docs have been extracted from the respective mod.rs to
    .md files in api_generator/docs, and are inserted in the generated
    source file.
  • namespace sources are generated directly in the src directory
  • the generator can now insert generated sections in regular source
    files based on GENERATE-BEGIN and GENERATE-END markers. This is
    used to generate the namespace module list in the main lib.rs and
    merge generated with manual code in params.rs
  • generated and merged source files are listed in src/.generated.toml
    so that the generator can cleanup on start.

Before considering merging generated code with special markers we
experimented with the include!() macro, but this didn't work well:

  • rustfmt will not reformat include files
  • mod statement look for files relative to the source file, so
    include files have to be located in the same directory as their
    includer, while locating them in a dedicated directory would have
    provided a nicer organization.

Merging generated sections provides a more natural source code layout.

Remove the `generated` directory and the various re-exports.

- namespace docs have been extracted from the respective `mod.rs` to
  `.md` files in api_generator/docs, and are inserted in the generated
  source file.
- namespace sources are generated directly in the `src` directory
- the generator can now insert generated sections in regular source
  files based on `GENERATE-BEGIN` and `GENERATE-END` markers. This is
  used to generate the namespace module list in the main `lib.rs` and
  merge generated with manual code in `params.rs`
- generated and merged source files are listed in `src/.generated.toml`
  so that the generator can cleanup on start.

Before considering merging generated code with special markers we
experimented with the `include!()` macro, but this didn't work well:
- rustfmt will not reformat include files
- `mod` statement look for files relative to the source file, so
  include files have to be located in the same directory as their
  includer, while locating them in a dedicated directory would have
  provided a nicer organization.

Merging generated sections provides a more natural source code layout.
@swallez swallez requested a review from russcam September 7, 2020 22:01
Copy link
Contributor

@russcam russcam left a comment

Choose a reason for hiding this comment

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

The change looks good overall. I left some comments about modules that are now both public and re-exported at the crate root

Comment on lines 358 to 359
pub mod client;
pub mod error;
Copy link
Contributor

Choose a reason for hiding this comment

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

These are now public modules in addition to being re-exported at the crate level on line 393. Take a look at the generated docs (cargo make docs) compared to https://docs.rs/elasticsearch/7.9.0-alpha.1/elasticsearch/#modules

I think the crate is a little cleaner if these are exposed only at the crate level - the types within them are universally applicable.

Copy link
Member Author

@swallez swallez Sep 8, 2020

Choose a reason for hiding this comment

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

Good catch, I overlooked this while reorganizing mod declarations. Fixed in 9ba6ca2

pub mod error;
pub mod http;
pub mod params;
pub mod root;
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 now a public module in addition to being re-exported at the crate level on line 393. I think the crate is a little cleaner if this exposed only at the crate level - the "root" namespace is a bit of an odd construct which effectively represents "no namespace" 🙂

@@ -28,9 +28,9 @@ use std::{
};

fn main() -> Result<(), failure::Error> {
// This must be run from the src root directory, with cargo run -p api_generator
// This must be run from the repo root directory, with cargo run -p api_generator
Copy link
Contributor

Choose a reason for hiding this comment

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

Guess we can update this now to cargo make generate-api 😃

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed 😉 - done in 00efb77

toml::to_string_pretty(&GeneratedFiles {
written: written_files,
merged: merged_files,
})?,
Copy link
Contributor

@russcam russcam Sep 8, 2020

Choose a reason for hiding this comment

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

should we make written_files and merged_files be ordinal ordered, for consistent ordering? I understand they don't need to be, but I ran cargo make generate-api on my machine and the order of entries in .generated.toml is different (possible difference in default string ordering implementation on Windows vs. non-Windows)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, replaced by a BTreeSet in 9ba6ca2 to have a stable ordering.

@swallez swallez merged commit 9d9a4fb into elastic:master Sep 8, 2020
@swallez swallez deleted the remove-reexports branch September 8, 2020 10:07
russcam pushed a commit that referenced this pull request Sep 23, 2020
Remove the `generated` directory and the various re-exports.

- namespace docs have been extracted from the respective `mod.rs` to
  `.md` files in api_generator/docs, and are inserted in the generated
  source file.
- namespace sources are generated directly in the `src` directory
- the generator can now insert generated sections in regular source
  files based on `GENERATE-BEGIN` and `GENERATE-END` markers. This is
  used to generate the namespace module list in the main `lib.rs` and
  merge generated with manual code in `params.rs`
- generated and merged source files are listed in `src/.generated.toml`
  so that the generator can cleanup on start.

Before considering merging generated code with special markers we
experimented with the `include!()` macro, but this didn't work well:
- rustfmt will not reformat include files
- `mod` statement look for files relative to the source file, so
  include files have to be located in the same directory as their
  includer, while locating them in a dedicated directory would have
  provided a nicer organization.

Merging generated sections provides a more natural source code layout.

(cherry picked from commit 9d9a4fb)
russcam pushed a commit that referenced this pull request Sep 23, 2020
Remove the `generated` directory and the various re-exports.

- namespace docs have been extracted from the respective `mod.rs` to
  `.md` files in api_generator/docs, and are inserted in the generated
  source file.
- namespace sources are generated directly in the `src` directory
- the generator can now insert generated sections in regular source
  files based on `GENERATE-BEGIN` and `GENERATE-END` markers. This is
  used to generate the namespace module list in the main `lib.rs` and
  merge generated with manual code in `params.rs`
- generated and merged source files are listed in `src/.generated.toml`
  so that the generator can cleanup on start.

Before considering merging generated code with special markers we
experimented with the `include!()` macro, but this didn't work well:
- rustfmt will not reformat include files
- `mod` statement look for files relative to the source file, so
  include files have to be located in the same directory as their
  includer, while locating them in a dedicated directory would have
  provided a nicer organization.

Merging generated sections provides a more natural source code layout.

(cherry picked from commit 9d9a4fb)
@swallez swallez added enhancement New feature or request v7.10.0-alpha.1 labels Nov 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request v7.10.0-alpha.1
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants