| 1 | use crate::{generate::*, type_info::*}; |
| 2 | use std::fmt; |
| 3 | |
| 4 | /// Definition of `__new__` method. |
| 5 | #[derive (Debug, Clone, PartialEq)] |
| 6 | pub struct NewDef { |
| 7 | pub args: Vec<Arg>, |
| 8 | } |
| 9 | |
| 10 | impl Import for NewDef { |
| 11 | fn import(&self) -> HashSet<ModuleRef> { |
| 12 | let mut import: HashSet = HashSet::new(); |
| 13 | for arg: &Arg in &self.args { |
| 14 | import.extend(arg.import().into_iter()); |
| 15 | } |
| 16 | import |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | impl From<&NewInfo> for NewDef { |
| 21 | fn from(info: &NewInfo) -> Self { |
| 22 | Self { |
| 23 | args: info.args.iter().map(Arg::from).collect(), |
| 24 | } |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl fmt::Display for NewDef { |
| 29 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 30 | let indent: &'static str = indent(); |
| 31 | write!(f, " {indent}def __new__(cls," )?; |
| 32 | for (n: usize, arg: &Arg) in self.args.iter().enumerate() { |
| 33 | write!(f, " {}" , arg)?; |
| 34 | if n != self.args.len() - 1 { |
| 35 | write!(f, ", " )?; |
| 36 | } |
| 37 | } |
| 38 | writeln!(f, "): ..." )?; |
| 39 | Ok(()) |
| 40 | } |
| 41 | } |
| 42 | |