ryo_source/generator/mod.rs
1//! Source code generation from Pure AST.
2//!
3//! This module provides generators that convert Pure AST structures into
4//! Rust source code. Different generators support different output strategies:
5//!
6//! - `SingleFileGenerator`: Generates a single lib.rs/main.rs with nested `mod {}` blocks
7//! - `MultiFileGenerator`: Generates multiple files following Rust module conventions
8//!
9//! # Architecture
10//!
11//! ```text
12//! ModuleTree ──► SingleFileGenerator ──► PureFile ──► String (source code)
13//! │
14//! ├── Hierarchical module structure with items
15//! │
16//! └─► MultiFileGenerator ──► GeneratedFiles (PathBuf → GeneratedSource)
17//! ```
18//!
19//! # Example
20//!
21//! ```ignore
22//! use ryo_source::generator::{ModuleTree, SingleFileGenerator, MultiFileGenerator};
23//!
24//! let tree = ModuleTree::new("crate")
25//! .with_item(PureItem::Struct(my_struct))
26//! .with_child(ModuleTree::new("utils")
27//! .with_item(PureItem::Fn(helper_fn)));
28//!
29//! // Single file output
30//! let generator = SingleFileGenerator::new();
31//! let source = generator.generate(&tree);
32//!
33//! // Multi-file output
34//! let generator = MultiFileGenerator::new();
35//! let files = generator.generate(&tree);
36//! // files.get("lib.rs"), files.get("utils.rs"), etc.
37//! ```
38
39mod multi;
40mod single;
41mod tree;
42
43pub use multi::{GeneratedFiles, MultiFileGenerator};
44pub use single::SingleFileGenerator;
45pub use tree::ModuleTree;
46
47use crate::pure::PureFile;
48
49/// Result of source generation.
50#[derive(Debug, Clone)]
51pub struct GeneratedSource {
52 /// The generated source code.
53 pub source: String,
54 /// The PureFile representation (for further manipulation if needed).
55 pub pure_file: PureFile,
56}
57
58/// Trait for source code generators.
59///
60/// Generators transform a `ModuleTree` into source code, supporting
61/// different output strategies (single file, multi-file, etc.).
62pub trait SourceGenerator {
63 /// Generate source code from a module tree.
64 fn generate(&self, tree: &ModuleTree) -> Result<GeneratedSource, crate::pure::ToSynError>;
65}