pyo3_stub_gen/generate/
arg.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::{generate::Import, stub_type::ModuleRef, type_info::*, TypeInfo};
use std::{collections::HashSet, fmt};

#[derive(Debug, Clone, PartialEq)]
pub struct Arg {
    pub name: &'static str,
    pub r#type: TypeInfo,
    pub signature: Option<SignatureArg>,
}

impl Import for Arg {
    fn import(&self) -> HashSet<ModuleRef> {
        self.r#type.import.clone()
    }
}

impl From<&ArgInfo> for Arg {
    fn from(info: &ArgInfo) -> Self {
        Self {
            name: info.name,
            r#type: (info.r#type)(),
            signature: info.signature.clone(),
        }
    }
}

impl fmt::Display for Arg {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        if let Some(signature) = &self.signature {
            match signature {
                SignatureArg::Ident => write!(f, "{}:{}", self.name, self.r#type),
                SignatureArg::Assign { default } => {
                    let default: &String = default;
                    write!(f, "{}:{}={}", self.name, self.r#type, default)
                }
                SignatureArg::Star => write!(f, "*"),
                SignatureArg::Args => write!(f, "*{}", self.name),
                SignatureArg::Keywords => write!(f, "**{}", self.name),
            }
        } else {
            write!(f, "{}:{}", self.name, self.r#type)
        }
    }
}