#keyword #string #reserved

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

Uses new Rust 2021

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

#861 in Rust patterns

Download history 173/week @ 2022-08-14 183/week @ 2022-08-21 84/week @ 2022-08-28 88/week @ 2022-09-04 69/week @ 2022-09-11 94/week @ 2022-09-18 100/week @ 2022-09-25 83/week @ 2022-10-02 53/week @ 2022-10-09 236/week @ 2022-10-16 539/week @ 2022-10-23 242/week @ 2022-10-30 242/week @ 2022-11-06 171/week @ 2022-11-13 110/week @ 2022-11-20 228/week @ 2022-11-27

758 downloads per month
Used in 8 crates (4 directly)

MIT/Apache

8KB
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

  • 2018