python_ast/ast/tree/
attribute.rs1use proc_macro2::TokenStream;
2use pyo3::FromPyObject;
3use quote::{format_ident, quote};
4
5use crate::{dump, CodeGen, CodeGenContext, ExprType, Node, PythonOptions, SymbolTableScopes};
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
10pub struct Attribute {
12 value: Box<ExprType>,
13 attr: String,
14 ctx: String,
15}
16
17impl<'a> FromPyObject<'a> for Attribute {
18 fn extract(ob: &pyo3::PyAny) -> pyo3::PyResult<Self> {
19 let value = ob.getattr("value").expect("Attribute.value");
20 let attr = ob.getattr("attr").expect("Attribute.attr");
21 let ctx = ob
22 .getattr("ctx")
23 .expect("getting attribute context")
24 .get_type()
25 .name()
26 .expect(
27 ob.error_message(
28 "<unknown>",
29 format!("extracting type name {:?} in attribute", dump(ob, None)),
30 )
31 .as_str(),
32 );
33 Ok(Attribute {
34 value: Box::new(ExprType::extract(&value).expect("Attribute.value")),
35 attr: attr.extract().expect("Attribute.attr"),
36 ctx: ctx.to_string(),
37 })
38 }
39}
40
41impl<'a> CodeGen for Attribute {
42 type Context = CodeGenContext;
43 type Options = PythonOptions;
44 type SymbolTable = SymbolTableScopes;
45
46 fn to_rust(
47 self,
48 _ctx: Self::Context,
49 _options: Self::Options,
50 _symbols: Self::SymbolTable,
51 ) -> Result<TokenStream, Box<dyn std::error::Error>> {
52 let name = self
53 .value
54 .to_rust(_ctx, _options, _symbols)
55 .expect("Attribute.value");
56 let attr = format_ident!("{}", self.attr);
57 Ok(quote!(#name.#attr))
58 }
59}