sara-core 0.5.2

Core library for Sara - Requirements Knowledge Graph CLI
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
//! Frontmatter template generation using Tera.

use std::sync::OnceLock;

use tera::{Context, Tera};

use crate::init::TypeConfig;
use crate::model::{FieldName, ItemType};

/// Embedded templates - compiled into the binary.
const SOLUTION_TEMPLATE: &str = include_str!("../../templates/solution.tera");
const USE_CASE_TEMPLATE: &str = include_str!("../../templates/use_case.tera");
const SCENARIO_TEMPLATE: &str = include_str!("../../templates/scenario.tera");
const SYSTEM_REQUIREMENT_TEMPLATE: &str = include_str!("../../templates/system_requirement.tera");
const HARDWARE_REQUIREMENT_TEMPLATE: &str =
    include_str!("../../templates/hardware_requirement.tera");
const SOFTWARE_REQUIREMENT_TEMPLATE: &str =
    include_str!("../../templates/software_requirement.tera");
const SYSTEM_ARCHITECTURE_TEMPLATE: &str = include_str!("../../templates/system_architecture.tera");
const HARDWARE_DETAILED_DESIGN_TEMPLATE: &str =
    include_str!("../../templates/hardware_detailed_design.tera");
const SOFTWARE_DETAILED_DESIGN_TEMPLATE: &str =
    include_str!("../../templates/software_detailed_design.tera");
const ADR_TEMPLATE: &str = include_str!("../../templates/adr.tera");

/// Global Tera instance, lazily initialized.
static TERA: OnceLock<Tera> = OnceLock::new();

/// Gets or initializes the global Tera instance.
fn get_tera() -> &'static Tera {
    TERA.get_or_init(|| {
        let mut tera = Tera::default();
        tera.add_raw_templates(vec![
            ("solution.tera", SOLUTION_TEMPLATE),
            ("use_case.tera", USE_CASE_TEMPLATE),
            ("scenario.tera", SCENARIO_TEMPLATE),
            ("system_requirement.tera", SYSTEM_REQUIREMENT_TEMPLATE),
            ("hardware_requirement.tera", HARDWARE_REQUIREMENT_TEMPLATE),
            ("software_requirement.tera", SOFTWARE_REQUIREMENT_TEMPLATE),
            ("system_architecture.tera", SYSTEM_ARCHITECTURE_TEMPLATE),
            (
                "hardware_detailed_design.tera",
                HARDWARE_DETAILED_DESIGN_TEMPLATE,
            ),
            (
                "software_detailed_design.tera",
                SOFTWARE_DETAILED_DESIGN_TEMPLATE,
            ),
            ("adr.tera", ADR_TEMPLATE),
        ])
        .expect("Failed to load embedded templates");
        tera
    })
}

/// Options for generating frontmatter.
#[derive(Debug, Clone)]
pub struct GeneratorOptions {
    /// The item ID.
    pub id: String,
    /// The item name.
    pub name: String,
    /// Optional description.
    pub description: Option<String>,
    /// Type-specific attributes.
    pub type_config: TypeConfig,
}

impl GeneratorOptions {
    /// Creates new generator options with defaults for the given item type.
    pub fn new(item_type: ItemType, id: String, name: String) -> Self {
        Self {
            id,
            name,
            description: None,
            type_config: TypeConfig::from_item_type(item_type),
        }
    }

    /// Creates new generator options with specific type configuration.
    pub fn with_type_config(id: String, name: String, type_config: TypeConfig) -> Self {
        Self {
            id,
            name,
            description: None,
            type_config,
        }
    }

    /// Returns the item type for these options.
    pub fn item_type(&self) -> ItemType {
        self.type_config.item_type()
    }

    /// Sets the description.
    pub fn with_description(mut self, description: impl Into<String>) -> Self {
        self.description = Some(description.into());
        self
    }

    /// Sets refines references (for UseCase, Scenario).
    pub fn with_refines(mut self, refs: Vec<String>) -> Self {
        match &mut self.type_config {
            TypeConfig::UseCase { refines } | TypeConfig::Scenario { refines } => {
                *refines = refs;
            }
            _ => {}
        }
        self
    }

