3 unstable releases

Uses old Rust 2015

0.2.0 Jan 14, 2018
0.1.1 Aug 9, 2017
0.1.0 Jul 17, 2017

#2503 in Rust patterns

Download history 132/week @ 2023-11-18 135/week @ 2023-11-25 67/week @ 2023-12-02 114/week @ 2023-12-09 139/week @ 2023-12-16 104/week @ 2023-12-23 58/week @ 2023-12-30 122/week @ 2024-01-06 138/week @ 2024-01-13 93/week @ 2024-01-20 85/week @ 2024-01-27 66/week @ 2024-02-03 130/week @ 2024-02-10 213/week @ 2024-02-17 119/week @ 2024-02-24 118/week @ 2024-03-02

604 downloads per month
Used in 16 crates (via mauzi_macros)

MIT/Apache

31KB
789 lines

literalext

Build Status

WARNING: This crate is no longer maintained. The literal parsing logic in this crate has been moved into syn 0.12.

To get similar behaviour with syn, parse a syn::Lit by calling syn::parse2::<syn::Lit>(ts) or syn::parse_str::<syn::Lit>(s).

This crate provides extension methods to proc-macro, and proc-macro2's Literal types. These methods provide a mechanism for extracting the value of the type.

API

Adds a trait with implementations for the types proc_macro2::Literal, proc_macro::Literal, and DummyLiteral with the following methods for extracting the value of the type:

pub trait LiteralExt {
    /// If the `Literal` is an integer literal, returns its value.
    fn parse_int(&self) -> Option<IntLit>;

    /// If the `Literal` is a floating point literal, returns its value.
    fn parse_float(&self) -> Option<FloatLit>;

    /// If the `Literal` is a string literal, returns it's value.
    fn parse_string(&self) -> Option<String>;

    /// If the `Literal` is a char literal, returns it's value.
    fn parse_char(&self) -> Option<char>;

    /// If the `Literal` is a byte string literal, returns it's value.
    fn parse_bytes(&self) -> Option<Vec<u8>>;

    /// If the `Literal` is a byte literal, returns it's value.
    fn parse_byte(&self) -> Option<u8>;

    /// If the `Literal` is an inner doc comment (`//!` or `/*!`), returns a
    /// string with the text of the comment.
    fn parse_inner_doc(&self) -> Option<String>;

    /// If the `Literal` is an outer doc comment (`///` or `/**`), returns a
    /// string with the text of the comment.
    fn parse_outer_doc(&self) -> Option<String>;
}

Supported Features

  • i128: Add support for interpreting the i128 and u128 integer types. nightly only

  • proc-macro2 default: Implement LiteralExt on proc_macro2::Literal.

  • proc-macro: Implement LiteralExt on proc_macro::Literal. nightly only

  • dummy: Export a type DummyLiteral with a public constructor which implements the LiteralExt trait.

Dependencies

~220KB