abi_stable_derive/
parse_utils.rs
1use as_derive_utils::ret_err_on_peek;
4
5use syn::{parse, punctuated::Punctuated, token::Add, TypeParamBound};
6
7use proc_macro2::Span;
8
9pub(crate) fn parse_str_as_ident(lit: &str) -> syn::Ident {
12 syn::Ident::new(lit, Span::call_site())
13}
14
15pub(crate) fn parse_str_as_path(lit: &str) -> Result<syn::Path, syn::Error> {
16 syn::parse_str(lit)
17}
18
19pub(crate) fn parse_str_as_trait_bound(lit: &str) -> Result<syn::TraitBound, syn::Error> {
20 syn::parse_str(lit)
21}
22
23pub(crate) fn parse_str_as_type(lit: &str) -> Result<syn::Type, syn::Error> {
24 syn::parse_str(lit)
25}
26
27#[allow(dead_code)]
28pub(crate) fn parse_lit_as_type_bound(lit: &syn::LitStr) -> Result<TypeParamBound, syn::Error> {
29 lit.parse()
30}
31
32pub struct ParseBounds {
33 pub list: Punctuated<TypeParamBound, Add>,
34}
35
36impl parse::Parse for ParseBounds {
37 fn parse(input: parse::ParseStream) -> parse::Result<Self> {
38 ret_err_on_peek! {input, syn::Lit, "bound", "literal"}
39
40 let list = Punctuated::<TypeParamBound, Add>::parse_terminated(input).map_err(|e| {
41 let msg = format!("while parsing bounds: {}", e);
42 syn::Error::new(e.span(), msg)
43 })?;
44
45 if list.is_empty() {
46 Err(input.error("type bounds can't be empty"))
47 } else {
48 Ok(Self { list })
49 }
50 }
51}
52
53pub struct ParsePunctuated<T, P> {
54 pub list: Punctuated<T, P>,
55}
56
57impl<T, P> parse::Parse for ParsePunctuated<T, P>
58where
59 T: parse::Parse,
60 P: parse::Parse,
61{
62 fn parse(input: parse::ParseStream) -> parse::Result<Self> {
63 Ok(Self {
64 list: Punctuated::parse_terminated(input)?,
65 })
66 }
67}