12 stable releases

new 1.9.2 Apr 16, 2024
1.8.0 Feb 28, 2024
1.7.0 Aug 11, 2023
1.6.0 May 12, 2023
1.0.0 Nov 22, 2022

#708 in Procedural macros

Download history 59/week @ 2023-12-22 102/week @ 2023-12-29 153/week @ 2024-01-05 172/week @ 2024-01-12 215/week @ 2024-01-19 144/week @ 2024-01-26 239/week @ 2024-02-02 202/week @ 2024-02-09 208/week @ 2024-02-16 352/week @ 2024-02-23 280/week @ 2024-03-01 276/week @ 2024-03-08 233/week @ 2024-03-15 223/week @ 2024-03-22 501/week @ 2024-03-29 398/week @ 2024-04-05

1,390 downloads per month
Used in typeshare-cli

MIT/Apache

345KB
10K SLoC

Rust 4.5K SLoC // 0.0% comments Swift 2K SLoC // 0.0% comments Go 1.5K SLoC // 0.0% comments Scala 780 SLoC // 0.1% comments Kotlin 778 SLoC // 0.1% comments TypeScript 338 SLoC // 0.1% comments

typeshare-core

The core library behind typeshare, containing type definitions, parsing, and code generation.

Formatting

Formatting code is hard. Like, really, really hard. Typeshare happens to be a program that needs to generate beautifully formatted code in multiple languages.

In the interests of avoiding the complexities of formatting code perfectly, typeshare takes the approach of generating "best-effort" output that it then runs through the appropriate formatting tool for each language. This makes typeshare's output exactly what language devs would expect, every time, with minimal effort on our part.

Generating well-formatted output therefore requires you to have the appropriate formatting tools for each language installed and available when running typeshare. If you don't have them, typeshare's default (and less pretty) output will be used.

Testing

typeshare's test suite is built on the concept of snapshot testing, a data-driven testing methodology that aims to make it quick and painless to enact large, sweeping changes in a codebase.

Tests are declared inside of a macro defined in tests/snapshot_tests.rs. Once a test has been declared, run:

env UPDATE_EXPECT=1 cargo test -p typeshare-core

This will generate the the folder for the new test (inside of data/tests) along with starter files inside of it. Save whatever Rust source input you'd like to test in the input.rs file. Then, run the command again:

env UPDATE_EXPECT=1 cargo test -p typeshare-core

The various output files will be updated with typeshare's current output for the given input. If you're happy with the output, move on; if not, hack on typeshare until the output makes sense, re-running the above command each time you'd like to update the expected output.

The test suite can of course be run normally without updating any expectations:

cargo test -p typeshare-core

If you find yourself needing to update expectations for a specific test only, run the following (subsituting the name of your test in for the last arg):

env UPDATE_EXPECT=1 cargo test -p typeshare-core --test snapshot_tests -- can_handle_serde_rename_all::swift

The data stored in the snapshot test files is typeshare's output, unmodified.

This will write output_formatted.(ts|kt|swift) files next to each stored snapshot file.

Dependencies

~0.7–1.2MB
~27K SLoC