Shapeshift

Shapeshift Ports

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.0

Once 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

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.

RunTime
Python 3.12.3, inflection 0.5.1451 to 457 ms
Rust 1.99.0, this port38.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.

TapeWhere the calls came fromCallsMatchCounted
shadow/suite-state-tape.jsonlthe original's test suite, each call with the module's uncountable word list as it stood before the call736736yes
shadow/random-tape.jsonlrandom words and strings from shadow/gen_random_tape.py, including non-ASCII letters, newlines and wrong argument types4,0004,000yes
shadow/suite-tape.jsonlthe same suite calls without the recorded word list; the two that differ come from a test that changes the global list, so they are expected736734no

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-adapter

Or 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-adapter

What the tapes do not cover