kotlin_poet_rs/spec/
class_inheritance_modifier.rs

1use crate::io::RenderKotlin;
2use crate::spec::CodeBlock;
3use crate::tokens;
4
5/// Inheritance modifiers applicable to class-like entities.
6/// Enables converting class to interface, enum e.t.c.
7/// Defaults to [ClassInheritanceModifier::Final], lack of inheritance modifier is represented as default.
8///
9/// Class / File members use [crate::spec::MemberInheritanceModifier] instead.
10#[derive(Debug, Clone, Default)]
11pub enum ClassInheritanceModifier {
12    /// Corresponds open classes a.k.a. classes that can be inherited.
13    Open,
14    /// Default, no inheritance allowed.
15    #[default]
16    Final,
17    /// Denotes that class-like entity is interface.
18    Interface,
19    /// Denotes abstract classes
20    Abstract,
21    /// Denotes sealed class, for simplicity abstract keyword is omitted
22    Sealed,
23    /// Denotes that class-like entry is standalone object, for companion objects see [crate::spec::CompanionObject]
24    Object,
25    /// Denotes that class-like entity is enum
26    Enum,
27    /// Denotes that class-like entity is data, for simplicity final keyword is omitted
28    Data,
29}
30
31impl RenderKotlin for ClassInheritanceModifier {
32    fn render_into(&self, block: &mut CodeBlock) {
33        let text = match self {
34            ClassInheritanceModifier::Open => tokens::keyword::OPEN,
35            ClassInheritanceModifier::Final => tokens::keyword::FINAL,
36            ClassInheritanceModifier::Abstract => tokens::keyword::ABSTRACT,
37            ClassInheritanceModifier::Sealed => tokens::keyword::SEALED,
38            ClassInheritanceModifier::Interface => tokens::keyword::INTERFACE,
39            ClassInheritanceModifier::Object => tokens::keyword::OBJECT,
40            ClassInheritanceModifier::Enum => tokens::keyword::ENUM,
41            ClassInheritanceModifier::Data => tokens::keyword::DATA
42        };
43
44        block.push_atom(text);
45    }
46}
47
48#[cfg(test)]
49mod tests {
50    use crate::io::RenderKotlin;
51    use crate::spec::ClassInheritanceModifier;
52
53    #[test]
54    fn test_render() {
55        assert_eq!(ClassInheritanceModifier::Open.render_string(), "open");
56        assert_eq!(ClassInheritanceModifier::Final.render_string(), "final");
57        assert_eq!(ClassInheritanceModifier::Abstract.render_string(), "abstract");
58        assert_eq!(ClassInheritanceModifier::Sealed.render_string(), "sealed");
59    }
60}