    /// Sets derives_from references (for requirement types).
    pub fn with_derives_from(mut self, refs: Vec<String>) -> Self {
        match &mut self.type_config {
            TypeConfig::SystemRequirement { derives_from, .. }
            | TypeConfig::SoftwareRequirement { derives_from, .. }
            | TypeConfig::HardwareRequirement { derives_from, .. } => {
                *derives_from = refs;
            }
            _ => {}
        }
        self
    }

    /// Sets satisfies references (for architecture and design types).
    pub fn with_satisfies(mut self, refs: Vec<String>) -> Self {
        match &mut self.type_config {
            TypeConfig::SystemArchitecture { satisfies, .. }
            | TypeConfig::SoftwareDetailedDesign { satisfies }
            | TypeConfig::HardwareDetailedDesign { satisfies } => {
                *satisfies = refs;
            }
            _ => {}
        }
        self
    }

    /// Sets peer dependencies (for requirement types).
    pub fn with_depends_on(mut self, refs: Vec<String>) -> Self {
        match &mut self.type_config {
            TypeConfig::SystemRequirement { depends_on, .. }
            | TypeConfig::SoftwareRequirement { depends_on, .. }
            | TypeConfig::HardwareRequirement { depends_on, .. } => {
                *depends_on = refs;
            }
            _ => {}
        }
        self
    }

    /// Sets the specification (for requirement types).
    pub fn with_specification(mut self, spec: impl Into<String>) -> Self {
        match &mut self.type_config {
            TypeConfig::SystemRequirement { specification, .. }
            | TypeConfig::SoftwareRequirement { specification, .. }
            | TypeConfig::HardwareRequirement { specification, .. } => {
                *specification = Some(spec.into());
            }
            _ => {}
        }
        self
    }

    /// Sets the target platform (for SystemArchitecture).
    pub fn with_platform(mut self, plat: impl Into<String>) -> Self {
        if let TypeConfig::SystemArchitecture { platform, .. } = &mut self.type_config {
            *platform = Some(plat.into());
        }
        self
    }

    /// Sets the ADR status.
    pub fn with_status(mut self, stat: impl Into<String>) -> Self {
        if let TypeConfig::Adr { status, .. } = &mut self.type_config {
            *status = Some(stat.into());
        }
        self
    }

    /// Sets the ADR deciders.
    pub fn with_deciders(mut self, decs: Vec<String>) -> Self {
        if let TypeConfig::Adr { deciders, .. } = &mut self.type_config {
            *deciders = decs;
        }
        self
    }

    /// Sets the design artifacts this ADR justifies.
    pub fn with_justifies(mut self, just: Vec<String>) -> Self {
        if let TypeConfig::Adr { justifies, .. } = &mut self.type_config {
            *justifies = just;
        }
        self
    }

    /// Sets the older ADRs this decision supersedes.
    pub fn with_supersedes(mut self, sups: Vec<String>) -> Self {
        if let TypeConfig::Adr { supersedes, .. } = &mut self.type_config {
            *supersedes = sups;
        }
        self
    }

    /// Sets the newer ADR that supersedes this one.
    pub fn with_superseded_by(mut self, sup_by: impl Into<String>) -> Self {
        if let TypeConfig::Adr { superseded_by, .. } = &mut self.type_config {
            *superseded_by = Some(sup_by.into());
        }
        self
    }

