Skip to main content

oxilean_codegen/ruby_backend/
rubyclassdef_traits.rs

1//! # RubyClassDef - Trait Implementations
2//!
3//! This module contains trait implementations for `RubyClassDef`.
4//!
5//! ## Implemented Traits
6//!
7//! - `Display`
8//!
9//! 🤖 Generated with [SplitRS](https://github.com/cool-japan/splitrs)
10
11use crate::lcnf::*;
12
13use super::functions::{fmt_ruby_class, fmt_ruby_method, fmt_ruby_module_stmt, fmt_ruby_stmt};
14use super::types::RubyClassDef;
15use std::fmt;
16
17impl std::fmt::Display for RubyClassDef {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        let super_str = if let Some(s) = &self.superclass {
20            format!(" < {}", s)
21        } else {
22            String::new()
23        };
24        writeln!(f, "class {}{}", self.name, super_str)?;
25        for inc in &self.includes {
26            writeln!(f, "  include {}", inc)?;
27        }
28        for ext in &self.extends {
29            writeln!(f, "  extend {}", ext)?;
30        }
31        for prep in &self.prepends {
32            writeln!(f, "  prepend {}", prep)?;
33        }
34        for (n, r, w) in &self.attrs {
35            if *r && *w {
36                writeln!(f, "  attr_accessor :{}", n)?;
37            } else if *r {
38                writeln!(f, "  attr_reader :{}", n)?;
39            } else if *w {
40                writeln!(f, "  attr_writer :{}", n)?;
41            }
42        }
43        for (n, v) in &self.constants {
44            writeln!(f, "  {} = {}", n, v)?;
45        }
46        for m in &self.methods {
47            writeln!(f, "  {}", m)?;
48        }
49        write!(f, "end")
50    }
51}