|
9 | 9 | //! built. This crate also offers a bit more flexibility compared to `syn` |
10 | 10 | //! (only regarding literals, of course). |
11 | 11 | //! |
12 | | -//! --- |
| 12 | +//! |
| 13 | +//! # Quick start |
| 14 | +//! |
| 15 | +//! | **`StringLit::try_from(tt)?.value()`** | |
| 16 | +//! | - | |
| 17 | +//! |
| 18 | +//! ... where `tt` is a `proc_macro::TokenTree` and where [`StringLit`] can be |
| 19 | +//! replaced with [`Literal`] or other types of literals (e.g. [`FloatLit`]). |
| 20 | +//! Calling `value()` returns the value that is represented by the literal. |
| 21 | +//! |
| 22 | +//! **Mini Example** |
| 23 | +//! |
| 24 | +//! ```ignore |
| 25 | +//! use proc_macro::TokenStream; |
| 26 | +//! |
| 27 | +//! #[proc_macro] |
| 28 | +//! pub fn foo(input: TokenStream) -> TokenStream { |
| 29 | +//! let first_token = input.into_iter().next().unwrap(); // Do proper error handling! |
| 30 | +//! let string_value = match litrs::StringLit::try_from(first_token) { |
| 31 | +//! Ok(string_lit) => string_lit.value(), |
| 32 | +//! Err(e) => return e.to_compile_error(), |
| 33 | +//! }; |
| 34 | +//! |
| 35 | +//! // `string_value` is the string value with all escapes resolved. |
| 36 | +//! todo!() |
| 37 | +//! } |
| 38 | +//! ``` |
| 39 | +//! |
| 40 | +//! # Overview |
13 | 41 | //! |
14 | 42 | //! The main types of this library are [`Literal`], representing any kind of |
15 | 43 | //! literal, and `*Lit`, like [`StringLit`] or [`FloatLit`], representing a |
|
41 | 69 | //! |
42 | 70 | //! **Note**: `true` and `false` are `Ident`s when passed to your proc macro. |
43 | 71 | //! The `TryFrom<TokenTree>` impls check for those two special idents and |
44 | | -//! return a `BoolLit` appropriately. For that reason, there is also no |
45 | | -//! `TryFrom<proc_macro::Literal>` impl for `BoolLit`. The `proc_macro::Literal` |
| 72 | +//! return a [`BoolLit`] appropriately. For that reason, there is also no |
| 73 | +//! `TryFrom<proc_macro::Literal>` impl for [`BoolLit`]. The `proc_macro::Literal` |
46 | 74 | //! simply cannot represent bool literals. |
47 | 75 | //! |
48 | 76 | //! |
|
0 commit comments