tessera-design-toolkit 0.7.1

Tessera Design Toolkit (TDT) - CLI for managing engineering artifacts with requirements, risks, BOMs, tolerance analysis, and full traceability
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
//! Shared entity command infrastructure
//!
//! This module provides common patterns for entity CRUD operations,
//! reducing boilerplate across the 20+ command files.
//!
//! Note: Some of this infrastructure is not yet adopted - individual entity
//! commands have their own implementations. Kept for potential future refactoring.

#![allow(dead_code)]

use console::style;
use miette::{IntoDiagnostic, Result};
use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashSet;
use std::fs;
use std::path::PathBuf;

use crate::cli::{GlobalOpts, OutputFormat};
use tdt_core::core::identity::{EntityId, EntityPrefix};
use tdt_core::core::project::Project;
use tdt_core::core::shortid::ShortIdIndex;
use tdt_core::core::Config;

// =========================================================================
// Entity Configuration
// =========================================================================

/// Static configuration for an entity type
pub struct EntityConfig {
    /// Entity prefix (e.g., EntityPrefix::Sup)
    pub prefix: EntityPrefix,
    /// Directories where entities are stored (e.g., &["bom/suppliers"])
    pub dirs: &'static [&'static str],
    /// Singular name for messages (e.g., "supplier")
    pub name: &'static str,
    /// Plural name for messages (e.g., "suppliers")
    pub name_plural: &'static str,
}

// =========================================================================
// Common Show Implementation
// =========================================================================

/// Generic show command that handles YAML/JSON/ID output formats
///
/// For pretty output (default), call the entity-specific pretty printer after this.
pub fn run_show_generic<T>(
    id: &str,
    config: &EntityConfig,
    global: &GlobalOpts,
) -> Result<Option<(T, PathBuf)>>
where
    T: DeserializeOwned + Serialize,
{
    let project = Project::discover().map_err(|e| miette::miette!("{}", e))?;

    // Resolve short ID if needed
    let short_ids = ShortIdIndex::load(&project);
    let resolved_id = short_ids.resolve(id).unwrap_or_else(|| id.to_string());

    // Find the entity file
    let path = find_entity_file(&project, &resolved_id, config.dirs)?;

    // Read and parse entity
    let content = fs::read_to_string(&path).into_diagnostic()?;
    let entity: T = serde_yml::from_str(&content).into_diagnostic()?;

    match global.output {
        OutputFormat::Yaml => {
            print!("{}", content);
            Ok(None)
        }
        OutputFormat::Json => {
            let json = serde_json::to_string_pretty(&entity).into_diagnostic()?;
            println!("{}", json);
            Ok(None)
        }
        OutputFormat::Id => {
            // For ID output, we need to extract the ID - caller handles this
            Ok(Some((entity, path)))
        }
        OutputFormat::ShortId => {
            // For ShortId output, caller handles this
            Ok(Some((entity, path)))
        }
        _ => {
            // Return entity for pretty printing
            Ok(Some((entity, path)))
        }
    }
}

/// Print entity ID in the requested format
pub fn print_entity_id(id: &EntityId, format: OutputFormat, project: &Project) {
    match format {
        OutputFormat::ShortId => {
            let short_ids = ShortIdIndex::load(project);
            let short_id = short_ids.get_short_id(&id.to_string()).unwrap_or_default();
            println!("{}", short_id);
        }
        _ => {
            println!("{}", id);
        }
    }
}

// =========================================================================
// Common Edit Implementation
// =========================================================================

/// Generic edit command
pub fn run_edit_generic(id: &str, config: &EntityConfig) -> Result<()> {
    let project = Project::discover().map_err(|e| miette::miette!("{}", e))?;
    let cli_config = Config::load();

    // Resolve short ID if needed
    let short_ids = ShortIdIndex::load(&project);
    let resolved_id = short_ids.resolve(id).unwrap_or_else(|| id.to_string());

    // Find the entity file
    let path = find_entity_file(&project, &resolved_id, config.dirs)?;

    println!(
        "Opening {} in {}...",
        style(path.display()).cyan(),
        style(cli_config.editor()).yellow()
    );

    cli_config.run_editor(&path).into_diagnostic()?;

    // Sync cache after editing (in case user saved changes)
    crate::cli::commands::utils::sync_cache(&project);

    Ok(())
}

