pyo3_stub_gen/generate/
arg.rs

1use crate::{generate::Import, stub_type::ModuleRef, type_info::*, TypeInfo};
2use std::{collections::HashSet, fmt};
3
4#[derive(Debug, Clone, PartialEq)]
5pub struct Arg {
6    pub name: &'static str,
7    pub r#type: TypeInfo,
8    pub signature: Option<SignatureArg>,
9}
10
11impl Import for Arg {
12    fn import(&self) -> HashSet<ModuleRef> {
13        self.r#type.import.clone()
14    }
15}
16
17impl From<&ArgInfo> for Arg {
18    fn from(info: &ArgInfo) -> Self {
19        Self {
20            name: info.name,
21            r#type: (info.r#type)(),
22            signature: info.signature.clone(),
23        }
24    }
25}
26
27impl fmt::Display for Arg {
28    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29        if let Some(signature) = &self.signature {
30            match signature {
31                SignatureArg::Ident => write!(f, "{}:{}", self.name, self.r#type),
32                SignatureArg::Assign { default } => {
33                    let default: &String = default;
34                    write!(f, "{}:{}={}", self.name, self.r#type, default)
35                }
36                SignatureArg::Star => write!(f, "*"),
37                SignatureArg::Args => write!(f, "*{}", self.name),
38                SignatureArg::Keywords => write!(f, "**{}", self.name),
39            }
40        } else {
41            write!(f, "{}:{}", self.name, self.r#type)
42        }
43    }
44}