#compiler-plugin #string-literal #plugin

nightly rustplacements

A rust compiler plugin for modifying string literals

1 unstable release

Uses old Rust 2015

0.1.0 Aug 27, 2017

#788 in Programming languages

MIT license

21KB
281 lines

Rustplacements

This is a compiler plugin for the Rust language that replaces all of your string literals in the source code with random text. Well, it's not really random. You can choose to replace text with items from any of the lists on this page by simply adding a few attributes to your existing Rust code.

A Brief Example

Let's start with a simple example like the one below. It prints out the words in the sentence below one word at a time.

const SENTENCE: [&'static str; 9] = ["The", "Quick", "Brown", "Fox", "Jumped", "Over", "the",
                                     "Lazy", "Dog"];

fn main() {
    for word in &SENTENCE {
        println!("{}", word);
    }
}

The output should look like:

The
Quick
Brown
Fox
Jumped
Over
the
Lazy
Dog

Rustplacements let's us replace all the strings at compile with other values. Let's say we want to replace all the text with emojis. Rustplacements can do that.

#![feature(plugin)]
#![plugin(rustplacements)]

// Placing it in the module root will replace everything in the module
#![Rustplacements = "emojis"]

const SENTENCE: [&'static str; 9] = ["The", "Quick", "Brown", "Fox", "Jumped", "Over", "the",
                                     "Lazy", "Dog"];

fn main() {
    for word in &SENTENCE {
        println!("{}", word);
    }
}

The new output will look something like this. The output is randomized so it will be re-generated everytime you compile your crate.

😢 😫 🤓
😞 😠 😟 😖 😧
😬 😬 😈 😡 😟
😓 😒 😬
😝 😘 🤧 😬 😧 😡
😗 😈 😉 😫
😄 😱 😰
😃 🤡 😅 😯
🤒 😈 😈

Using Rustplacements

Compiler plugins like Rustplacements are only available on nightly rust because they require a feature flag to use. To get started, Rustplacements is available on crates.io. To download the latest version, add the following line to the Cargo.toml.

[dependencies]
rustplacements = "*"

To enable the compiler plugin, add the following lines on the top of your main.rs or lib.rs.

#![feature(plugin)]
#![plugin(rustplacements)]

You can now use the plugin anywhere in the crate by applying the #[Rustplacements = "one-direction"] to any language element. You can place the element in the root with #![Rustplacements = "got-quotes"] and it will transform all the string literals in your module. It can also be applied to specific strings / impls / functions for more fine grained control.

That's pretty much all there is to it. Check out the categories page for more categories that you can use.

Contributing

Do you have a category that you'd like to add? Feel free to open a PR with your new category, or edits to an existing category. You'll need to change exprs.rs and the CATEGORIES.md file to include the new category. See the contributing page for more details. All Pull Requests are expected to be professional, inclusive of others, and generally non-offensive. We'll err on the side of more conservative if there's any debate about the appropriateness of a category.

Dependencies

~330–560KB