pub struct LitStr { /* private fields */ }
Expand description
A UTF-8 string literal: "foo"
.
This type is available if Syn is built with the "derive"
or
"full"
feature.
Implementations§
source§impl LitStr
impl LitStr
pub fn new(value: &str, span: Span) -> Self
pub fn value(&self) -> String
sourcepub fn parse<T: Parse>(&self) -> Result<T, Error>
pub fn parse<T: Parse>(&self) -> Result<T, Error>
Parse a syntax tree node from the content of this string literal.
All spans in the syntax tree will point to the span of this LitStr
.
Example
use proc_macro2::Span;
use syn::{Attribute, Ident, Lit, Meta, MetaNameValue, Path};
use syn::parse::{Error, Result};
// Parses the path from an attribute that looks like:
//
// #[path = "a::b::c"]
//
// or returns `None` if the input is some other attribute.
fn get_path(attr: &Attribute) -> Result<Option<Path>> {
if !attr.path.is_ident("path") {
return Ok(None);
}
match attr.parse_meta()? {
Meta::NameValue(MetaNameValue { lit: Lit::Str(lit_str), .. }) => {
lit_str.parse().map(Some)
}
_ => {
let error_span = attr.bracket_token.span;
let message = "expected #[path = \"...\"]";
Err(Error::new(error_span, message))
}
}
}