Skip to main content

oxilean_codegen/ruby_backend/
rubymethoddef_traits.rs

1//! # RubyMethodDef - Trait Implementations
2//!
3//! This module contains trait implementations for `RubyMethodDef`.
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::{RubyMethodDef, RubyVisibility};
15use std::fmt;
16
17impl std::fmt::Display for RubyMethodDef {
18    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19        let params: Vec<String> = self
20            .params
21            .iter()
22            .map(|(n, t)| {
23                if let Some(t) = t {
24                    format!("{}: T.cast(nil, {})", n, t)
25                } else {
26                    n.clone()
27                }
28            })
29            .collect();
30        let self_prefix = if self.is_class_method { "self." } else { "" };
31        write!(
32            f,
33            "{}def {}{}({})\n  {}\nend",
34            if self.visibility != RubyVisibility::Public {
35                format!("{}\n", self.visibility)
36            } else {
37                String::new()
38            },
39            self_prefix,
40            self.name,
41            params.join(", "),
42            self.body,
43        )
44    }
45}