ito-core 0.1.27

Core functionality and business logic for Ito
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
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
//! Convert Ito markdown artifacts into JSON-friendly structures.
//!
//! This module is used by "show"-style commands and APIs. It reads spec and
//! change markdown files from disk and produces lightweight structs that can be
//! serialized to JSON.

use std::path::Path;

use crate::error_bridge::IntoCoreResult;
use crate::errors::{CoreError, CoreResult};
use crate::spec_repository::FsSpecRepository;
use ito_domain::modules::ModuleRepository;
use ito_domain::specs::SpecRepository;
use serde::Serialize;

use ito_domain::changes::ChangeRepository;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// One raw scenario block from a spec or delta.
pub struct Scenario {
    #[serde(rename = "rawText")]
    /// The original scenario text (preserves newlines).
    pub raw_text: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// A single requirement statement and its scenarios.
pub struct Requirement {
    /// The normalized requirement statement.
    pub text: String,

    /// Optional stable identifier for traceability (e.g. `REQ-001`).
    #[serde(rename = "requirementId", skip_serializing_if = "Option::is_none")]
    pub requirement_id: Option<String>,

    /// Scenario blocks associated with the requirement.
    pub scenarios: Vec<Scenario>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// JSON-serializable view of a spec markdown file.
pub struct SpecShowJson {
    /// Spec id (folder name under `.ito/specs/`).
    pub id: String,
    /// Human-readable title (currently same as `id`).
    pub title: String,
    /// Extracted `## Purpose` section.
    pub overview: String,
    #[serde(rename = "requirementCount")]
    /// Total number of requirements.
    pub requirement_count: u32,

    /// Requirements parsed from the markdown.
    pub requirements: Vec<Requirement>,

    /// Metadata describing the output format.
    pub metadata: SpecMetadata,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// Additional info included in serialized spec output.
pub struct SpecMetadata {
    /// Output schema version.
    pub version: String,

    /// Output format identifier.
    pub format: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// JSON-serializable view of all main specs bundled together.
pub struct SpecsBundleJson {
    #[serde(rename = "specCount")]
    /// Total number of bundled specs.
    pub spec_count: u32,

    /// Bundled specs, ordered by ascending spec id.
    pub specs: Vec<BundledSpec>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// One bundled spec entry (id + source path + raw markdown).
pub struct BundledSpec {
    /// Spec id (folder name under `.ito/specs/`).
    pub id: String,

    /// Absolute path to the source `.ito/specs/<id>/spec.md` file.
    pub path: String,

    /// Raw markdown contents of the spec.
    pub markdown: String,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// JSON-serializable view of a change (proposal + deltas).
pub struct ChangeShowJson {
    /// Change id (folder name under `.ito/changes/`).
    pub id: String,
    /// Human-readable title (currently same as `id`).
    pub title: String,
    #[serde(rename = "deltaCount")]
    /// Total number of deltas.
    pub delta_count: u32,

    /// Parsed deltas from delta spec files.
    pub deltas: Vec<ChangeDelta>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
/// One delta entry extracted from a change delta spec.
pub struct ChangeDelta {
    /// Spec id the delta belongs to.
    pub spec: String,

    /// Delta operation (e.g. `ADDED`, `MODIFIED`).
    pub operation: String,

    /// Human-readable description for display.
    pub description: String,

    /// Primary requirement extracted for the delta (legacy shape).
    pub requirement: Requirement,