// =========================================================================
// File Finding Utilities
// =========================================================================

/// Find an entity file in the given directories
pub fn find_entity_file(
    project: &Project,
    entity_id: &str,
    entity_dirs: &[&str],
) -> Result<PathBuf> {
    for dir in entity_dirs {
        let dir_path = project.root().join(dir);
        if !dir_path.exists() {
            continue;
        }

        for entry in fs::read_dir(&dir_path).into_diagnostic()? {
            let entry = entry.into_diagnostic()?;
            let path = entry.path();

            if path.extension().is_some_and(|e| e == "yaml") {
                let filename = path.file_stem().and_then(|s| s.to_str()).unwrap_or("");
                if filename.contains(entity_id) || filename.starts_with(entity_id) {
                    return Ok(path);
                }
            }
        }
    }

    Err(miette::miette!("No entity found matching '{}'", entity_id))
}

// =========================================================================
// Status Filter Conversion
// =========================================================================

use crate::cli::filters::StatusFilter;

/// Convert StatusFilter to cache-compatible Option<&str>
pub fn status_filter_to_str(filter: StatusFilter) -> Option<&'static str> {
    match filter {
        StatusFilter::Draft => Some("draft"),
        StatusFilter::Review => Some("review"),
        StatusFilter::Approved => Some("approved"),
        StatusFilter::Released => Some("released"),
        StatusFilter::Obsolete => Some("obsolete"),
        StatusFilter::Active | StatusFilter::All => None,
    }
}

/// Check if a status string matches the filter
pub fn status_matches_filter(status: &str, filter: StatusFilter) -> bool {
    match filter {
        StatusFilter::Draft => status == "draft",
        StatusFilter::Review => status == "review",
        StatusFilter::Approved => status == "approved",
        StatusFilter::Released => status == "released",
        StatusFilter::Obsolete => status == "obsolete",
        StatusFilter::Active => status != "obsolete",
        StatusFilter::All => true,
    }
}

/// Check if a Status enum matches the filter
pub fn status_enum_matches_filter(
    status: &tdt_core::core::entity::Status,
    filter: StatusFilter,
) -> bool {
    use tdt_core::core::entity::Status;
    match filter {
        StatusFilter::Draft => *status == Status::Draft,
        StatusFilter::Review => *status == Status::Review,
        StatusFilter::Approved => *status == Status::Approved,
        StatusFilter::Released => *status == Status::Released,
        StatusFilter::Obsolete => *status == Status::Obsolete,
        StatusFilter::Active => *status != Status::Obsolete,
        StatusFilter::All => true,
    }
}

/// Convert StatusFilter to Option<Status> for EntityFilter
pub fn status_filter_to_status(filter: StatusFilter) -> Option<tdt_core::core::entity::Status> {
    use tdt_core::core::entity::Status;
    match filter {
        StatusFilter::Draft => Some(Status::Draft),
        StatusFilter::Review => Some(Status::Review),
        StatusFilter::Approved => Some(Status::Approved),
        StatusFilter::Released => Some(Status::Released),
        StatusFilter::Obsolete => Some(Status::Obsolete),
        StatusFilter::Active | StatusFilter::All => None,
    }
}

// =========================================================================
// Common New Output Helpers
// =========================================================================

