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