| 1 | use crate::{generate::*, type_info::*}; |
| 2 | use std::fmt; |
| 3 | |
| 4 | /// Definition of a Python enum. |
| 5 | #[derive (Debug, Clone, PartialEq)] |
| 6 | pub struct EnumDef { |
| 7 | pub name: &'static str, |
| 8 | pub doc: &'static str, |
| 9 | pub variants: &'static [&'static str], |
| 10 | } |
| 11 | |
| 12 | impl From<&PyEnumInfo> for EnumDef { |
| 13 | fn from(info: &PyEnumInfo) -> Self { |
| 14 | Self { |
| 15 | name: info.pyclass_name, |
| 16 | doc: info.doc, |
| 17 | variants: info.variants, |
| 18 | } |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | impl fmt::Display for EnumDef { |
| 23 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 24 | writeln!(f, "class {}(Enum):" , self.name)?; |
| 25 | let indent: &'static str = indent(); |
| 26 | let doc: &str = self.doc.trim(); |
| 27 | if !doc.is_empty() { |
| 28 | writeln!(f, r#" {indent}r""""# )?; |
| 29 | for line: &str in doc.lines() { |
| 30 | writeln!(f, " {indent}{}" , line)?; |
| 31 | } |
| 32 | writeln!(f, r#" {indent}""""# )?; |
| 33 | } |
| 34 | for variants: &'static &str in self.variants { |
| 35 | writeln!(f, " {indent}{} = auto()" , variants)?; |
| 36 | } |
| 37 | writeln!(f)?; |
| 38 | Ok(()) |
| 39 | } |
| 40 | } |
| 41 | |