workspace_tools 0.12.0

Reliable workspace-relative path resolution for Rust projects. Automatically finds your workspace root and provides consistent file path handling regardless of execution context. Features memory-safe secret management, configuration loading with validation, and resource discovery.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# Task 002: Template System

**Priority**: šŸ—ļø High Impact  
**Phase**: 1 (Immediate)  
**Estimated Effort**: 4-5 days  
**Dependencies**: Task 001 (Cargo Integration) recommended  

## **Objective**
Implement a workspace scaffolding system that creates standard project structures, reducing time-to-productivity for new projects and establishing workspace_tools as a project creation tool.

## **Technical Requirements**

### **Core Features**
1. **Built-in Templates**
   - CLI application template
   - Web service template  
   - Library template
   - Desktop application template

2. **Template Engine**
   - Variable substitution (project name, author, etc.)
   - Conditional file generation
   - Directory structure creation
   - File content templating

3. **Extensibility**
   - Custom template support
   - Template validation
   - Template metadata

### **New API Surface**
```rust
impl Workspace 
{
    /// Create workspace structure from built-in template
    pub fn scaffold_from_template(&self, template: TemplateType) -> Result<()>;
    
    /// Create workspace structure from custom template
    pub fn scaffold_from_path<P: AsRef<Path>>(&self, template_path: P) -> Result<()>;
    
    /// List available built-in templates
    pub fn available_templates() -> Vec<TemplateInfo>;
    
    /// Validate template before scaffolding
    pub fn validate_template<P: AsRef<Path>>(&self, template_path: P) -> Result<TemplateValidation>;
}

#[derive(Debug, Clone)]
pub enum TemplateType 
{
    Cli,
    WebService,
    Library, 
    Desktop,
}

#[derive(Debug, Clone)]
pub struct TemplateInfo 
{
    pub name: String,
    pub description: String,
    pub files_created: usize,
    pub directories_created: usize,
}

#[derive(Debug, Clone)]
pub struct TemplateValidation 
{
    pub valid: bool,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct TemplateContext 
{
    pub project_name: String,
    pub author_name: String,
    pub author_email: String,
    pub license: String,
    pub variables: HashMap<String, String>,
}
```

### **Implementation Steps**

#### **Step 1: Template Engine Foundation** (Day 1)
```rust
// Add to Cargo.toml dependencies
[features]
default = ["enabled", "templates"]
templates = ["dep:handlebars", "dep:serde_json"]

[dependencies]
handlebars = { version = "4.0", optional = true }
serde_json = { version = "1.0", optional = true }

// Template engine implementation
#[cfg(feature = "templates")]
mod templating {
    use handlebars::Handlebars;
    use serde_json::{json, Value};
    use std::collections::HashMap;
    
    pub struct TemplateEngine 
{
        handlebars: Handlebars<'static>,
    }
    
    impl TemplateEngine 
{
        pub fn new() -> Self 
{
            let mut handlebars = Handlebars::new();
            handlebars.set_strict_mode(true);
            Self { handlebars }
        }
        
        pub fn render_string(&self, template: &str, context: &TemplateContext) -> Result<String> 
{
            let json_context = json!({
                "project_name": context.project_name,
                "author_name": context.author_name,
                "author_email": context.author_email,
                "license": context.license,
                "variables": context.variables,
            });
            
            self.handlebars.render_template(template, &json_context)
                .map_err(|e| WorkspaceError::ConfigurationError(e.to_string()))
        }
        
        pub fn render_file<P: AsRef<Path>>(
            &self, 
            template_path: P, 
            context: &TemplateContext
        ) -> Result<String> {
            let template_content = std::fs::read_to_string(template_path)
                .map_err(|e| WorkspaceError::IoError(e.to_string()))?;
            self.render_string(&template_content, context)
        }
    }
}
```

#### **Step 2: Built-in Templates** (Day 2)
```rust
// Embedded templates using include_str!
const CLI_TEMPLATE: &[(&str, &str)] = &[
    ("Cargo.toml", include_str!("../templates/cli/Cargo.toml.hbs")),
    ("src/main.rs", include_str!("../templates/cli/src/main.rs.hbs")),
    ("src/cli.rs", include_str!("../templates/cli/src/cli.rs.hbs")),
    ("config/app.toml", include_str!("../templates/cli/config/app.toml.hbs")),
    ("README.md", include_str!("../templates/cli/README.md.hbs")),
    (".gitignore", include_str!("../templates/cli/.gitignore")),
];

const WEB_SERVICE_TEMPLATE: &[(&str, &str)] = &[
    ("Cargo.toml", include_str!("../templates/web/Cargo.toml.hbs")),
    ("src/main.rs", include_str!("../templates/web/src/main.rs.hbs")),
    ("src/handlers.rs", include_str!("../templates/web/src/handlers.rs.hbs")),
    ("src/config.rs", include_str!("../templates/web/src/config.rs.hbs")),
    ("config/development.toml", include_str!("../templates/web/config/development.toml.hbs")),
    ("config/production.toml", include_str!("../templates/web/config/production.toml.hbs")),
    ("static/css/main.css", include_str!("../templates/web/static/css/main.css")),
    ("templates/base.html", include_str!("../templates/web/templates/base.html.hbs")),
    ("docker-compose.yml", include_str!("../templates/web/docker-compose.yml.hbs")),
    ("Dockerfile", include_str!("../templates/web/Dockerfile.hbs")),
];

impl TemplateType 
{
    fn template_files(&self) -> &'static [(&'static str, &'static str)] 
{
        match self {
            TemplateType::Cli => CLI_TEMPLATE,
            TemplateType::WebService => WEB_SERVICE_TEMPLATE,
            TemplateType::Library => LIBRARY_TEMPLATE,
            TemplateType::Desktop => DESKTOP_TEMPLATE,
        }
    }
    
    fn directories(&self) -> &'static [&'static str] 
{
        match self {
            TemplateType::Cli => &["src", "config", "data", "logs", "tests"],
            TemplateType::WebService => &[
                "src", "config", "data", "logs", "static/css", "static/js", 
                "templates", "uploads", "tests"
            ],
            TemplateType::Library => &["src", "examples", "tests", "benches"],
            TemplateType::Desktop => &[
                "src", "assets", "resources", "config", "data", "plugins"
            ],
        }
    }
}
```

