ligen_python_parser/
object.rs1use ligen::parser::ParserConfig;
2use rustpython_parser::ast::{Expr, StmtAnnAssign, StmtAssign, StmtAugAssign};
3use ligen::ir::Object;
4use crate::identifier::IdentifierParser;
5use crate::prelude::*;
6use crate::types::type_::TypeParser;
7
8#[derive(Default)]
9pub struct ObjectParser;
10
11impl Parser<WithSource<&StmtAnnAssign>> for ObjectParser {
12 type Output = Object;
13 fn parse(&self, input: WithSource<&StmtAnnAssign>, config: &ParserConfig) -> Result<Self::Output> {
14 let mut object = self.parse(input.ast.target.as_ref(), config)?;
15 if !config.get_only_parse_symbols() {
16 object.type_ = TypeParser::new().parse(input.sub(&*input.ast.annotation), config)?;
17 }
18 Ok(object)
19 }
20}
21
22impl Parser<&StmtAugAssign> for ObjectParser {
23 type Output = Object;
24 fn parse(&self, input: &StmtAugAssign, config: &ParserConfig) -> Result<Self::Output> {
25 self.parse(input.target.as_ref(), config)
26 }
27}
28
29impl Parser<&Expr> for ObjectParser {
30 type Output = Object;
31 fn parse(&self, expr: &Expr, config: &ParserConfig) -> Result<Self::Output> {
32 let identifier = expr
33 .as_name_expr()
34 .ok_or(Error::Message("Expected identifier".into()))?
35 .id
36 .as_str();
37 let identifier_parser = IdentifierParser::new();
38 let identifier = identifier_parser.parse(identifier, config)?;
39 if config.get_only_parse_symbols() {
40 Ok(Object { identifier, ..Default::default() })
41 } else {
42 let mutability = identifier_parser.get_mutability(&identifier);
43 let type_ = Default::default();
44 let literal = Default::default();
45 Ok(Object { identifier, mutability, literal, type_ })
46 }
47 }
48}
49
50impl Parser<&StmtAssign> for ObjectParser {
51 type Output = Vec<Object>;
52 fn parse(&self, input: &StmtAssign, config: &ParserConfig) -> Result<Self::Output> {
53 let mut objects = Vec::new();
54 for target in &input.targets {
55 if let Ok(object) = self.parse(target, config) {
56 objects.push(object);
57 }
58 }
59 Ok(objects)
60 }
61}
62