/// Output format for newly created entity
pub fn output_new_entity(
    id: &EntityId,
    file_path: &std::path::Path,
    short_id: Option<String>,
    entity_name: &str,
    title: &str,
    extra_info: Option<&str>,
    added_links: &[(String, String)],
    global: &GlobalOpts,
) {
    use crate::cli::helpers::format_short_id;

    match global.output {
        OutputFormat::Id => {
            println!("{}", id);
        }
        OutputFormat::ShortId => {
            println!("{}", short_id.unwrap_or_else(|| format_short_id(id)));
        }
        OutputFormat::Path => {
            println!("{}", file_path.display());
        }
        _ => {
            let display_id = short_id.unwrap_or_else(|| format_short_id(id));
            println!(
                "{} Created {} {}",
                style("✓").green(),
                entity_name,
                style(&display_id).cyan()
            );
            println!("   {}", style(file_path.display()).dim());

            if let Some(info) = extra_info {
                println!("   {}", info);
            } else {
                println!("   {}", style(title).yellow());
            }

            // Show added links
            for (link_type, target) in added_links {
                if let Ok(target_id) = EntityId::parse(target) {
                    println!(
                        "   {} --[{}]--> {}",
                        style("→").dim(),
                        style(link_type).cyan(),
                        style(format_short_id(&target_id)).yellow()
                    );
                }
            }

            let schema_type = id.prefix().as_str().to_lowercase();
            println!(
                "   {} {}",
                style("?").dim(),
                style(format!("See all fields: tdt schema show {}", schema_type)).dim()
            );
        }
    }
}

// =========================================================================
// Common List Output Helpers
// =========================================================================

/// Print "No X found" message
pub fn print_no_results(name_plural: &str) {
    println!("No {} found.", name_plural);
}

/// Print list footer with count
pub fn print_list_footer(count: usize, prefix: EntityPrefix) {
    println!();
    println!(
        "{} {}(es) found. Use {} to reference by short ID.",
        style(count).cyan(),
        prefix,
        style(format!("{}@N", prefix)).cyan()
    );
}

// =========================================================================
// Generic List Implementation
// =========================================================================

use crate::cli::table::{ColumnDef, TableConfig, TableFormatter, TableRow};
use tdt_core::core::entity::Entity;
use tdt_core::services::{ListableService, SortDirection};

/// Configuration for generic list commands
pub struct ListConfig<E, C, S> {
    /// Column definitions for table output
    pub columns: &'static [ColumnDef],
    /// Entity name for messages (e.g., "requirement")
    pub entity_name: &'static str,
    /// Entity prefix string for table footer (e.g., "REQ")
    pub prefix_str: &'static str,
    /// Convert full entity to table row
    pub entity_to_row: fn(&E, &ShortIdIndex) -> TableRow,
    /// Convert cached entity to table row
    pub cached_to_row: fn(&C, &ShortIdIndex) -> TableRow,
    /// Optional function to sort cached entities
    pub cached_sort: Option<fn(&mut [C], S, SortDirection)>,
}

/// Common list arguments extracted from CLI args
#[derive(Debug, Clone)]
pub struct CommonListArgs {
    pub columns: Vec<String>,
    pub limit: Option<usize>,
    pub reverse: bool,
    pub count: bool,
    pub wrap: Option<usize>,
}

