litext 0.3.0

Just what you need for extracting string literal contents at compile time
Documentation
use proc_macro::Span;

/// A parsed string literal, bundling the extracted text with its source location.
///
/// `LitStr` is returned by [`extract_litstr`] when you need more than just the
/// string contents, specifically when you need to emit diagnostics that point
/// back at the original literal in the macro input.
///
/// # Example
///
/// ```ignore
/// use litext::{litext, TokenStream};
///
/// pub fn my_macro(input: TokenStream) -> TokenStream {
///     let lit = litext!(input as LitStr);
///     println!("value: {}", lit.value());
///     // lit.span() can be used with Diagnostic::spanned for precise errors
/// }
/// ```
pub struct LitStr {
    /// The inner text of the string literal, with quotes and escape sequences removed.
    ///
    /// For a literal like `"hello\nworld"` this will be `hello` + newline + `world`.
    /// For a raw literal like `r#"hello\nworld"#` this will be `hello\nworld` verbatim,
    /// since raw strings do not process escape sequences.
    pub value: String,

    /// The source location of the string literal in the macro input.
    ///
    /// Useful for emitting compiler diagnostics that point at the exact position
    /// of the literal, for example via `Diagnostic::spanned` on nightly, or by
    /// constructing a new token with this span attached.
    pub span: Span,
}

impl LitStr {
    /// Creates a new `LitStr` from an extracted string value and its source span.
    ///
    /// This is called internally by [`extract_litstr`] after successfully parsing
    /// a string literal token. The span records where the literal appeared in
    /// the macro input so that downstream proc-macro code can emit well-located
    /// diagnostics.
    pub fn new(value: String, span: Span) -> Self {
        Self { value, span }
    }

    /// Returns the inner text of the string literal.
    ///
    /// For `"hello world"` this returns `hello world`.
    pub fn value(&self) -> &str {
        &self.value
    }

    /// Returns the source span of the literal.
    ///
    /// Useful when you want to attach a compiler error to the exact location
    /// of the string in the macro input, for example via `Diagnostic::spanned`.
    pub fn span(&self) -> Span {
        self.span
    }
}