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 the path `Self` as a default if the attribute is not of
// that form.
fn get_path(attr: &Attribute) -> Result<Path> {
let default = || Path::from(Ident::new("Self", Span::call_site()));
let meta = match attr.interpret_meta() {
Some(meta) => meta,
None => return Ok(default()),
};
if meta.name() != "path" {
return Ok(default());
}
match meta {
Meta::NameValue(MetaNameValue { lit: Lit::Str(lit_str), .. }) => {
lit_str.parse()
}
_ => {
let error_span = attr.bracket_token.span;
let message = "expected #[path = \"...\"]";
Err(Error::new(error_span, message))
}
}
}