pyo3_stub_gen/generate/
variable.rs1use std::{collections::HashSet, fmt};
2
3use crate::{generate::Import, stub_type::ImportRef, type_info::PyVariableInfo, TypeInfo};
4
5#[derive(Debug, Clone, PartialEq)]
6pub struct VariableDef {
7 pub name: &'static str,
8 pub type_: TypeInfo,
9 pub default: Option<String>,
10}
11
12impl From<&PyVariableInfo> for VariableDef {
13 fn from(info: &PyVariableInfo) -> Self {
14 Self {
15 name: info.name,
16 type_: (info.r#type)(),
17 default: info.default.map(|f| f()),
18 }
19 }
20}
21
22impl Import for VariableDef {
23 fn import(&self) -> HashSet<ImportRef> {
24 self.type_.import.clone()
25 }
26}
27
28impl fmt::Display for VariableDef {
29 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
30 write!(f, "{}: {}", self.name, self.type_)?;
31 if let Some(default) = &self.default {
32 write!(f, " = {default}")?;
33 }
34 Ok(())
35 }
36}
37
38impl VariableDef {
39 pub fn fmt_for_module(&self, target_module: &str, f: &mut fmt::Formatter) -> fmt::Result {
44 let qualified_type = self.type_.qualified_for_module(target_module);
45 write!(f, "{}: {}", self.name, qualified_type)?;
46 if let Some(default) = &self.default {
47 write!(f, " = {default}")?;
48 }
49 Ok(())
50 }
51}