#reserved #string #keyword

check_keyword

A trait for String-like types to check if it is a reserved keyword and convert it to a safe non-keyword if so

3 unstable releases

0.2.0 Dec 3, 2021
0.1.1 Jun 13, 2021
0.1.0 Jun 13, 2021

#694 in Rust patterns

Download history 342/week @ 2023-12-16 268/week @ 2023-12-23 151/week @ 2023-12-30 429/week @ 2024-01-06 302/week @ 2024-01-13 527/week @ 2024-01-20 445/week @ 2024-01-27 572/week @ 2024-02-03 417/week @ 2024-02-10 539/week @ 2024-02-17 543/week @ 2024-02-24 320/week @ 2024-03-02 439/week @ 2024-03-09 672/week @ 2024-03-16 638/week @ 2024-03-23 568/week @ 2024-03-30

2,349 downloads per month
Used in 22 crates (7 directly)

MIT/Apache

9KB
124 lines

check_keyword

A trait for String-like types to check if a string is a reserved keyword, and convert it to a safe non-keyword if so.

Only strict and reserved keywords are checked against; weak keywords are not included.

You can add this dependency with:

[dependencies]
check_keyword = "0.2"

Example

use check_keyword::CheckKeyword;
let keyword = "match";

assert!(keyword.is_keyword());
assert_eq!(keyword.into_safe(), "r#match");

The CheckKeyword::into_safe method automatically checks CheckKeyword::is_keyword for you. You don't need to call CheckKeyword::is_keyword if you don't care whether it was originally a keyword or not.

Implementations

There is a special implementation of CheckKeyword<String> for [&str], and a blanket implementation of CheckKeyword<T> where T: AsRef<str> + From<String>.

The blanket implementation covers [String], and is only tested for that, but should cover any other String-like type as well. I can try to broaden the definition to fit other types if needed (open an issue).

Rust Editions

By default, the keywords added in Rust Edition 2018 are included in the list of checked keywords. This can be disabled with default-features = false in your Cargo.toml.

[dependencies]
check_keyword = { version = "0.2", default-features = false }

Future Rust editions may add new keywords, and this crate will be updated to reflect that. (Or you can create an issue on github if I don't.)

License: MIT OR Apache-2.0

No runtime deps

Features