patterns icon indicating copy to clipboard operation
patterns copied to clipboard

Check code formatting of examples in CI

Open marcoieni opened this issue 5 years ago • 7 comments

Is this possible with rustfmt and mdbook?

marcoieni avatar Jan 01 '21 20:01 marcoieni

Opened an upstream issue: https://github.com/rust-lang/mdBook/issues/1422

simonsan avatar Jan 04 '21 10:01 simonsan

I removed this from "To do" in Rust Patterns because it is blocked by upstream.

marcoieni avatar Jan 06 '21 13:01 marcoieni

An idea would be to extract the code and put them in specific example files like ffi-export.rs and include this file/parts of this file with the following syntax:

https://rust-lang.github.io/mdBook/format/mdbook.html#including-files

That would make it possible to actually check the files, have better examples people can compile directly and we can run cargo fmt and cargo check on them if we want to.

Downside of it is that it's a bit more work to include it in the markdown but the upside is that we separate actual code examples from the documentation. What are your thoughts about it?

simonsan avatar Jan 07 '21 07:01 simonsan

Having code examples in the markdown is super handy. I don't think it's worth it.

What about extracting the code examples from markdown with a script, and run rustfmt on them individually?

marcoieni avatar Jan 10 '21 22:01 marcoieni

But is it possible to check the code samples in the docs while keeping it inlined? I don't think it is useful to separate code samples for a book.

pickfire avatar Jan 12 '21 01:01 pickfire

I'm not aware of any solution that checks the Rust code formatting inline in Markdown nor extracting the code and formatting it on-the-flow and writing it back to the exact same position, hence I thought this could be a solution.

But indeed, not a comfortable one. For the book itself the code examples would be inline, for the markdown they would not be inline anymore.

As I think the most important part in this is now the book itself, I don't have much of a problem separating code from text if it makes linting, formatting and compiling easier.

Also another advantage: We would be able to have running programs, where people could pick out portions of that file to further explain them in the markdown. And if people would like to run a more complete example, they could compile it with (e.g.) cargo run --example builder.

~~The disadvantage with this kind of separation is if we edit code examples we need to update the lines in the markdown file as well.~~ EDIT: We wouldn't need to with anchors: To avoid breaking your book when modifying included files, you can also include a specific section using anchors instead of line numbers.

I'm not quite sure if this wouldn't be the overall better long-term solution, though. In my imagination it would be much cleaner for this repository while the book itself would look the same. Is there anything else I might be overlooking here?

EDIT2: https://github.com/PsichiX/md-bakery seems also interesting, maybe not for our use case, just for me as a reminder

simonsan avatar Jan 12 '21 11:01 simonsan