pdmt 1.0.1

High-performance, deterministic templating library for Model Context Protocol (MCP) applications with comprehensive todo validation and quality enforcement
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//! README Builder Example
//!
//! This example demonstrates using PDMT's deterministic templating system
//! to generate consistent, well-structured README.md files for projects.
//! The deterministic nature ensures standardized documentation across projects.
//!
//! Run with: cargo run --example readme_builder --features="full"

use clap::Parser;
use pdmt::template::{definition::TemplateDefinition, engine::TemplateEngine};
use serde::{Deserialize, Serialize};

#[derive(Parser, Debug)]
#[command(name = "readme-builder")]
#[command(about = "Generate deterministic README.md files from structured templates")]
struct Args {
    /// Project name
    #[arg(short = 'n', long)]
    name: Option<String>,

    /// Project description
    #[arg(short = 'd', long)]
    description: Option<String>,

    /// Primary programming language
    #[arg(short = 'l', long)]
    language: Option<String>,

    /// License type (MIT, Apache-2.0, GPL-3.0, etc.)
    #[arg(long, default_value = "MIT")]
    license: String,

    /// GitHub username or organization
    #[arg(short = 'u', long)]
    github_user: Option<String>,

    /// Repository name
    #[arg(short = 'r', long)]
    repo: Option<String>,

    /// Include badges (CI, coverage, version, etc.)
    #[arg(short = 'b', long)]
    badges: bool,

    /// Include installation section
    #[arg(long, default_value = "true")]
    installation: bool,

    /// Include contributing section
    #[arg(long, default_value = "true")]
    contributing: bool,

    /// Interactive mode
    #[arg(short = 'i', long)]
    interactive: bool,

