python_ast/ast/tree/
keyword.rs

1use proc_macro2::TokenStream;
2use pyo3::FromPyObject;
3use quote::{format_ident, quote};
4use serde::{Deserialize, Serialize};
5
6use crate::{Arg, CodeGen, CodeGenContext, PythonOptions, SymbolTableScopes};
7
8/// A keyword argument, gnerally used in function calls.
9#[derive(Clone, Debug, Default, FromPyObject, Serialize, Deserialize, PartialEq)]
10pub struct Keyword {
11    arg: String,
12    value: Arg,
13}
14
15impl CodeGen for Keyword {
16    type Context = CodeGenContext;
17    type Options = PythonOptions;
18    type SymbolTable = SymbolTableScopes;
19
20    fn to_rust(
21        self,
22        ctx: Self::Context,
23        options: Self::Options,
24        symbols: Self::SymbolTable,
25    ) -> Result<TokenStream, Box<dyn std::error::Error>> {
26        let arg = format_ident!("{}", self.arg);
27        let value = self
28            .value
29            .clone()
30            .to_rust(ctx, options, symbols)
31            .expect(format!("parsing argument {:?}", self.value).as_str());
32        Ok(quote!(#arg = #value))
33    }
34}