PythonRust
inflection in Rust
The Python inflection library 0.5.1 in Rust: pluralize, singularize, camelize and the rest, giving the answers Python gives. It ports inflection 0.5.1.
100%
4,736 of 4,736 calls recorded from the Python original answer the same in the port. Replayed again on .
What it ports
camelize, dasherize, humanize, ordinal, ordinalize, parameterize, pluralize, singularize, tableize, titleize, transliterate, underscore.
Install
The package is shapeshift-inflection, version 0.1.0. It is not on crates.io yet; until it is, install it from the GitHub release. Needs a stable Rust toolchain, edition 2021.
cargo add shapeshift-inflection --git https://github.com/Pushkal-Gupta/shapeshift-inflection --tag v0.1.0Once it is on crates.io: cargo add shapeshift-inflection
Use it
use shapeshift_inflection::{camelize, ordinalize, parameterize, pluralize, singularize, underscore};
assert_eq!(pluralize("octopus"), "octopi");
assert_eq!(singularize("people"), "person");
assert_eq!(camelize("device_type", true).unwrap(), "DeviceType");
assert_eq!(underscore("HTMLTidyGenerator"), "html_tidy_generator");
assert_eq!(parameterize("Donald E. Knuth", "-").unwrap(), "donald-e-knuth");
assert_eq!(ordinalize(1003), "1003rd");Checked by the crate's doctest in src/lib.rs, run by cargo test.
Why use the port
The same words from Rust and Python
Plurals, table names, slugs and titles come out as the Python library makes them, including the parts that come from Python's re and str rules.
No Python at run time
A Rust crate with three dependencies: regex, serde_json and unicode-normalization.
Faster on the bench
100,000 pluralize calls ran in less than a tenth of the Python time on the bench below.
Errors as values
Functions that can raise in Python return a Result whose PyError names the Python exception type.
Speed, as measured in the repository
100,000 pluralize calls over the same ten words, best of five runs, run three times on one arm64 machine under macOS (src/bin/bench.rs and the timeit line in the README). Times for 100,000 calls; lower is faster. Other machines will differ.
| Run | Time |
|---|---|
| Python 3.12.3, inflection 0.5.1 | 451 to 457 ms |
| Rust 1.99.0, this port | 38.5 to 40.0 ms |
The tapes
A tape is a file of calls recorded from the original while it ran: the arguments, and the result or the exception type. A call matches when the port gives the same result or raises the same exception type. The counted tapes make up the headline figure.
| Tape | Where the calls came from | Calls | Match | Counted |
|---|---|---|---|---|
shadow/suite-state-tape.jsonl | the original's test suite, each call with the module's uncountable word list as it stood before the call | 736 | 736 | yes |
shadow/random-tape.jsonl | random words and strings from shadow/gen_random_tape.py, including non-ASCII letters, newlines and wrong argument types | 4,000 | 4,000 | yes |
shadow/suite-tape.jsonl | the same suite calls without the recorded word list; the two that differ come from a test that changes the global list, so they are expected | 736 | 734 | no |
Replay it yourself
With pgremlin installed:
git clone https://github.com/Pushkal-Gupta/shapeshift-inflection
cd shapeshift-inflection
cargo build --release --bin shapeshift-adapter
goblin shapeshift replay shadow/suite-state-tape.jsonl -- ./target/release/shapeshift-adapter
goblin shapeshift replay shadow/random-tape.jsonl -- ./target/release/shapeshift-adapter
goblin shapeshift replay shadow/suite-tape.jsonl -- ./target/release/shapeshift-adapterOr with the public checker, Shapeshifter, from the repository root:
shapeshifter replay shadow/suite-state-tape.jsonl -- ./target/release/shapeshift-adapter
shapeshifter replay shadow/random-tape.jsonl -- ./target/release/shapeshift-adapter
shapeshifter replay shadow/suite-tape.jsonl -- ./target/release/shapeshift-adapterWhat the tapes do not cover
- Strings with non-ASCII decimal digits passed to ordinal and ordinalize: the original accepts them and the port refuses them. No tape held such a call; it is open as issue 1 in the repository.
- The module's rule lists are fixed; only the uncountable word list can be replaced, through the _with functions.
- The titlecase table was generated from Python 3.14, Unicode 16.0.
Links
- Repository, tapes and log: github.com/Pushkal-Gupta/shapeshift-inflection
- Release: shapeshift-inflection v0.1.0
- Licence: MIT, the licence of the original