Skip to main content

reda_lib/io/read/
semantic.rs

1use std::collections::HashMap;
2use crate::{error::LibError, model::{LibAst, LibGroup}};
3
4pub trait FromLibGroup: Sized {
5    type Error: Sized;
6    fn from_group(group: LibGroup) -> Result<Self, Self::Error>;
7}
8
9impl LibGroup {
10    pub fn from_ast(ast: LibAst) -> Result<Self, LibError> {
11        match ast {
12            LibAst::LibGroup { kind, params, items } => {
13                let mut attrs = HashMap::new();
14                let mut groups = Vec::new();
15                
16                for item_ast in items {
17                    if let LibAst::Attr { key, value } = item_ast {
18                        attrs.insert(key, value);
19                    } else {
20                        groups.push(LibGroup::from_ast(item_ast)?);
21                    }
22                }
23
24                Ok(Self {
25                    kind, 
26                    params,
27                    attrs,
28                    groups
29                })
30            }
31            LibAst::Attr { key:_, value:_ } => Err(LibError::ExceptLibGroup),
32        }
33    }
34}