#string #reserved #identifier #safe #check #convert #keyword

check_keyword

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

5 unstable releases

0.3.1 Jun 17, 2024
0.3.0 Jun 17, 2024
0.2.0 Dec 3, 2021
0.1.1 Jun 13, 2021
0.1.0 Jun 13, 2021

#49 in Procedural macros

Download history 418/week @ 2024-07-21 584/week @ 2024-07-28 665/week @ 2024-08-04 664/week @ 2024-08-11 476/week @ 2024-08-18 715/week @ 2024-08-25 837/week @ 2024-09-01 554/week @ 2024-09-08 1127/week @ 2024-09-15 2524/week @ 2024-09-22 1423/week @ 2024-09-29 2575/week @ 2024-10-06 2652/week @ 2024-10-13 3054/week @ 2024-10-20 2549/week @ 2024-10-27 2752/week @ 2024-11-03

11,060 downloads per month
Used in 31 crates (11 directly)

MIT/Apache

12KB
147 lines

check_keyword

A trait for String-like types to check if a string is a keyword, and convert it to a safe non-keyword if so. All types of keywords are supported, and compile features can be used to check against past rust editions. (Default is Rust 2021.)

This library assumes the strings being checked are already valid identifiers in every way except that it might be a reserved keyword.

You can add this dependency with:

[dependencies]
check_keyword = "0.3.1"

Example

assert!(!"not_a_keyword".is_keyword());
assert_eq!("not_a_keyword".into_safe(), "not_a_keyword");

assert!("match".is_keyword());
assert_eq!("match".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.

CheckKeyword::is_keyword only checks for strict and reserved keywords. For more detail, and support for weak keywords, use CheckKeyword::keyword_status.

Implementors

This trait has a blanket implementation for all types that implement AsRef<str>. This includes &str and String.

Raw Identifiers

Raw identifiers are identifiers that start with r#, and most keywords are allowed to be used as raw identifiers.

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.3.1", default-features = false }

This crate is up-to-date with Rust 2021. 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 forget.)

License: MIT OR Apache-2.0

Dependencies

~0.6–1.1MB
~24K SLoC