/// Run a generic list command using a service
///
/// This function handles:
/// - Two-tier caching (cache for table output, full entities for JSON/YAML)
/// - All output formats (table, tsv, csv, md, json, yaml, id, shortid)
/// - Sorting, limiting, reversing
/// - Short ID assignment
///
/// # Arguments
/// * `config` - Entity-specific list configuration
/// * `service` - The service implementing ListableService
/// * `filter` - Entity-specific filter
/// * `sort_field` - Field to sort by
/// * `sort_dir` - Sort direction
/// * `args` - Common list arguments (columns, limit, etc.)
/// * `global` - Global CLI options (output format)
/// * `project` - Project reference
/// * `needs_full_for_filter` - True if post-filtering requires full entities
/// * `allowed_ids` - If Some, only include entities whose ID is in this set (for --linked-to)
pub fn run_list_generic<E, C, F, S, Svc>(
    config: &ListConfig<E, C, S>,
    service: &Svc,
    filter: &F,
    sort_field: S,
    sort_dir: SortDirection,
    args: &CommonListArgs,
    global: &GlobalOpts,
    project: &Project,
    needs_full_for_filter: bool,
    allowed_ids: Option<&HashSet<String>>,
) -> Result<()>
where
    E: Entity + Serialize + Clone,
    C: Clone,
    S: Copy,
    Svc: ListableService<E, C, F, S>,
{
    let format = match global.output {
        OutputFormat::Auto => OutputFormat::Tsv,
        f => f,
    };

    let needs_full_output = matches!(format, OutputFormat::Json | OutputFormat::Yaml);
    let needs_full = needs_full_output || needs_full_for_filter;

    if needs_full {
        // Full entity path - required for JSON/YAML or cross-entity filters
        let result = service
            .list(filter, sort_field, sort_dir)
            .map_err(|e| miette::miette!("{}", e))?;

        let mut items = result.items;

        // Apply linked-to filter
        if let Some(ids) = allowed_ids {
            items.retain(|e| ids.contains(&e.id().to_string()));
        }

        // Apply reverse and limit
        if args.reverse {
            items.reverse();
        }
        if let Some(limit) = args.limit {
            items.truncate(limit);
        }

        // Count only mode
        if args.count {
            println!("{}", items.len());
            return Ok(());
        }

        if items.is_empty() {
            return output_empty_list(config.entity_name, format);
        }

        // Update short IDs
        let mut short_ids = ShortIdIndex::load(project);
        short_ids.ensure_all(items.iter().map(|e| e.id().to_string()));
        super::commands::utils::save_short_ids(&mut short_ids, project);

        output_full_entities(&items, config, &short_ids, args, format)
    } else {
        // Cache path - fast, no YAML parsing needed
        let result = service
            .list_cached(filter)
            .map_err(|e| miette::miette!("{}", e))?;

        let mut items = result.items;

        // Sort cached entities if a sort function is provided
        if let Some(sort_fn) = config.cached_sort {
            sort_fn(&mut items, sort_field, sort_dir);
        }

        // Apply linked-to filter
        if let Some(ids) = allowed_ids {
            let short_ids_temp = ShortIdIndex::load(project);
            items.retain(|e| {
                let row = (config.cached_to_row)(e, &short_ids_temp);
                ids.contains(&row.full_id)
            });
        }

        // Apply reverse and limit
        if args.reverse {
            items.reverse();
        }
        if let Some(limit) = args.limit {
            items.truncate(limit);
        }

        // Count only mode
        if args.count {
            println!("{}", items.len());
            return Ok(());
        }

        if items.is_empty() {
            println!("No {} found.", config.entity_name);
            return Ok(());
        }

        // Update short IDs for cached items (need to extract IDs differently)
        let mut short_ids = ShortIdIndex::load(project);
        // Note: for cached items, we get the ID from the row conversion
        // We'll update short IDs based on the rows
        let rows: Vec<TableRow> = items
            .iter()
            .map(|e| (config.cached_to_row)(e, &short_ids))
            .collect();
        short_ids.ensure_all(rows.iter().map(|r| r.full_id.clone()));
        super::commands::utils::save_short_ids(&mut short_ids, project);

        output_cached_entities(&items, config, &short_ids, args, format)
    }
}

/// Output empty list in appropriate format
fn output_empty_list(entity_name: &str, format: OutputFormat) -> Result<()> {
    match format {
        OutputFormat::Json => println!("[]"),
        OutputFormat::Yaml => println!("[]"),
        _ => println!("No {} found.", entity_name),
    }
    Ok(())
}