    /// All requirements extracted for the delta.
    pub requirements: Vec<Requirement>,
}

/// Read the markdown for a spec id from `.ito/specs/<id>/spec.md`.
pub fn read_spec_markdown(ito_path: &Path, id: &str) -> CoreResult<String> {
    let repo = FsSpecRepository::new(ito_path);
    read_spec_markdown_from_repository(&repo, id)
}

/// Read the markdown for a spec id from a repository.
pub fn read_spec_markdown_from_repository(
    repo: &(impl SpecRepository + ?Sized),
    id: &str,
) -> CoreResult<String> {
    let spec = repo.get(id).into_core()?;
    Ok(spec.markdown)
}

/// Read the proposal markdown for a change id.
pub fn read_change_proposal_markdown(
    repo: &(impl ChangeRepository + ?Sized),
    change_id: &str,
) -> CoreResult<Option<String>> {
    let change = repo.get(change_id).into_core()?;
    Ok(change.proposal)
}

/// Read the raw markdown for a module's `module.md` file.
pub fn read_module_markdown(
    module_repo: &(impl ModuleRepository + ?Sized),
    module_id: &str,
) -> CoreResult<String> {
    let module = module_repo.get(module_id).into_core()?;
    let module_md_path = module.path.join("module.md");
    if module_md_path.is_file() {
        let md = ito_common::io::read_to_string_or_default(&module_md_path);
        return Ok(md);
    }

    if module.path.as_os_str().is_empty() {
        return Ok(render_module_markdown_fallback(&module));
    }

    Ok(String::new())
}

fn render_module_markdown_fallback(module: &ito_domain::modules::Module) -> String {
    let mut out = String::new();
    out.push_str(&format!("# {}\n", module.name));
    if let Some(description) = module.description.as_deref()
        && !description.trim().is_empty()
    {
        out.push_str("\n## Purpose\n");
        out.push_str(description.trim());
        out.push('\n');
    }
    out
}

/// Parse spec markdown into a serializable structure.
pub fn parse_spec_show_json(id: &str, markdown: &str) -> SpecShowJson {
    let overview = extract_section_text(markdown, "Purpose");
    let requirements = parse_spec_requirements(markdown);
    SpecShowJson {
        id: id.to_string(),
        title: id.to_string(),
        overview,
        requirement_count: requirements.len() as u32,
        requirements,
        metadata: SpecMetadata {
            version: "1.0.0".to_string(),
            format: "ito".to_string(),
        },
    }
}

/// Bundle all main specs under `.ito/specs/*/spec.md` into a JSON-friendly structure.
pub fn bundle_main_specs_show_json(ito_path: &Path) -> CoreResult<SpecsBundleJson> {
    use ito_common::fs::StdFs;

    let fs = StdFs;
    let mut ids = ito_domain::discovery::list_spec_dir_names(&fs, ito_path).into_core()?;
    ids.sort();

    if ids.is_empty() {
        return Err(CoreError::not_found(
            "No specs found under .ito/specs (expected .ito/specs/<id>/spec.md)".to_string(),
        ));
    }

    let mut specs = Vec::with_capacity(ids.len());
    for id in ids {
        let path = ito_common::paths::spec_markdown_path(ito_path, &id);
        let markdown = ito_common::io::read_to_string(&path)
            .map_err(|e| CoreError::io(format!("reading spec {}", id), std::io::Error::other(e)))?;
        specs.push(BundledSpec {
            id,
            path: path.to_string_lossy().to_string(),
            markdown,
        });
    }

    Ok(SpecsBundleJson {
        spec_count: specs.len() as u32,
        specs,
    })
}

/// Bundle all promoted specs from a repository into a JSON-friendly structure.
pub fn bundle_specs_show_json_from_repository(
    repo: &(impl SpecRepository + ?Sized),
) -> CoreResult<SpecsBundleJson> {
    let mut summaries = repo.list().into_core()?;
    summaries.sort_by(|left, right| left.id.cmp(&right.id));
    if summaries.is_empty() {
        return Err(CoreError::not_found(
            "No specs found under .ito/specs (expected .ito/specs/<id>/spec.md)".to_string(),
        ));
    }

    let mut specs = Vec::with_capacity(summaries.len());
    for summary in summaries {
        let spec = repo.get(&summary.id).into_core()?;
        specs.push(BundledSpec {
            id: spec.id,
            path: spec.path.to_string_lossy().to_string(),
            markdown: spec.markdown,
        });
    }

    Ok(SpecsBundleJson {
        spec_count: specs.len() as u32,
        specs,
    })
}

/// Bundle all main specs under `.ito/specs/*/spec.md` into a single markdown stream.
///
/// Each spec is preceded by a metadata comment line:
/// `<!-- spec-id: <id>; source: <absolute-path-to-spec.md> -->`.
pub fn bundle_main_specs_markdown(ito_path: &Path) -> CoreResult<String> {
    let repo = FsSpecRepository::new(ito_path);
    bundle_specs_markdown_from_repository(&repo)
}

/// Bundle all promoted specs from a repository into a single markdown stream.
pub fn bundle_specs_markdown_from_repository(
    repo: &(impl SpecRepository + ?Sized),
) -> CoreResult<String> {
    let bundle = bundle_specs_show_json_from_repository(repo)?;
    let mut out = String::new();
    for (i, spec) in bundle.specs.iter().enumerate() {
        if i != 0 {
            out.push_str("\n\n");
        }
        out.push_str(&format!(
            "<!-- spec-id: {}; source: {} -->\n",
            spec.id, spec.path
        ));
        out.push_str(&spec.markdown);
    }
    Ok(out)
}

/// Return all delta spec files for a change from the repository.
pub fn read_change_delta_spec_files(
    repo: &(impl ChangeRepository + ?Sized),
    change_id: &str,
) -> CoreResult<Vec<DeltaSpecFile>> {
    let change = repo.get(change_id).into_core()?;
    let mut out: Vec<DeltaSpecFile> = change
        .specs
        .into_iter()
        .map(|spec| DeltaSpecFile {
            spec: spec.name,
            markdown: spec.content,
        })
        .collect();
    out.sort_by(|a, b| a.spec.cmp(&b.spec));
    Ok(out)
}

/// Parse a change id plus its delta spec files into a JSON-friendly structure.
pub fn parse_change_show_json(change_id: &str, delta_specs: &[DeltaSpecFile]) -> ChangeShowJson {
    let mut deltas: Vec<ChangeDelta> = Vec::new();
    for file in delta_specs {
        deltas.extend(parse_delta_spec_file(file));
    }

    ChangeShowJson {
        id: change_id.to_string(),
        title: change_id.to_string(),
        delta_count: deltas.len() as u32,
        deltas,
    }
}

#[derive(Debug, Clone)]
/// One loaded delta spec file.
pub struct DeltaSpecFile {
    /// Spec id this delta spec belongs to.
    pub spec: String,

