Skip to main content

mist_parser/parser/items/
class.rs

1use crate::{
2    Rule,
3    ast::*,
4    error::{AstError, IntoErr},
5    parser::consume_rule,
6};
7
8impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassConstructor {
9    type Error = AstError<'a, Self>;
10
11    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
12        let mut inner = pair.into_inner();
13
14        Ok(Self {
15            visibility: Visibility::try_from(&mut inner).get()?,
16
17            generics: consume_rule(&mut inner, Rule::generics_decl)
18                .map(GenericsDecl::try_from)
19                .transpose()
20                .get()?
21                .unwrap_or_default(),
22
23            params: consume_rule(&mut inner, Rule::param_list)
24                .map(ParamList::try_from)
25                .transpose()
26                .get()?
27                .unwrap_or_default(),
28
29            body: inner.next().unwrap().try_into().get()?,
30        })
31    }
32}
33
34impl<'a> TryFrom<pest::iterators::Pair<'a, Rule>> for ClassItem {
35    type Error = AstError<'a, Self>;
36
37    fn try_from(pair: pest::iterators::Pair<'a, Rule>) -> Result<Self, Self::Error> {
38        let rule = pair.as_rule();
39
40        match rule {
41            Rule::impl_decl | Rule::impl_for_decl => {
42                Ok(ClassItem::ImplDecl(pair.try_into().get()?))
43            }
44
45            Rule::function_decl => Ok(ClassItem::Method(pair.try_into().get()?)),
46
47            _ => AstError::bug_unimplemented(pair),
48        }
49    }
50}