    /// Output file path
    #[arg(short = 'o', long, default_value = "README.md")]
    output: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ReadmeInput {
    project: ProjectInfo,
    badges: Vec<Badge>,
    sections: ReadmeSections,
    features: Vec<Feature>,
    installation: InstallationInfo,
    usage: UsageInfo,
    api: Option<ApiDocumentation>,
    testing: TestingInfo,
    contributing: ContributingInfo,
    license: LicenseInfo,
    acknowledgements: Option<Vec<String>>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ProjectInfo {
    name: String,
    description: String,
    version: String,
    language: String,
    github_user: String,
    repo_name: String,
    documentation_url: Option<String>,
    homepage: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Badge {
    name: String,
    url: String,
    image_url: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ReadmeSections {
    include_toc: bool,
    include_features: bool,
    include_installation: bool,
    include_usage: bool,
    include_api: bool,
    include_testing: bool,
    include_contributing: bool,
    include_license: bool,
    include_acknowledgements: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct Feature {
    emoji: String,
    title: String,
    description: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct InstallationInfo {
    package_manager: String,
    install_command: String,
    requirements: Vec<String>,
    optional_features: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct UsageInfo {
    quick_start: String,
    basic_example: CodeExample,
    advanced_examples: Vec<CodeExample>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct CodeExample {
    title: String,
    language: String,
    code: String,
    description: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ApiDocumentation {
    main_modules: Vec<String>,
    key_functions: Vec<String>,
    documentation_link: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct TestingInfo {
    test_command: String,
    coverage_command: Option<String>,
    lint_command: Option<String>,
    benchmark_command: Option<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct ContributingInfo {
    guidelines_url: Option<String>,
    code_of_conduct_url: Option<String>,
    issue_template: bool,
    pr_template: bool,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
struct LicenseInfo {
    license_type: String,
    copyright_holder: String,
    year: String,
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let args = Args::parse();

    println!("📚 PDMT README Builder");
    println!("Generate deterministic, well-structured README files\n");

    // Create template engine
    let mut engine = TemplateEngine::new();

    // Define README template
    let readme_template = create_readme_template();
    engine.register_template(readme_template)?;

    // Get README input
    let readme_input = if args.interactive {
        get_interactive_input()?
    } else {
        get_input_from_args(&args)
    };

    // Generate README using deterministic template
    println!("🔄 Generating README.md...");
    let start = std::time::Instant::now();

    let input_json = serde_json::to_value(&readme_input)?;
    let result = engine.generate("readme_template", input_json).await?;

    let duration = start.elapsed();
    println!("✅ README generated in {:?}\n", duration);

    // Write to file
    std::fs::write(&args.output, &result.content)?;
    println!("💾 README saved to: {}", args.output);

    // Show preview
    println!("\n📄 Preview (first 20 lines):\n");
    for line in result.content.lines().take(20) {
        println!("{}", line);
    }
    println!("...\n");

    Ok(())
}

fn create_readme_template() -> TemplateDefinition {
    let template_content = r#"# {{project.name}}

{{#if badges}}
{{#each badges}}[![{{name}}]({{image_url}})]({{url}}) {{/each}}
{{/if}}

{{project.description}}

{{#if sections.include_toc}}
## 📑 Table of Contents

- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)
{{#if sections.include_api}}- [API Documentation](#api-documentation){{/if}}
- [Testing](#testing)
- [Contributing](#contributing)
- [License](#license)
{{#if sections.include_acknowledgements}}- [Acknowledgements](#acknowledgements){{/if}}
{{/if}}

{{#if sections.include_features}}
## ✨ Features

{{#each features}}
- **{{emoji}} {{title}}**: {{description}}
{{/each}}
{{/if}}

{{#if sections.include_installation}}
## 📦 Installation

### Requirements

{{#each installation.requirements}}
- {{this}}
{{/each}}

### Using {{installation.package_manager}}

```bash
{{installation.install_command}}
```

{{#if installation.optional_features}}
### Optional Features

{{#each installation.optional_features}}
- `{{this}}`
{{/each}}
{{/if}}
{{/if}}

{{#if sections.include_usage}}
## 🚀 Usage

### Quick Start

{{usage.quick_start}}

### Basic Example

{{#if usage.basic_example.description}}{{usage.basic_example.description}}{{/if}}

```{{usage.basic_example.language}}
{{usage.basic_example.code}}
```

{{#if usage.advanced_examples}}
### Advanced Examples

{{#each usage.advanced_examples}}
#### {{title}}

{{#if description}}{{description}}{{/if}}

```{{language}}
{{code}}
```

{{/each}}
{{/if}}
{{/if}}

{{#if sections.include_api}}
{{#if api}}
## 📖 API Documentation

### Main Modules

{{#each api.main_modules}}
- `{{this}}`
{{/each}}

### Key Functions

{{#each api.key_functions}}
- `{{this}}`
{{/each}}

For complete API documentation, see [{{api.documentation_link}}]({{api.documentation_link}})
{{/if}}
{{/if}}

{{#if sections.include_testing}}
## 🧪 Testing

Run tests with:

```bash
{{testing.test_command}}
```

{{#if testing.coverage_command}}
### Coverage

```bash
{{testing.coverage_command}}
```
{{/if}}

{{#if testing.lint_command}}
### Linting

```bash
{{testing.lint_command}}
```
{{/if}}

{{#if testing.benchmark_command}}
### Benchmarks

```bash
{{testing.benchmark_command}}
```
{{/if}}
{{/if}}

{{#if sections.include_contributing}}
## 🤝 Contributing

We welcome contributions! Please see our [Contributing Guidelines]({{#if contributing.guidelines_url}}{{contributing.guidelines_url}}{{else}}CONTRIBUTING.md{{/if}}) for details.

{{#if contributing.code_of_conduct_url}}
Please note that this project is released with a [Code of Conduct]({{contributing.code_of_conduct_url}}). By participating in this project you agree to abide by its terms.
{{/if}}

### Development Setup

```bash
git clone https://github.com/{{project.github_user}}/{{project.repo_name}}
cd {{project.repo_name}}
{{#if installation.install_command}}{{installation.install_command}}{{/if}}
{{testing.test_command}}
```
{{/if}}

{{#if sections.include_license}}
## 📄 License

This project is licensed under the {{license.license_type}} License - see the [LICENSE](LICENSE) file for details.

Copyright © {{license.year}} {{license.copyright_holder}}
{{/if}}

{{#if sections.include_acknowledgements}}
{{#if acknowledgements}}
## 🙏 Acknowledgements

{{#each acknowledgements}}
- {{this}}
{{/each}}
{{/if}}
{{/if}}

---

<p align="center">
Built with ❤️ using <a href="https://github.com/paiml/pdmt">PDMT</a>
</p>"#;

    TemplateDefinition::new(
        "readme_template",
        "1.0.0",
        template_content,
    )
}

fn get_input_from_args(args: &Args) -> ReadmeInput {
    let project_name = args.name.clone().unwrap_or_else(|| "my-project".to_string());
    let github_user = args.github_user.clone().unwrap_or_else(|| "username".to_string());
    let repo_name = args.repo.clone().unwrap_or_else(|| project_name.clone());
    let language = args.language.clone().unwrap_or_else(|| "rust".to_string());
    
    let badges = if args.badges {
        vec![
            Badge {
                name: "CI".to_string(),
                url: format!("https://github.com/{}/{}/actions", github_user, repo_name),
                image_url: format!("https://github.com/{}/{}/workflows/CI/badge.svg", github_user, repo_name),
            },
            Badge {
                name: "Crates.io".to_string(),
                url: format!("https://crates.io/crates/{}", repo_name),
                image_url: format!("https://img.shields.io/crates/v/{}.svg", repo_name),
            },
            Badge {
                name: "Documentation".to_string(),
                url: format!("https://docs.rs/{}", repo_name),
                image_url: format!("https://docs.rs/{}/badge.svg", repo_name),
            },
            Badge {
                name: format!("License: {}", args.license),
                url: "https://opensource.org/licenses/MIT".to_string(),
                image_url: format!("https://img.shields.io/badge/License-{}-yellow.svg", args.license),
            },
        ]
    } else {
        vec![]
    };

    ReadmeInput {
        project: ProjectInfo {
            name: project_name.clone(),
            description: args.description.clone().unwrap_or_else(|| 
                "A powerful, efficient solution built with modern best practices.".to_string()
            ),
            version: "1.0.0".to_string(),
            language: language.clone(),
            github_user: github_user.clone(),
            repo_name: repo_name.clone(),
            documentation_url: Some(format!("https://docs.rs/{}", repo_name)),
            homepage: Some(format!("https://github.com/{}/{}", github_user, repo_name)),
        },
        badges,
        sections: ReadmeSections {
            include_toc: true,
            include_features: true,
            include_installation: args.installation,
            include_usage: true,
            include_api: language == "rust",
            include_testing: true,
            include_contributing: args.contributing,
            include_license: true,
            include_acknowledgements: false,
        },
        features: vec![
            Feature {
                emoji: "🚀".to_string(),
                title: "High Performance".to_string(),
                description: "Optimized for speed and efficiency".to_string(),
            },
            Feature {
                emoji: "🛡️".to_string(),
                title: "Type Safe".to_string(),
                description: "Comprehensive type checking and validation".to_string(),
            },
            Feature {
                emoji: "📚".to_string(),
                title: "Well Documented".to_string(),
                description: "Extensive documentation and examples".to_string(),
            },
            Feature {
                emoji: "🧪".to_string(),
                title: "Thoroughly Tested".to_string(),
                description: "Comprehensive test coverage and quality assurance".to_string(),
            },
        ],
        installation: InstallationInfo {
            package_manager: if language == "rust" { "Cargo".to_string() } else { "npm".to_string() },
            install_command: if language == "rust" {
                format!("cargo add {}", repo_name)
            } else {
                format!("npm install {}", repo_name)
            },
            requirements: vec![
                if language == "rust" { "Rust 1.70+".to_string() } else { "Node.js 18+".to_string() },
                "Git".to_string(),
            ],
            optional_features: if language == "rust" {
                vec!["full".to_string(), "async".to_string()]
            } else {
                vec![]
            },
        },
        usage: UsageInfo {
            quick_start: format!("Get started with {} in minutes!", project_name),
            basic_example: CodeExample {
                title: "Basic Usage".to_string(),
                language: language.clone(),
                code: if language == "rust" {
                    format!(r#"use {};

fn main() {{
    println!("Hello from {{}}!", "{}");
}}"#, repo_name, repo_name)
                } else {
                    format!(r#"const {} = require('{}');

{}.doSomething();"#, repo_name, repo_name, repo_name)
                },
                description: Some("Here's a simple example to get you started:".to_string()),
            },
            advanced_examples: vec![
                CodeExample {
                    title: "Advanced Configuration".to_string(),
                    language: language.clone(),
                    code: if language == "rust" {
                        r#"use my_project::{Config, Engine};

let config = Config::builder()
    .with_option("value")
    .build()?;

let engine = Engine::new(config);
engine.run().await?;"#.to_string()
                    } else {
                        r#"const { configure } = require('my-project');

const instance = configure({
  option: 'value',
  advanced: true
});

await instance.run();"#.to_string()
                    },
                    description: Some("Configure advanced features:".to_string()),
                },
            ],
        },
        api: if language == "rust" {
            Some(ApiDocumentation {
                main_modules: vec![
                    format!("{}::core", repo_name),
                    format!("{}::utils", repo_name),
                    format!("{}::config", repo_name),
                ],
                key_functions: vec![
                    "new() -> Self".to_string(),
                    "configure(config: Config) -> Result<Self>".to_string(),
                    "run() -> Result<()>".to_string(),
                ],
                documentation_link: format!("https://docs.rs/{}", repo_name),
            })
        } else {
            None
        },
        testing: TestingInfo {
            test_command: if language == "rust" { 
                "cargo test".to_string() 
            } else { 
                "npm test".to_string() 
            },
            coverage_command: Some(if language == "rust" {
                "cargo tarpaulin --out Html".to_string()
            } else {
                "npm run coverage".to_string()
            }),
            lint_command: Some(if language == "rust" {
                "cargo clippy".to_string()
            } else {
                "npm run lint".to_string()
            }),
            benchmark_command: if language == "rust" {
                Some("cargo bench".to_string())
            } else {
                None
            },
        },
        contributing: ContributingInfo {
            guidelines_url: Some("CONTRIBUTING.md".to_string()),
            code_of_conduct_url: Some("CODE_OF_CONDUCT.md".to_string()),
            issue_template: true,
            pr_template: true,
        },
        license: LicenseInfo {
            license_type: args.license.clone(),
            copyright_holder: github_user,
            year: "2025".to_string(),
        },
        acknowledgements: None,
    }
}

fn get_interactive_input() -> Result<ReadmeInput, Box<dyn std::error::Error>> {
    use dialoguer::{Input, Select, Confirm};
    
    println!("🎯 Let's build your README interactively!\n");
    
    let name: String = Input::new()
        .with_prompt("Project name")
        .interact()?;
    
    let description: String = Input::new()
        .with_prompt("Project description")
        .interact()?;
    
    let languages = &["rust", "javascript", "python", "go", "java", "other"];
    let language_idx = Select::new()
        .with_prompt("Primary language")
        .items(languages)
        .default(0)
        .interact()?;
    let language = languages[language_idx].to_string();
    
    let include_badges = Confirm::new()
        .with_prompt("Include badges (CI, version, etc.)?")
        .default(true)
        .interact()?;
    
    let github_user: String = Input::new()
        .with_prompt("GitHub username/organization")
        .interact()?;
    
    let repo_name: String = Input::new()
        .with_prompt("Repository name")
        .default(name.clone())
        .interact()?;
    
    let licenses = &["MIT", "Apache-2.0", "GPL-3.0", "BSD-3-Clause", "ISC"];
    let license_idx = Select::new()
        .with_prompt("License type")
        .items(licenses)
        .default(0)
        .interact()?;
    let license = licenses[license_idx].to_string();
    
    // Build complete input from interactive responses
    let mut args = Args::parse();
    args.name = Some(name);
    args.description = Some(description);
    args.language = Some(language);
    args.badges = include_badges;
    args.github_user = Some(github_user);
    args.repo = Some(repo_name);
    args.license = license;
    
    Ok(get_input_from_args(&args))
}