Skip to main content

typhoon_syn/
context.rs

1use {
2    crate::{account::InstructionAccount, arguments::Arguments},
3    syn::{Ident, ItemStruct},
4};
5
6pub struct Context {
7    pub name: Ident,
8    pub accounts: Vec<InstructionAccount>,
9    pub arguments: Option<Arguments>,
10}
11
12impl TryFrom<&ItemStruct> for Context {
13    type Error = syn::Error;
14
15    fn try_from(value: &ItemStruct) -> Result<Self, Self::Error> {
16        let accounts = value
17            .fields
18            .iter()
19            .map(InstructionAccount::try_from)
20            .collect::<Result<_, _>>()?;
21
22        let arguments = value
23            .attrs
24            .iter()
25            .find(|attr| attr.meta.path().is_ident("args"))
26            .and_then(|attr| Arguments::try_from(attr).ok());
27
28        Ok(Context {
29            name: value.ident.clone(),
30            accounts,
31            arguments,
32        })
33    }
34}