    /// Full markdown contents of the delta `spec.md`.
    pub markdown: String,
}

/// Load a delta `spec.md` and infer the spec id from its parent directory.
pub fn load_delta_spec_file(path: &Path) -> CoreResult<DeltaSpecFile> {
    let markdown = ito_common::io::read_to_string(path).map_err(|e| {
        CoreError::io(
            format!("reading delta spec {}", path.display()),
            std::io::Error::other(e),
        )
    })?;
    let spec = path
        .parent()
        .and_then(|p| p.file_name())
        .map(|s| s.to_string_lossy().to_string())
        .unwrap_or_else(|| "unknown".to_string());
    Ok(DeltaSpecFile { spec, markdown })
}

fn parse_delta_spec_file(file: &DeltaSpecFile) -> Vec<ChangeDelta> {
    let mut out: Vec<ChangeDelta> = Vec::new();

    let mut current_op: Option<String> = None;
    let mut i = 0usize;
    let normalized = file.markdown.replace('\r', "");
    let lines: Vec<&str> = normalized.split('\n').collect();
    while i < lines.len() {
        let line = lines[i].trim_end();
        if let Some(op) = parse_delta_op_header(line) {
            current_op = Some(op);
            i += 1;
            continue;
        }

        if let Some(title) = line.strip_prefix("### Requirement:") {
            let op = current_op.clone().unwrap_or_else(|| "ADDED".to_string());
            let (_req_title, requirement, next) = parse_requirement_block(&lines, i);
            i = next;

            let description = match op.as_str() {
                "ADDED" => format!("Add requirement: {}", requirement.text),
                "MODIFIED" => format!("Modify requirement: {}", requirement.text),
                "REMOVED" => format!("Remove requirement: {}", requirement.text),
                "RENAMED" => format!("Rename requirement: {}", requirement.text),
                _ => format!("Add requirement: {}", requirement.text),
            };
            out.push(ChangeDelta {
                spec: file.spec.clone(),
                operation: op,
                description,
                requirement: requirement.clone(),
                requirements: vec![requirement],
            });
            // Title is currently unused but parsed for parity with TS structure.
            let _ = title;
            continue;
        }

        i += 1;
    }

    out
}

fn parse_delta_op_header(line: &str) -> Option<String> {
    // Example: "## ADDED Requirements"
    let t = line.trim();
    let rest = t.strip_prefix("## ")?;
    let rest = rest.trim();
    let op = rest.strip_suffix(" Requirements").unwrap_or(rest).trim();
    if op == "ADDED" || op == "MODIFIED" || op == "REMOVED" || op == "RENAMED" {
        return Some(op.to_string());
    }
    None
}

fn parse_spec_requirements(markdown: &str) -> Vec<Requirement> {
    let req_section = extract_section_lines(markdown, "Requirements");
    parse_requirements_from_lines(&req_section)
}

fn parse_requirements_from_lines(lines: &[String]) -> Vec<Requirement> {
    let mut out: Vec<Requirement> = Vec::new();
    let mut i = 0usize;
    let raw: Vec<&str> = lines.iter().map(|s| s.as_str()).collect();
    while i < raw.len() {
        let line = raw[i].trim_end();
        if line.starts_with("### Requirement:") {
            let (_title, req, next) = parse_requirement_block(&raw, i);
            out.push(req);
            i = next;
            continue;
        }
        i += 1;
    }
    out
}

/// Parses a requirement block beginning at `lines[start]` (which must be a `### Requirement:` header).
///
/// The function extracts:
/// - the requirement title from the header,
/// - the normalized requirement statement text (collapsing internal whitespace),
/// - an optional Requirement ID from a metadata line of the form `- **Requirement ID**: <id>` (if present),
/// - any `#### Scenario:` blocks as `Scenario { raw_text }` preserving internal newlines and trimmed of trailing blank lines.
///   Parsing stops when the next `### Requirement:` or a top-level `## ` section is encountered; the returned index is the first unconsumed line.
///
/// # Parameters
/// - `lines`: slice of input lines (typically from the markdown) to parse from.
/// - `start`: index of the line that contains the `### Requirement:` header to begin parsing.
///
/// # Returns
/// A tuple `(title, requirement, next_index)` where `title` is the requirement header title, `requirement` is a `Requirement` with `text`, optional `requirement_id`, and collected `scenarios`, and `next_index` is the index of the first line not consumed by this requirement.
///
/// # Examples
///
/// ```ignore
/// let md = vec![
///     "### Requirement: Example".as_ref(),
///     "- **Requirement ID**: RQ-1".as_ref(),
///     "This is the requirement statement.".as_ref(),
///     "#### Scenario: happy path".as_ref(),
///     "Step 1".as_ref(),
///     "".as_ref(),
///     "Step 2".as_ref(),
///     "### Requirement: Next".as_ref(),
/// ];
/// let (title, req, next) = parse_requirement_block(&md, 0);
/// assert_eq!(title, "Example");
/// assert_eq!(req.requirement_id.as_deref(), Some("RQ-1"));
/// assert_eq!(req.text, "This is the requirement statement.");
/// assert_eq!(req.scenarios.len(), 1);
/// assert!(next >= 6);
/// ```
fn parse_requirement_block(lines: &[&str], start: usize) -> (String, Requirement, usize) {
    let header = lines[start].trim_end();
    let title = header
        .strip_prefix("### Requirement:")
        .unwrap_or("")
        .trim()
        .to_string();

    let mut i = start + 1;

    // Requirement statement: consume non-empty lines until we hit a scenario header or next requirement.
    let mut statement_lines: Vec<String> = Vec::new();
    let mut requirement_id: Option<String> = None;
    while i < lines.len() {
        let t = lines[i].trim_end();
        if t.starts_with("#### Scenario:")
            || t.starts_with("### Requirement:")
            || t.starts_with("## ")
        {
            break;
        }
        // Detect `- **Requirement ID**: <id>` metadata line; keep the first, ignore duplicates.
        if let Some(rest) = t
            .trim()
            .strip_prefix("- **Requirement ID**:")
            .map(str::trim)
        {
            if !rest.is_empty() && requirement_id.is_none() {
                requirement_id = Some(rest.to_string());
            }
            i += 1;
            continue;
        }
        if !t.trim().is_empty() {
            statement_lines.push(t.trim().to_string());
        }
        i += 1;
    }
    let text = collapse_whitespace(&statement_lines.join(" "));

    // Scenarios
    let mut scenarios: Vec<Scenario> = Vec::new();
    while i < lines.len() {
        let t = lines[i].trim_end();
        if t.starts_with("### Requirement:") || t.starts_with("## ") {
            break;
        }
        if let Some(_name) = t.strip_prefix("#### Scenario:") {
            i += 1;
            let mut raw_lines: Vec<String> = Vec::new();
            while i < lines.len() {
                let l = lines[i].trim_end();
                if l.starts_with("#### Scenario:")
                    || l.starts_with("### Requirement:")
                    || l.starts_with("## ")
                {
                    break;
                }
                raw_lines.push(l.to_string());
                i += 1;
            }
            let raw_text = trim_trailing_blank_lines(&raw_lines).join("\n");
            scenarios.push(Scenario { raw_text });
            continue;
        }
        i += 1;
    }

    (
        title,
        Requirement {
            text,
            requirement_id,
            scenarios,
        },
        i,
    )
}

/// Extracts the text of the named top-level section and collapses internal whitespace.
///
/// The returned string is the content of the first H2 section whose title matches `header` (case-insensitive),
/// joined into a single line with runs of whitespace replaced by a single space and trimmed.
///
/// # Examples
///
/// ```ignore
/// let md = "# Title\n\n## Purpose\nThis  is   a\npurpose.\n\n## Requirements\n...";
/// let txt = extract_section_text(md, "Purpose");
/// assert_eq!(txt, "This is a purpose.");
/// ```
fn extract_section_text(markdown: &str, header: &str) -> String {
    let lines = extract_section_lines(markdown, header);
    let joined = lines.join(" ");
    collapse_whitespace(joined.trim())
}

fn extract_section_lines(markdown: &str, header: &str) -> Vec<String> {
    let mut in_section = false;
    let mut out: Vec<String> = Vec::new();
    let normalized = markdown.replace('\r', "");
    for raw in normalized.split('\n') {
        let line = raw.trim_end();
        if let Some(h) = line.strip_prefix("## ") {
            let title = h.trim();
            if title.eq_ignore_ascii_case(header) {
                in_section = true;
                continue;
            }
            if in_section {
                break;
            }
        }
        if in_section {
            out.push(line.to_string());
        }
    }
    out
}

fn collapse_whitespace(input: &str) -> String {
    let mut out = String::new();
    let mut last_was_space = false;
    for ch in input.chars() {
        if ch.is_whitespace() {
            if !last_was_space {
                out.push(' ');
                last_was_space = true;
            }
        } else {
            out.push(ch);
            last_was_space = false;
        }
    }
    out.trim().to_string()
}

fn trim_trailing_blank_lines(lines: &[String]) -> Vec<String> {
    let mut start = 0usize;
    while start < lines.len() {
        if lines[start].trim().is_empty() {
            start += 1;
        } else {
            break;
        }
    }

    let mut end = lines.len();
    while end > start {
        if lines[end - 1].trim().is_empty() {
            end -= 1;
        } else {
            break;
        }
    }

    lines[start..end].to_vec()
}