    /// Builds a Tera context from the options.
    fn to_context(&self) -> Context {
        let mut context = Context::new();
        let item_type = self.item_type();

        context.insert(FieldName::Id.as_str(), &self.id);
        context.insert(FieldName::Type.as_str(), item_type.as_str());
        context.insert(FieldName::Name.as_str(), &escape_yaml_string(&self.name));

        if let Some(ref desc) = self.description {
            context.insert(FieldName::Description.as_str(), &escape_yaml_string(desc));
        }

        // Insert type-specific attributes
        match &self.type_config {
            TypeConfig::Solution => {
                // Solutions don't have type-specific attributes
            }

            TypeConfig::UseCase { refines } | TypeConfig::Scenario { refines } => {
                if !refines.is_empty() {
                    context.insert(FieldName::Refines.as_str(), refines);
                }
            }

            TypeConfig::SystemRequirement {
                specification,
                derives_from,
                depends_on,
            }
            | TypeConfig::SoftwareRequirement {
                specification,
                derives_from,
                depends_on,
            }
            | TypeConfig::HardwareRequirement {
                specification,
                derives_from,
                depends_on,
            } => {
                if !derives_from.is_empty() {
                    context.insert(FieldName::DerivesFrom.as_str(), derives_from);
                }
                if !depends_on.is_empty() {
                    context.insert(FieldName::DependsOn.as_str(), depends_on);
                }
                let spec = specification
                    .as_deref()
                    .unwrap_or("The system SHALL <describe the requirement>.");
                context.insert(FieldName::Specification.as_str(), &escape_yaml_string(spec));
            }

            TypeConfig::SystemArchitecture {
                platform,
                satisfies,
            } => {
                if !satisfies.is_empty() {
                    context.insert(FieldName::Satisfies.as_str(), satisfies);
                }
                if let Some(plat) = platform {
                    context.insert(FieldName::Platform.as_str(), &escape_yaml_string(plat));
                }
            }

            TypeConfig::SoftwareDetailedDesign { satisfies }
            | TypeConfig::HardwareDetailedDesign { satisfies } => {
                if !satisfies.is_empty() {
                    context.insert(FieldName::Satisfies.as_str(), satisfies);
                }
            }

            TypeConfig::Adr {
                status,
                deciders,
                justifies,
                supersedes,
                superseded_by,
            } => {
                if let Some(stat) = status {
                    context.insert(FieldName::Status.as_str(), stat);
                }
                if !deciders.is_empty() {
                    context.insert(FieldName::Deciders.as_str(), deciders);
                }
                if !justifies.is_empty() {
                    context.insert(FieldName::Justifies.as_str(), justifies);
                }
                if !supersedes.is_empty() {
                    context.insert(FieldName::Supersedes.as_str(), supersedes);
                }
                if let Some(sup_by) = superseded_by {
                    context.insert(FieldName::SupersededBy.as_str(), sup_by);
                }
            }
        }

        context
    }

    /// Returns the template name for the item type.
    fn template_name(&self) -> &'static str {
        match self.item_type() {
            ItemType::Solution => "solution.tera",
            ItemType::UseCase => "use_case.tera",
            ItemType::Scenario => "scenario.tera",
            ItemType::SystemRequirement => "system_requirement.tera",
            ItemType::HardwareRequirement => "hardware_requirement.tera",
            ItemType::SoftwareRequirement => "software_requirement.tera",
            ItemType::SystemArchitecture => "system_architecture.tera",
            ItemType::HardwareDetailedDesign => "hardware_detailed_design.tera",
            ItemType::SoftwareDetailedDesign => "software_detailed_design.tera",
            ItemType::ArchitectureDecisionRecord => "adr.tera",
        }
    }
}

/// Generates a complete document with frontmatter and body.
pub fn generate_document(opts: &GeneratorOptions) -> String {
    let tera = get_tera();
    let context = opts.to_context();
    tera.render(opts.template_name(), &context)
        .expect("Failed to render document template")
}

/// Escapes a string for YAML.
fn escape_yaml_string(s: &str) -> String {
    s.replace('\\', "\\\\")
        .replace('"', "\\\"")
        .replace('\n', "\\n")
}

/// Generates a new ID for the given type, optionally using a sequence number.
pub fn generate_id(item_type: ItemType, sequence: Option<u32>) -> String {
    let prefix = item_type.prefix();
    let num = sequence.unwrap_or(1);
    format!("{}-{:03}", prefix, num)
}

/// Suggests the next ID based on existing items in the graph (FR-044).
///
/// Finds the highest existing ID for the given type and returns the next sequential ID.
/// If no graph is provided or no items exist, returns the first ID (e.g., "SOL-001").
pub fn suggest_next_id(
    item_type: ItemType,
    graph: Option<&crate::graph::KnowledgeGraph>,
) -> String {
    let Some(graph) = graph else {
        return generate_id(item_type, None);
    };

    let prefix = item_type.prefix();
    let max_num = graph
        .items()
        .filter(|item| item.item_type == item_type)
        .filter_map(|item| {
            item.id
                .as_str()
                .strip_prefix(prefix)
                .and_then(|suffix| suffix.trim_start_matches('-').parse::<u32>().ok())
        })
        .max()
        .unwrap_or(0);

    format!("{}-{:03}", prefix, max_num + 1)
}