/// Output full entities (for JSON/YAML or when full data needed)
pub fn output_full_entities<E, C, S>(
    items: &[E],
    config: &ListConfig<E, C, S>,
    short_ids: &ShortIdIndex,
    args: &CommonListArgs,
    format: OutputFormat,
) -> Result<()>
where
    E: Serialize + Clone,
{
    match format {
        OutputFormat::Json => {
            let json = serde_json::to_string_pretty(items).map_err(|e| miette::miette!("{}", e))?;
            println!("{}", json);
        }
        OutputFormat::Yaml => {
            let yaml = serde_yml::to_string(items).map_err(|e| miette::miette!("{}", e))?;
            print!("{}", yaml);
        }
        OutputFormat::Id | OutputFormat::ShortId => {
            for item in items {
                let row = (config.entity_to_row)(item, short_ids);
                if format == OutputFormat::ShortId {
                    println!("{}", row.short_id);
                } else {
                    println!("{}", row.full_id);
                }
            }
        }
        _ => {
            // Table formats (table, tsv, csv, md)
            let rows: Vec<TableRow> = items
                .iter()
                .map(|e| (config.entity_to_row)(e, short_ids))
                .collect();

            let columns: Vec<&str> = args.columns.iter().map(|s| s.as_str()).collect();

            let table_config = TableConfig {
                wrap_width: args.wrap,
                show_summary: true,
            };
            let formatter =
                TableFormatter::new(config.columns, config.entity_name, config.prefix_str)
                    .with_config(table_config);
            formatter.output(rows, format, &columns);
        }
    }
    Ok(())
}

/// Output cached entities (fast path for table output)
pub fn output_cached_entities<E, C, S>(
    items: &[C],
    config: &ListConfig<E, C, S>,
    short_ids: &ShortIdIndex,
    args: &CommonListArgs,
    format: OutputFormat,
) -> Result<()>
where
    C: Clone,
{
    match format {
        OutputFormat::Id | OutputFormat::ShortId => {
            for item in items {
                let row = (config.cached_to_row)(item, short_ids);
                if format == OutputFormat::ShortId {
                    println!("{}", row.short_id);
                } else {
                    println!("{}", row.full_id);
                }
            }
        }
        _ => {
            // Table formats (table, tsv, csv, md)
            let rows: Vec<TableRow> = items
                .iter()
                .map(|e| (config.cached_to_row)(e, short_ids))
                .collect();

            let columns: Vec<&str> = args.columns.iter().map(|s| s.as_str()).collect();

            let table_config = TableConfig {
                wrap_width: args.wrap,
                show_summary: true,
            };
            let formatter =
                TableFormatter::new(config.columns, config.entity_name, config.prefix_str)
                    .with_config(table_config);
            formatter.output(rows, format, &columns);
        }
    }
    Ok(())
}

// =========================================================================
// Link Handling
// =========================================================================

use tdt_core::core::links::add_inferred_link;

/// Process --link flags and add inferred links to a newly created entity
pub fn process_link_flags(
    file_path: &std::path::Path,
    source_prefix: EntityPrefix,
    link_targets: &[String],
    short_ids: &ShortIdIndex,
) -> Vec<(String, String)> {
    let mut added_links = Vec::new();

    for link_target in link_targets {
        let resolved_target = short_ids
            .resolve(link_target)
            .unwrap_or_else(|| link_target.clone());

        if let Ok(target_entity_id) = EntityId::parse(&resolved_target) {
            match add_inferred_link(
                file_path,
                source_prefix,
                &resolved_target,
                target_entity_id.prefix(),
            ) {
                Ok(link_type) => {
                    added_links.push((link_type, resolved_target.clone()));
                }
                Err(e) => {
                    eprintln!(
                        "{} Failed to add link to {}: {}",
                        style("!").yellow(),
                        link_target,
                        e
                    );
                }
            }
        } else {
            eprintln!("{} Invalid entity ID: {}", style("!").yellow(), link_target);
        }
    }

    added_links
}

/// Add a link between two entities by inferring the link type.
/// This is a convenience wrapper around the core add_inferred_link for use
/// when the caller already knows both entity prefixes (e.g., CAPA --ncr flag).
pub fn add_inferred_link_to_file(
    file_path: &std::path::Path,
    source_prefix: EntityPrefix,
    target_id: &str,
    target_prefix: EntityPrefix,
) -> std::result::Result<String, String> {
    use tdt_core::core::links::add_inferred_link;
    add_inferred_link(file_path, source_prefix, target_id, target_prefix).map_err(|e| e.to_string())
}