pub struct Error { /* fields omitted */ }
Error returned when a Syn parser cannot parse the input tokens.
Refer to the module documentation for details about parsing in Syn.
This type is available if Syn is built with the "parsing"
feature.
Usually the ParseStream::error
method will be used instead, which
automatically uses the correct span from the current position of the
parse stream.
Use Error::new
when the error needs to be triggered on some span other
than where the parse stream is currently positioned.
#[macro_use]
extern crate syn;
use syn::{Ident, LitStr};
use syn::parse::{Error, ParseStream, Result};
fn parse_name(input: ParseStream) -> Result<LitStr> {
let name_token: Ident = input.parse()?;
if name_token != "name" {
return Err(Error::new(name_token.span(), "expected `name`"));
}
input.parse::<Token![=]>()?;
let s: LitStr = input.parse()?;
Ok(s)
}
Creates an error with the specified message spanning the given syntax
tree node.
Unlike the Error::new
constructor, this constructor takes an argument
tokens
which is a syntax tree node. This allows the resulting Error
to attempt to span all tokens inside of tokens
. While you would
typically be able to use the Spanned
trait with the above Error::new
constructor, implementation limitations today mean that
Error::new_spanned
may provide a higher-quality error message on
stable Rust.
When in doubt it's recommended to stick to Error::new
(or
ParseStream::error
)!
The source location of the error.
Spans are not thread-safe so this function returns Span::call_site()
if called from a different thread than the one on which the Error
was
originally created.
Render the error as an invocation of compile_error!
.
The parse_macro_input!
macro provides a convenient way to invoke
this method correctly in a procedural macro.
Performs copy-assignment from source
. Read more
Formats the value using the given formatter. Read more
Formats the value using the given formatter. Read more
Deprecating in 1.33.0
: replaced by Error::source, which can support downcasting
The lower-level cause of this error, if any. Read more
The lower-level source of this error, if any. Read more
Converts the given value to a String
. Read more
Creates owned data from borrowed data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)
Immutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (get_type_id
)
this method will likely be replaced by an associated static
Mutably borrows from an owned value. Read more
🔬 This is a nightly-only experimental API. (try_from
)
The type returned in the event of a conversion error.
🔬 This is a nightly-only experimental API. (try_from
)