allframe_forge/
templates.rs

1//! Template strings for project scaffolding
2//!
3//! This module contains all the template strings used when generating
4//! new AllFrame projects with the `allframe ignite` command.
5//!
6//! All templates follow Clean Architecture principles and include:
7//! - Example code demonstrating proper layer separation
8//! - Documentation comments explaining each layer's purpose
9//! - Trait-based abstractions for dependency inversion
10
11/// Generate Cargo.toml content for a new AllFrame project
12///
13/// Creates a minimal `Cargo.toml` with:
14/// - Project name and metadata
15/// - Core dependencies: tokio, serde, anyhow, async-trait
16/// - Binary configuration
17///
18/// Note: The `allframe` crate dependency will be added once it's published to
19/// crates.io.
20///
21/// # Arguments
22/// * `project_name` - Name of the project (used for package name and binary
23///   name)
24///
25/// # Returns
26/// A formatted Cargo.toml file content as a String
27pub fn cargo_toml(project_name: &str) -> String {
28    // For now, we generate a minimal Cargo.toml without allframe dependency
29    // since allframe is not published to crates.io yet.
30    // When allframe is published, we'll add the features and allframe dependency.
31    format!(
32        r#"[package]
33name = "{}"
34version = "0.1.0"
35edition = "2021"
36
37[dependencies]
38tokio = {{ version = "1", features = ["full"] }}
39serde = {{ version = "1", features = ["derive"] }}
40anyhow = "1.0"
41async-trait = "0.1"
42
43[[bin]]
44name = "{}"
45path = "src/main.rs"
46"#,
47        project_name, project_name
48    )
49}
50
51/// Generate src/main.rs content with tokio runtime
52///
53/// Creates the application entry point with:
54/// - Module declarations for all Clean Architecture layers
55/// - Async main function with tokio runtime
56/// - Basic test structure
57///
58/// # Returns
59/// A static string containing the main.rs template
60pub fn main_rs() -> &'static str {
61    r#"//! AllFrame Application
62//!
63//! This is an AllFrame application following Clean Architecture principles.
64
65mod domain;
66mod application;
67mod infrastructure;
68mod presentation;
69
70#[tokio::main]
71async fn main() {
72    println!("AllFrame - One frame. Infinite transformations.");
73    println!("Your application is ready!");
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_application_compiles() {
82        // This test verifies the application structure compiles
83        assert!(true);
84    }
85}
86"#
87}
88
89/// Generate domain/mod.rs content
90pub fn domain_mod() -> &'static str {
91    r#"//! Domain Layer
92//!
93//! This layer contains the core business logic and has NO external dependencies.
94//!
95//! Structure:
96//! - entities/: Business entities with behavior
97//! - repositories/: Repository trait definitions (interfaces)
98//! - services/: Domain services for complex business logic
99//! - value_objects/: Immutable value types
100
101pub mod entities;
102pub mod repositories;
103
104// Re-export commonly used types
105pub use entities::*;
106pub use repositories::*;
107"#
108}
109
110/// Generate domain/entities.rs content
111pub fn domain_entities() -> &'static str {
112    r#"//! Domain Entities
113//!
114//! Business entities with behavior live here.
115
116// Example entity (remove and add your own)
117pub struct ExampleEntity {
118    id: String,
119    name: String,
120}
121
122impl ExampleEntity {
123    pub fn new(id: String, name: String) -> Self {
124        Self { id, name }
125    }
126
127    pub fn id(&self) -> &str {
128        &self.id
129    }
130
131    pub fn name(&self) -> &str {
132        &self.name
133    }
134}
135"#
136}
137
138/// Generate domain/repositories.rs content
139pub fn domain_repositories() -> &'static str {
140    r#"//! Repository Traits
141//!
142//! Repository interfaces (traits) are defined here.
143//! Implementations live in the infrastructure layer.
144
145// Example repository trait (remove and add your own)
146#[async_trait::async_trait]
147pub trait ExampleRepository: Send + Sync {
148    async fn find_by_id(&self, id: &str) -> anyhow::Result<Option<String>>;
149    async fn save(&self, id: &str, data: String) -> anyhow::Result<()>;
150}
151"#
152}
153
154/// Generate application/mod.rs content
155pub fn application_mod() -> &'static str {
156    r#"//! Application Layer
157//!
158//! This layer orchestrates domain objects to fulfill use cases.
159//! It depends only on the domain layer.
160
161pub mod services;
162
163pub use services::*;
164"#
165}
166
167/// Generate application/services.rs content
168pub fn application_services() -> &'static str {
169    r#"//! Application Services
170//!
171//! Use case orchestration lives here.
172
173// Example service (remove and add your own)
174pub struct ExampleService;
175
176impl ExampleService {
177    pub fn new() -> Self {
178        Self
179    }
180
181    pub async fn do_something(&self) -> anyhow::Result<String> {
182        Ok("Service executed successfully".to_string())
183    }
184}
185"#
186}
187
188/// Generate infrastructure/mod.rs content
189pub fn infrastructure_mod() -> &'static str {
190    r#"//! Infrastructure Layer
191//!
192//! This layer implements domain repository traits and handles external concerns.
193//! It depends on the domain layer (implements traits defined there).
194
195pub mod repositories;
196
197pub use repositories::*;
198"#
199}
200
201/// Generate infrastructure/repositories.rs content
202pub fn infrastructure_repositories() -> &'static str {
203    r#"//! Repository Implementations
204//!
205//! Concrete implementations of domain repository traits.
206
207// Example repository implementation (remove and add your own)
208pub struct InMemoryExampleRepository;
209
210impl InMemoryExampleRepository {
211    pub fn new() -> Self {
212        Self
213    }
214}
215
216#[async_trait::async_trait]
217impl crate::domain::repositories::ExampleRepository for InMemoryExampleRepository {
218    async fn find_by_id(&self, _id: &str) -> anyhow::Result<Option<String>> {
219        Ok(None)
220    }
221
222    async fn save(&self, _id: &str, _data: String) -> anyhow::Result<()> {
223        Ok(())
224    }
225}
226"#
227}
228
229/// Generate presentation/mod.rs content
230pub fn presentation_mod() -> &'static str {
231    r#"//! Presentation Layer
232//!
233//! This layer handles HTTP/gRPC/GraphQL requests and responses.
234//! It depends on the application and domain layers.
235
236pub mod handlers;
237
238pub use handlers::*;
239"#
240}
241
242/// Generate presentation/handlers.rs content
243pub fn presentation_handlers() -> &'static str {
244    r#"//! HTTP/API Handlers
245//!
246//! Thin handlers that delegate to application services.
247
248// Example handler (remove and add your own)
249pub async fn example_handler() -> String {
250    "Hello from AllFrame!".to_string()
251}
252"#
253}
254
255/// Generate .gitignore content
256pub fn gitignore() -> &'static str {
257    r#"# Rust
258target/
259Cargo.lock
260
261# IDE
262.vscode/
263.idea/
264*.swp
265*.swo
266
267# OS
268.DS_Store
269Thumbs.db
270
271# Environment
272.env
273.env.local
274
275# Logs
276*.log
277"#
278}
279
280/// Generate README.md content for the project
281///
282/// Creates comprehensive project documentation including:
283/// - Quick start guide
284/// - Project structure explanation
285/// - Feature highlights
286///
287/// # Arguments
288/// * `project_name` - Name of the project (used in title)
289///
290/// # Returns
291/// A formatted README.md file content as a String
292pub fn readme(project_name: &str) -> String {
293    format!(
294        r#"# {}
295
296An AllFrame application following Clean Architecture principles.
297
298## Quick Start
299
300```bash
301# Run the application
302cargo run
303
304# Run tests
305cargo test
306
307# Run with all features
308cargo run --all-features
309```
310
311## Structure
312
313This project follows Clean Architecture:
314
315```
316src/
317├── domain/          # Core business logic (no external dependencies)
318├── application/     # Use case orchestration
319├── infrastructure/  # External implementations (database, HTTP clients)
320└── presentation/    # HTTP/gRPC/GraphQL handlers
321```
322
323## Features
324
325- Clean Architecture enforced
326- TDD-first development
327- Protocol-agnostic (REST/GraphQL/gRPC)
328
329## Built with AllFrame
330
331[AllFrame](https://github.com/all-source-os/all-frame) - The composable Rust API framework.
332
333*One frame. Infinite transformations.*
334"#,
335        project_name
336    )
337}