baobao_codegen/
indent.rs

1//! Indentation configuration for code generation.
2
3/// Indentation style for generated code.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Indent {
6    /// Spaces with the specified width (e.g., 2 or 4).
7    Spaces(u8),
8    /// Tab character.
9    Tab,
10}
11
12impl Indent {
13    /// 4-space indentation (Rust, Python).
14    pub const RUST: Self = Self::Spaces(4);
15
16    /// 2-space indentation (TypeScript, JavaScript, YAML).
17    pub const TYPESCRIPT: Self = Self::Spaces(2);
18
19    /// Tab indentation (Go).
20    pub const GO: Self = Self::Tab;
21
22    /// Convert to the string representation for one indent level.
23    pub fn as_str(&self) -> &'static str {
24        match self {
25            Self::Spaces(2) => "  ",
26            Self::Spaces(4) => "    ",
27            Self::Spaces(8) => "        ",
28            // Fallback to 4 whitespaces
29            Self::Spaces(_) => "    ",
30            Self::Tab => "\t",
31        }
32    }
33}
34
35impl Default for Indent {
36    fn default() -> Self {
37        Self::RUST
38    }
39}
40
41#[cfg(test)]
42mod tests {
43    use super::*;
44
45    #[test]
46    fn test_indent_as_str() {
47        assert_eq!(Indent::Spaces(2).as_str(), "  ");
48        assert_eq!(Indent::Spaces(4).as_str(), "    ");
49        assert_eq!(Indent::Tab.as_str(), "\t");
50    }
51
52    #[test]
53    fn test_indent_constants() {
54        assert_eq!(Indent::RUST, Indent::Spaces(4));
55        assert_eq!(Indent::TYPESCRIPT, Indent::Spaces(2));
56        assert_eq!(Indent::GO, Indent::Tab);
57    }
58
59    #[test]
60    fn test_default() {
61        assert_eq!(Indent::default(), Indent::RUST);
62    }
63}