Debian Codegraph @

debian/rust-vendor/litrs/README.md

raw
1 # `litrs`: parsing and inspecting Rust literals
2
3 [<img alt="CI status of main" src="https://img.shields.io/github/actions/workflow/status/LukasKalbertodt/litrs/ci.yml?branch=main&label=CI&logo=github&logoColor=white&style=for-the-badge" height="23">](https://github.com/LukasKalbertodt/litrs/actions/workflows/ci.yml)
4 [<img alt="Crates.io Version" src="https://img.shields.io/crates/v/litrs?logo=rust&style=for-the-badge" height="23">](https://crates.io/crates/litrs)
5 [<img alt="docs.rs" src="https://img.shields.io/crates/v/litrs?color=blue&label=docs&style=for-the-badge" height="23">](https://docs.rs/litrs)
6
7 `litrs` offers functionality to parse Rust literals, i.e. tokens in the Rust programming language that represent fixed values.
8 For example: `27`, `"crab"`, `bool`.
9 This is particularly useful for proc macros, but can also be used outside of a proc-macro context.
10
11 **Why this library?**
12 Unfortunately, the `proc_macro` API shipped with the compiler offers no easy way to inspect literals.
13 There are mainly two libraries for this purpose:
14 [`syn`](https://github.com/dtolnay/syn) and [`literalext`](https://github.com/mystor/literalext).
15 The latter is deprecated.
16 And `syn` is oftentimes overkill for the task at hand, especially when developing function-like proc-macros (e.g. `foo!(..)`).
17 This crate is a lightweight alternative.
18 Also, when it comes to literals, `litrs` offers a bit more flexibility and a few more features compared to `syn`.
19
20 ## Example
21
22 ### In proc macro
23
24 ```rust
25 use std::convert::TryFrom;
26 use proc_macro::TokenStream;
27 use litrs::Literal;
28
29 #[proc_macro]
30 pub fn foo(input: TokenStream) -> TokenStream {
31 // Please do proper error handling in your real code!
32 let first_token = input.into_iter().next().expect("no input");
33
34 // `try_from` will return an error if the token is not a literal.
35 match Literal::try_from(first_token) {
36 // Convenient methods to produce decent errors via `compile_error!`.
37 Err(e) => return e.to_compile_error(),
38
39 // You can now inspect your literal!
40 Ok(Literal::Integer(i)) => {
41 println!("Got an integer specified in base {:?}", i.base());
42
43 let value = i.value::<u64>().expect("integer literal too large");
44 println!("Is your integer even? {}", value % 2 == 0);
45 }
46 Ok(other) => {
47 println!("Got a non-integer literal");
48 }
49 }
50
51 TokenStream::new() // dummy output
52 }
53 ```
54
55 If you are expecting a specific kind of literal, you can also use this, which will return an error if the token is not a float literal.
56
57 ```rust
58 FloatLit::try_from(first_token)
59 ```
60
61 ### Parsing from a `&str`
62
63 Outside of a proc macro context you might want to parse a string directly.
64
65 ```rust
66 use litrs::{FloatLit, Literal};
67
68 let lit = Literal::parse("'🦀'").expect("failed to parse literal");
69 let float_lit = FloatLit::parse("2.7e3").expect("failed to parse as float literal");
70 ```
71
72 See [**the documentation**](https://docs.rs/litrs) or the `examples/` directory for more examples and information.
73
74
75 <br />
76
77 ---
78
79 ## License
80
81 Licensed under either of <a href="LICENSE-APACHE">Apache License, Version
82 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option.
83 Unless you explicitly state otherwise, any contribution intentionally submitted
84 for inclusion in this project by you, as defined in the Apache-2.0 license,
85 shall be dual licensed as above, without any additional terms or conditions.