forge_tree/parser/
mod.rs

1pub mod tree_parser;
2
3pub use tree_parser::TreeParser;
4
5use crate::{Result, ForgeTreeError};
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct ProjectStructure {
11    pub root: String,
12    pub items: Vec<StructureItem>,
13    pub variables: HashMap<String, String>,
14}
15
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct StructureItem {
18    pub name: String,
19    pub path: String,
20    pub item_type: ItemType,
21    pub template: Option<String>,
22    pub content: Option<String>,
23    pub children: Vec<StructureItem>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
27pub enum ItemType {
28    Directory,
29    File,
30}
31
32pub struct Parser {
33    tree_parser: TreeParser,
34}
35
36impl Parser {
37    pub fn new() -> Self {
38        Self {
39            tree_parser: TreeParser::new(),
40        }
41    }
42
43    pub fn parse(&self, input: &str) -> Result<ProjectStructure> {
44        self.tree_parser.parse(input)
45    }
46
47    pub fn parse_file(&self, path: &str) -> Result<ProjectStructure> {
48        let content = std::fs::read_to_string(path)
49            .map_err(|e| ForgeTreeError::Io(e))?;
50        self.parse(&content)
51    }
52}
53
54impl Default for Parser {
55    fn default() -> Self {
56        Self::new()
57    }
58}