php_codegen/
lib.rs

1pub mod attribute;
2pub mod body;
3pub mod class;
4pub mod comment;
5pub mod constant;
6pub mod data_type;
7pub mod r#enum;
8pub mod enum_case;
9pub mod file;
10pub mod function;
11pub mod interface;
12pub mod literal;
13pub mod method;
14pub mod modifiers;
15pub mod parameter;
16pub mod property;
17pub mod r#trait;
18pub mod usage;
19
20#[derive(Debug, PartialEq, Eq, PartialOrd, Clone, Copy)]
21pub enum Indentation {
22    Spaces(usize),
23    Tabs(usize),
24}
25
26impl Indentation {
27    pub fn value(&self, level: usize) -> String {
28        self.to_string().repeat(level)
29    }
30
31    pub fn indent<T: ToString>(&self, code: T, level: usize) -> String {
32        let indentation = self.value(level);
33
34        code.to_string()
35            .lines()
36            .map(|line| format!("{}{}", indentation, line.trim_end()))
37            .collect::<Vec<String>>()
38            .join("\n")
39    }
40}
41
42impl std::fmt::Display for Indentation {
43    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
44        match self {
45            Indentation::Spaces(count) => write!(f, "{:1$}", " ", count),
46            Indentation::Tabs(count) => write!(f, "{:1$}", "\t", count),
47        }
48    }
49}
50
51impl Default for Indentation {
52    fn default() -> Self {
53        Indentation::Spaces(4)
54    }
55}
56
57pub trait Generator {
58    fn generate(&self, indentation: Indentation, level: usize) -> String;
59}