#### **Step 3: Scaffolding Implementation** (Day 3)
```rust
#[cfg(feature = "templates")]
impl Workspace 
{
    pub fn scaffold_from_template(&self, template: TemplateType) -> Result<()> 
{
        // Create default context
        let context = self.create_default_context()?;
        self.scaffold_with_context(template, &context)
    }
    
    pub fn scaffold_with_context(
        &self, 
        template: TemplateType, 
        context: &TemplateContext
    ) -> Result<()> {
        let engine = TemplateEngine::new();
        
        // Create directories
        for dir in template.directories() {
            let dir_path = self.join(dir);
            std::fs::create_dir_all(&dir_path)
                .map_err(|e| WorkspaceError::IoError(e.to_string()))?;
        }
        
        // Create files from templates
        for (file_path, template_content) in template.template_files() {
            let rendered_content = engine.render_string(template_content, context)?;
            let full_path = self.join(file_path);
            
            // Ensure parent directory exists
            if let Some(parent) = full_path.parent() {
                std::fs::create_dir_all(parent)
                    .map_err(|e| WorkspaceError::IoError(e.to_string()))?;
            }
            
            std::fs::write(&full_path, rendered_content)
                .map_err(|e| WorkspaceError::IoError(e.to_string()))?;
        }
        
        Ok(())
    }
    
    fn create_default_context(&self) -> Result<TemplateContext> 
{
        Ok(TemplateContext {
            project_name: self.root()
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("my_project")
                .to_string(),
            author_name: std::env::var("USER")
                .or_else(|_| std::env::var("USERNAME"))
                .unwrap_or_else(|_| "Author".to_string()),
            author_email: format!("{}@example.com", 
                std::env::var("USER").unwrap_or_else(|_| "author".to_string())
            ),
            license: "MIT".to_string(),
            variables: HashMap::new(),
        })
    }
}
```

#### **Step 4: Template Files Creation** (Day 4)
Create actual template files in `templates/` directory:

**templates/cli/Cargo.toml.hbs**:
```toml
[package]
name = "{{project_name}}"
version = "0.1.0"
edition = "2021"
authors = ["{{author_name}} <{{author_email}}>"]
license = "{{license}}"
description = "A CLI application built with workspace_tools"

[dependencies]
workspace_tools = "0.2"
clap = { version = "4.0", features = ["derive"] }
anyhow = "1.0"
```

**templates/cli/src/main.rs.hbs**:
```rust
//! {{project_name}} - CLI application

use workspace_tools::workspace;
use clap::{Parser, Subcommand};
use anyhow::Result;

#[derive(Parser)]
#[command(name = "{{project_name}}")]
#[command(about = "A CLI application with workspace_tools")]
struct Cli 
{
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands 
{
    /// Initialize the application
    Init,
    /// Show configuration information
    Info,
}

fn main() -> Result<()> 
{
    let cli = Cli::parse();
    let ws = workspace()?;
    
    match cli.command {
        Commands::Init => {
            println!("Initializing {{project_name}}...");
            // Create necessary directories
            std::fs::create_dir_all(ws.config_dir())?;
            std::fs::create_dir_all(ws.data_dir())?;
            std::fs::create_dir_all(ws.logs_dir())?;
            println!("āœ… Initialization complete!");
        }
        Commands::Info => {
            println!("{{project_name}} Information:");
            println!("Workspace root: {}", ws.root().display());
            println!("Config dir: {}", ws.config_dir().display());
            println!("Data dir: {}", ws.data_dir().display());
        }
    }
    
    Ok(())
}
```

**templates/web/src/main.rs.hbs**:
```rust
//! {{project_name}} - Web service

use workspace_tools::workspace;
use std::net::SocketAddr;

mod handlers;
mod config;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> 
{
    let ws = workspace()?;
    let config = config::load_config(&ws).await?;
    
    println!("šŸš€ Starting {{project_name}}");
    println!("Workspace: {}", ws.root().display());
    
    let addr = SocketAddr::from(([127, 0, 0, 1], config.port));
    println!("🌐 Server running on http://{}", addr);
    
    // Your web framework setup here
    // axum::Server::bind(&addr)...
    
    Ok(())
}
```

