anchor_syn/parser/
error.rs1use crate::{Error, ErrorArgs, ErrorCode};
2use syn::parse::{Parse, Result as ParseResult};
3use syn::Expr;
4
5pub fn parse(error_enum: &mut syn::ItemEnum, args: Option<ErrorArgs>) -> Error {
7 let ident = error_enum.ident.clone();
8 let mut last_discriminant = 0;
9 let codes: Vec<ErrorCode> = error_enum
10 .variants
11 .iter_mut()
12 .map(|variant: &mut syn::Variant| {
13 let msg = parse_error_attribute(variant);
14 let ident = variant.ident.clone();
15 let id = match &variant.discriminant {
16 None => last_discriminant,
17 Some((_, disc)) => match disc {
18 syn::Expr::Lit(expr_lit) => match &expr_lit.lit {
19 syn::Lit::Int(int) => {
20 int.base10_parse::<u32>().expect("Must be a base 10 number")
21 }
22 _ => panic!("Invalid error discriminant"),
23 },
24 _ => panic!("Invalid error discriminant"),
25 },
26 };
27 last_discriminant = id + 1;
28
29 variant
31 .attrs
32 .retain(|attr| attr.path.segments[0].ident == "doc");
33
34 ErrorCode { id, ident, msg }
35 })
36 .collect();
37 Error {
38 name: error_enum.ident.to_string(),
39 raw_enum: error_enum.clone(),
40 ident,
41 codes,
42 args,
43 }
44}
45
46fn parse_error_attribute(variant: &syn::Variant) -> Option<String> {
47 let attrs = variant
48 .attrs
49 .iter()
50 .filter(|attr| attr.path.segments[0].ident != "doc")
51 .collect::<Vec<_>>();
52 match attrs.len() {
53 0 => None,
54 1 => {
55 let attr = &attrs[0];
56 let attr_str = attr.path.segments[0].ident.to_string();
57 assert!(&attr_str == "msg", "Use msg to specify error strings");
58
59 let mut tts = attr.tokens.clone().into_iter();
60 let g_stream = match tts.next().expect("Must have a token group") {
61 proc_macro2::TokenTree::Group(g) => g.stream(),
62 _ => panic!("Invalid syntax"),
63 };
64
65 let msg = match g_stream.into_iter().next() {
66 None => panic!("Must specify a message string"),
67 Some(msg) => msg.to_string().replace('\"', ""),
68 };
69
70 Some(msg)
71 }
72 _ => {
73 panic!("Too many attributes found. Use `msg` to specify error strings");
74 }
75 }
76}
77
78pub struct ErrorInput {
79 pub error_code: Expr,
80}
81
82impl Parse for ErrorInput {
83 fn parse(stream: syn::parse::ParseStream) -> ParseResult<Self> {
84 let error_code = stream.call(Expr::parse)?;
85 Ok(Self { error_code })
86 }
87}