<div align="center">
# litext
[](https://doc.rust-lang.org/edition-guide/rust-2024/)
[](https://crates.io/crates/litext)
[](https://docs.rs/litext)
[](https://codeberg.org/razkar/litext/src/branch/main/LICENSE-MIT)
[](https://codeberg.org/razkar/litext/src/branch/main/LICENSE-APACHE)
[](https://crates.io/crates/litext)
[](https://deps.rs/repo/codeberg/razkar/litext)
[](https://codeberg.org/razkar/litext)
Seamless literal extraction for procedural macros.
```rust
let value: String = litext!(input);
let count: u32 = litext!(input as u32);
let (s, n): (String, i32) = litext!(input as (String, i32));
```
</div>
## What is this?
litext (*lee-text* /ˈliː.tɛkst/) is a crate for extracting literal values for procedural macros. It's clean, easy, extendable, and fits in a singular line.
When you write a procedural macro, you often receive a `TokenStream` that contains a single literal and need to pull out its value. `litext` does this for you, easily, in one line.
The library supports every Rust literal kind, span-aware wrapper types for precise diagnostics, a `ToTokens` round-trip trait, a `FromLit` extension point for custom types (that *you* can make!), and a variadic multi-extraction form.
## Installation
```sh
cargo add litext
```
Your proc-macro entry point needs to bridge `proc_macro` and `proc_macro2`:
```rust
#[proc_macro]
pub fn my_macro(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
my_macro_inner(input.into()).into()
}
fn my_macro_inner(input: proc_macro2::TokenStream) -> proc_macro2::TokenStream {
let s: String = litext!(input);
// ...
}
```
## Basic Usage
```rust
// Extract any literal as String
let text: String = litext!(input);
// Extract with explicit type
let num: i32 = litext!(input as i32);
let val: f64 = litext!(input as f64);
let ch: char = litext!(input as char);
let flag: bool = litext!(input as bool);
// Keep the Result instead of returning early
let result: Result<String, TokenStream> = litext!(try input);
```
See [MORE.md](MORE.md) for detailed documentation on multi-extraction, span-aware types, round-tripping, custom types, and all supported literal formats.
## Why litext?
- **Every literal kind**: strings, integers in any radix, floats, chars, bools, bytes, byte strings, C strings
- **Negative numbers**: handles `-42` for signed types automatically
- **Extract multiple at once**: pull several values from one TokenStream with a clean tuple syntax
- **Span-aware**: every type has a `Lit*` variant with `.span()` for precise error messages
- **Round-trip support**: convert back to TokenStream with original spans preserved
- **Extendable**: implement `FromLit` for your own types
## Supported Types
| Strings | `String`, `LitStr` |
| Integers | `i8`-`i128`, `isize`, `u8`-`u128`, `usize`, `LitInt<T>` |
| Floats | `f32`, `f64`, `LitFloat<T>` |
| Characters | `char`, `LitChar` |
| Booleans | `bool`, `LitBool` |
| Bytes | `u8`, `LitByte`, `Vec<u8>`, `LitByteStr` |
| C Strings | `CString`, `LitCStr` |
As of 1.2, negative literals work for signed types (`i32`, `f64`, etc. not `u32`, etc.)
## Requirements
- Rust 2024 edition (Rust 1.85)
- `proc_macro2` as a direct dependency
## License
Licensed under either of:
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or <http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
at your option.
Cheers, RazkarStudio
© 2026 RazkarStudio. All rights reserved.