#### **Step 5: Testing & Documentation** (Day 5)
```rust
#[cfg(test)]
#[cfg(feature = "templates")]
mod template_tests {
    use super::*;
    use crate::testing::create_test_workspace;
    
    #[test]
    fn test_cli_template_scaffolding() 
{
        let (_temp_dir, ws) = create_test_workspace();
        
        ws.scaffold_from_template(TemplateType::Cli).unwrap();
        
        // Verify files were created
        assert!(ws.join("Cargo.toml").exists());
        assert!(ws.join("src/main.rs").exists());
        assert!(ws.join("src/cli.rs").exists());
        assert!(ws.config_dir().join("app.toml").exists());
        
        // Verify content was templated
        let cargo_toml = std::fs::read_to_string(ws.join("Cargo.toml")).unwrap();
        assert!(cargo_toml.contains("workspace_tools"));
        assert!(!cargo_toml.contains("{{project_name}}"));
    }
    
    #[test]
    fn test_web_service_template_scaffolding() 
{
        let (_temp_dir, ws) = create_test_workspace();
        
        ws.scaffold_from_template(TemplateType::WebService).unwrap();
        
        // Verify web-specific structure
        assert!(ws.join("static/css").exists());
        assert!(ws.join("templates").exists());
        assert!(ws.join("docker-compose.yml").exists());
    }
    
    #[test]
    fn test_custom_template_context() 
{
        let (_temp_dir, ws) = create_test_workspace();
        
        let mut context = TemplateContext {
            project_name: "my_awesome_cli".to_string(),
            author_name: "Test Author".to_string(),
            author_email: "test@example.com".to_string(),
            license: "Apache-2.0".to_string(),
            variables: HashMap::new(),
        };
        
        ws.scaffold_with_context(TemplateType::Cli, &context).unwrap();
        
        let cargo_toml = std::fs::read_to_string(ws.join("Cargo.toml")).unwrap();
        assert!(cargo_toml.contains("my_awesome_cli"));
        assert!(cargo_toml.contains("Test Author"));
        assert!(cargo_toml.contains("Apache-2.0"));
    }
}
```

### **CLI Integration**
```rust
// Future: CLI command for scaffolding
// cargo workspace-tools init --template=web-service
// cargo workspace-tools scaffold --template=cli MyApp
```

### **Documentation Updates**

#### **README.md Addition**
```markdown
## šŸ—ļø project scaffolding

workspace_tools includes project templates for common Rust project types:

```rust
use workspace_tools::{workspace, TemplateType};

let ws = workspace()?;

// Create a CLI application structure
ws.scaffold_from_template(TemplateType::Cli)?;

// Create a web service structure  
ws.scaffold_from_template(TemplateType::WebService)?;
```

### Available templates:
- **CLI**: Command-line applications with argument parsing
- **Web Service**: Web applications with static assets and templates  
- **Library**: Rust libraries with examples and benchmarks
- **Desktop**: GUI applications with assets and resources
```

#### **New Example: templates.rs**
```rust
//! Project scaffolding example

use workspace_tools::{workspace, TemplateType, TemplateContext};
use std::collections::HashMap;

fn main() -> Result<(), Box<dyn std::error::Error>> 
{
    let ws = workspace()?;
    
    println!("šŸ—ļø  Project Scaffolding Demo");
    println!("Available templates:");
    
    for template in Workspace::available_templates() {
        println!("  šŸ“‹ {}: {}", template.name, template.description);
        println!("     Creates {} files, {} directories", 
                 template.files_created, template.directories_created);
    }
    
    // Scaffold with custom context
    let mut custom_vars = HashMap::new();
    custom_vars.insert("database".to_string(), "postgresql".to_string());
    
    let context = TemplateContext {
        project_name: "my_web_app".to_string(),
        author_name: "Developer".to_string(),
        author_email: "dev@example.com".to_string(),  
        license: "MIT".to_string(),
        variables: custom_vars,
    };
    
    println!("\nšŸ”Ø Scaffolding web service template...");
    ws.scaffold_with_context(TemplateType::WebService, &context)?;
    println!("āœ… Project structure created!");
    
    Ok(())
}
```

### **Success Criteria**
- [ ] Four built-in templates (CLI, Web, Library, Desktop)
- [ ] Template engine with variable substitution
- [ ] Custom context support for personalization
- [ ] Comprehensive test coverage for all templates
- [ ] Generated projects compile and run successfully  
- [ ] Documentation with examples
- [ ] Performance: Scaffolding completes in <1 second

### **Future Enhancements**
- External template repository support
- Interactive template selection
- Template validation and linting
- Integration with cargo-generate
- Custom template creation tools

### **Breaking Changes**
None - this is purely additive functionality with a feature flag.

This task establishes workspace_tools as not just a path resolution library, but a comprehensive project creation and management tool.