starkware-libs / cairo

Cairo is the first Turing-complete language for creating provable programs for general computation.
Apache License 2.0
1.57k stars 479 forks source link

feat: cairo-format format to string output formatted content #4134

Closed ClementWalter closed 8 months ago

ClementWalter commented 1 year ago

Feature Request

Describe the Feature Request

Currently, the cairo format can either overwrite the content, or output a FileDif. The feature request is to output only the result, not the diff.

Describe Preferred Solution

Add a flag --emit stdout like in rust fmt

Describe Alternatives

make --check returns the result and not the diff

Related Code

https://github.com/starkware-libs/cairo/blob/2e8142b67a4312c830aea1bb9e34631bf0e39a5d/crates/cairo-lang-formatter/src/cairo_formatter.rs#L209C9-L209C21

Additional Context

it's actually quite odd to have a format_to_string function that doesn't return a plain string but a FileDiff. The docstring actually seems to say that the returned result is the formatted output and not the diff neither. So maybe better just to return the output. Is anyone using the FileDiff? If not, the easiest would just be to update the current --check to return the formatted result.

orizi commented 1 year ago

we do the same as rustfmt - which outputs the diff as well. why do you need the binary to return the formatted string? what is the usecase?

mkaput commented 1 year ago

why do you need the binary to return the formatted string? what is the usecase?

@ClementWalter described his use case here: https://github.com/software-mansion/scarb/issues/700#issue-1910789861

orizi commented 1 year ago

As answered there - this does not actually require updates here if you want to use scarb fmt. You can feel free to use the direct binary here and add this feature to it - it would be basically the same work.