/// Extracts a name from a markdown file's first heading.
pub fn extract_name_from_content(content: &str) -> Option<String> {
    for line in content.lines() {
        let trimmed = line.trim();
        if let Some(heading) = trimmed.strip_prefix("# ") {
            return Some(heading.trim().to_string());
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_generate_document_solution() {
        let opts = GeneratorOptions::new(
            ItemType::Solution,
            "SOL-001".to_string(),
            "Test Solution".to_string(),
        );
        let doc = generate_document(&opts);

        assert!(doc.contains("# Solution: Test Solution"));
        assert!(doc.contains("## Overview"));
        assert!(doc.contains("## Goals & KPIs"));
    }

    #[test]
    fn test_generate_document_use_case() {
        let opts = GeneratorOptions::new(
            ItemType::UseCase,
            "UC-001".to_string(),
            "Test Use Case".to_string(),
        )
        .with_refines(vec!["SOL-001".to_string()]);

        let doc = generate_document(&opts);

        assert!(doc.contains("# Use Case: Test Use Case"));
        assert!(doc.contains("## Actor(s)"));
        assert!(doc.contains("refines:"));
        assert!(doc.contains("SOL-001"));
    }

    #[test]
    fn test_generate_id() {
        assert_eq!(generate_id(ItemType::Solution, Some(1)), "SOL-001");
        assert_eq!(generate_id(ItemType::UseCase, Some(42)), "UC-042");
        assert_eq!(generate_id(ItemType::SystemRequirement, None), "SYSREQ-001");
    }

    #[test]
    fn test_extract_name_from_content() {
        let content = "# My Document\n\nSome content here.";
        assert_eq!(
            extract_name_from_content(content),
            Some("My Document".to_string())
        );
    }

    #[test]
    fn test_generate_frontmatter_system_architecture_with_platform() {
        let opts = GeneratorOptions::new(
            ItemType::SystemArchitecture,
            "SYSARCH-001".to_string(),
            "Web Platform Architecture".to_string(),
        )
        .with_platform("AWS Lambda")
        .with_satisfies(vec!["SYSREQ-001".to_string()]);

        let doc = generate_document(&opts);

        assert!(doc.contains("id: \"SYSARCH-001\""));
        assert!(doc.contains("type: system_architecture"));
        assert!(doc.contains("platform: \"AWS Lambda\""));
        assert!(doc.contains("satisfies:"));
        assert!(doc.contains("SYSREQ-001"));
    }

    #[test]
    fn test_generate_document_adr() {
        let opts = GeneratorOptions::new(
            ItemType::ArchitectureDecisionRecord,
            "ADR-001".to_string(),
            "Use Microservices Architecture".to_string(),
        )
        .with_status("proposed")
        .with_deciders(vec!["Alice Smith".to_string(), "Bob Jones".to_string()])
        .with_justifies(vec!["SYSARCH-001".to_string()])
        .with_description("Decision to adopt microservices".to_string());

        let doc = generate_document(&opts);

        // Check frontmatter
        assert!(doc.contains("id: \"ADR-001\""));
        assert!(doc.contains("type: architecture_decision_record"));
        assert!(doc.contains("status: proposed"));
        assert!(doc.contains("deciders:"));
        assert!(doc.contains("Alice Smith"));
        assert!(doc.contains("Bob Jones"));
        assert!(doc.contains("justifies:"));
        assert!(doc.contains("SYSARCH-001"));

        // Check body structure
        assert!(doc.contains("# Architecture Decision: Use Microservices Architecture"));
        assert!(doc.contains("## Context and problem statement"));
        assert!(doc.contains("## Considered options"));
        assert!(doc.contains("## Decision Outcome"));
    }

    #[test]
    fn test_generate_id_adr() {
        assert_eq!(
            generate_id(ItemType::ArchitectureDecisionRecord, Some(1)),
            "ADR-001"
        );
        assert_eq!(
            generate_id(ItemType::ArchitectureDecisionRecord, Some(42)),
            "ADR-042"
        );
    }
}