Skip to main content

syster_cli/
lib.rs

1//! syster-cli library - Core analysis functionality
2//!
3//! This module provides the `run_analysis` function for parsing and analyzing
4//! SysML v2 and KerML files using the syster-base library.
5
6use serde::Serialize;
7use std::path::{Path, PathBuf};
8use syster::hir::{Severity, check_file};
9use syster::ide::AnalysisHost;
10use walkdir::WalkDir;
11
12/// Result of analyzing SysML/KerML files.
13#[derive(Debug, Serialize)]
14pub struct AnalysisResult {
15    /// Number of files analyzed.
16    pub file_count: usize,
17    /// Total number of symbols found.
18    pub symbol_count: usize,
19    /// Number of errors found.
20    pub error_count: usize,
21    /// Number of warnings found.
22    pub warning_count: usize,
23    /// All diagnostics collected.
24    pub diagnostics: Vec<DiagnosticInfo>,
25}
26
27/// A diagnostic message with location information.
28#[derive(Debug, Clone, Serialize)]
29pub struct DiagnosticInfo {
30    /// File path containing the diagnostic.
31    pub file: String,
32    /// Start line (1-indexed).
33    pub line: u32,
34    /// Start column (1-indexed).
35    pub col: u32,
36    /// End line (1-indexed).
37    pub end_line: u32,
38    /// End column (1-indexed).
39    pub end_col: u32,
40    /// The diagnostic message.
41    pub message: String,
42    /// Severity level.
43    #[serde(serialize_with = "serialize_severity")]
44    pub severity: Severity,
45    /// Optional error code.
46    pub code: Option<String>,
47}
48
49/// Serialize Severity as a string
50fn serialize_severity<S>(severity: &Severity, serializer: S) -> Result<S::Ok, S::Error>
51where
52    S: serde::Serializer,
53{
54    let s = match severity {
55        Severity::Error => "error",
56        Severity::Warning => "warning",
57        Severity::Info => "info",
58        Severity::Hint => "hint",
59    };
60    serializer.serialize_str(s)
61}
62
63/// Run analysis on input file or directory.
64///
65/// # Arguments
66/// * `input` - Path to a file or directory to analyze
67/// * `verbose` - Enable verbose output
68/// * `load_stdlib` - Whether to load the standard library
69/// * `stdlib_path` - Optional custom path to the standard library
70///
71/// # Returns
72/// An `AnalysisResult` with file count, symbol count, and diagnostics.
73pub fn run_analysis(
74    input: &Path,
75    verbose: bool,
76    load_stdlib: bool,
77    stdlib_path: Option<&Path>,
78) -> Result<AnalysisResult, String> {
79    let mut host = AnalysisHost::new();
80
81    // 1. Load stdlib if requested
82    if load_stdlib {
83        load_stdlib_files(&mut host, stdlib_path, verbose)?;
84    }
85
86    // 2. Load input file(s)
87    load_input(&mut host, input, verbose)?;
88
89    // 3. Trigger index rebuild and get analysis
90    let _analysis = host.analysis();
91
92    // 4. Collect diagnostics from all files
93    let diagnostics = collect_diagnostics(&host);
94
95    // 5. Build result
96    let error_count = diagnostics
97        .iter()
98        .filter(|d| matches!(d.severity, Severity::Error))
99        .count();
100    let warning_count = diagnostics
101        .iter()
102        .filter(|d| matches!(d.severity, Severity::Warning))
103        .count();
104
105    Ok(AnalysisResult {
106        file_count: host.file_count(),
107        symbol_count: host.symbol_index().all_symbols().count(),
108        error_count,
109        warning_count,
110        diagnostics,
111    })
112}
113
114/// Load input file or directory.
115fn load_input(host: &mut AnalysisHost, input: &Path, verbose: bool) -> Result<(), String> {
116    if input.is_file() {
117        load_file(host, input, verbose)
118    } else if input.is_dir() {
119        load_directory(host, input, verbose)
120    } else {
121        Err(format!("Path does not exist: {}", input.display()))
122    }
123}
124
125/// Load a single file into the analysis host.
126fn load_file(host: &mut AnalysisHost, path: &Path, verbose: bool) -> Result<(), String> {
127    if verbose {
128        println!("  Loading: {}", path.display());
129    }
130
131    let content = std::fs::read_to_string(path)
132        .map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
133
134    let path_str = path.to_string_lossy();
135    let parse_errors = host.set_file_content(&path_str, &content);
136
137    // Parse errors are reported but don't fail the load
138    for err in parse_errors {
139        eprintln!(
140            "parse error: {}:{}:{}: {}",
141            path.display(),
142            err.position.line,
143            err.position.column,
144            err.message
145        );
146    }
147
148    Ok(())
149}
150
151/// Load all SysML/KerML files from a directory.
152fn load_directory(host: &mut AnalysisHost, dir: &Path, verbose: bool) -> Result<(), String> {
153    if verbose {
154        println!("Scanning directory: {}", dir.display());
155    }
156
157    for entry in WalkDir::new(dir).follow_links(true) {
158        let entry = entry.map_err(|e| format!("Walk error: {}", e))?;
159        let path = entry.path();
160
161        if is_sysml_file(path) {
162            load_file(host, path, verbose)?;
163        }
164    }
165
166    Ok(())
167}
168
169/// Check if a path is a SysML or KerML file.
170fn is_sysml_file(path: &Path) -> bool {
171    path.is_file()
172        && matches!(
173            path.extension().and_then(|e| e.to_str()),
174            Some("sysml") | Some("kerml")
175        )
176}
177
178/// Load standard library files.
179fn load_stdlib_files(
180    host: &mut AnalysisHost,
181    custom_path: Option<&Path>,
182    verbose: bool,
183) -> Result<(), String> {
184    if verbose {
185        println!("Loading standard library...");
186    }
187
188    // Try custom path first
189    if let Some(path) = custom_path {
190        if path.exists() {
191            return load_directory(host, path, verbose);
192        } else {
193            return Err(format!("Stdlib path does not exist: {}", path.display()));
194        }
195    }
196
197    // Try default locations
198    let default_paths = [
199        PathBuf::from("sysml.library"),
200        PathBuf::from("../sysml.library"),
201        PathBuf::from("../base/sysml.library"),
202    ];
203
204    for path in &default_paths {
205        if path.exists() {
206            return load_directory(host, path, verbose);
207        }
208    }
209
210    if verbose {
211        println!("  Warning: Standard library not found");
212    }
213
214    Ok(())
215}
216
217/// Collect diagnostics from all files in the host.
218fn collect_diagnostics(host: &AnalysisHost) -> Vec<DiagnosticInfo> {
219    let mut all_diagnostics = Vec::new();
220
221    for path in host.files().keys() {
222        if let Some(file_id) = host.get_file_id_for_path(path) {
223            let file_path = path.to_string_lossy().to_string();
224            let diagnostics = check_file(host.symbol_index(), file_id);
225
226            for diag in diagnostics {
227                all_diagnostics.push(DiagnosticInfo {
228                    file: file_path.clone(),
229                    line: diag.start_line + 1, // 1-indexed for display
230                    col: diag.start_col + 1,
231                    end_line: diag.end_line + 1,
232                    end_col: diag.end_col + 1,
233                    message: diag.message.to_string(),
234                    severity: diag.severity,
235                    code: diag.code.map(|c| c.to_string()),
236                });
237            }
238        }
239    }
240
241    // Sort by file, then line, then column
242    all_diagnostics.sort_by(|a, b| (&a.file, a.line, a.col).cmp(&(&b.file, b.line, b.col)));
243
244    all_diagnostics
245}
246
247// ============================================================================
248// EXPORT FUNCTIONS
249// ============================================================================
250
251/// A symbol for JSON export (simplified from HirSymbol).
252#[derive(Debug, Serialize)]
253pub struct ExportSymbol {
254    pub name: String,
255    pub qualified_name: String,
256    pub kind: String,
257    pub file: String,
258    pub start_line: u32,
259    pub start_col: u32,
260    pub end_line: u32,
261    pub end_col: u32,
262    #[serde(skip_serializing_if = "Option::is_none")]
263    pub doc: Option<String>,
264    #[serde(skip_serializing_if = "Vec::is_empty")]
265    pub supertypes: Vec<String>,
266}
267
268/// AST export result.
269#[derive(Debug, Serialize)]
270pub struct AstExport {
271    pub files: Vec<FileAst>,
272}
273
274/// AST for a single file.
275#[derive(Debug, Serialize)]
276pub struct FileAst {
277    pub path: String,
278    pub symbols: Vec<ExportSymbol>,
279}
280
281/// Export AST (symbols) for all files.
282pub fn export_ast(
283    input: &Path,
284    verbose: bool,
285    load_stdlib: bool,
286    stdlib_path: Option<&Path>,
287) -> Result<String, String> {
288    let mut host = AnalysisHost::new();
289
290    if load_stdlib {
291        load_stdlib_files(&mut host, stdlib_path, verbose)?;
292    }
293
294    load_input(&mut host, input, verbose)?;
295    let _analysis = host.analysis();
296
297    let mut files = Vec::new();
298
299    // Only export user files, not stdlib
300    for path in host.files().keys() {
301        let path_str = path.to_string_lossy().to_string();
302
303        // Skip stdlib files
304        if path_str.contains("sysml.library") {
305            continue;
306        }
307
308        if let Some(file_id) = host.get_file_id_for_path(path) {
309            let symbols: Vec<ExportSymbol> = host
310                .symbol_index()
311                .symbols_in_file(file_id)
312                .into_iter()
313                .map(|sym| ExportSymbol {
314                    name: sym.name.to_string(),
315                    qualified_name: sym.qualified_name.to_string(),
316                    kind: format!("{:?}", sym.kind),
317                    file: path_str.clone(),
318                    start_line: sym.start_line + 1,
319                    start_col: sym.start_col + 1,
320                    end_line: sym.end_line + 1,
321                    end_col: sym.end_col + 1,
322                    doc: sym.doc.as_ref().map(|d| d.to_string()),
323                    supertypes: sym.supertypes.iter().map(|s| s.to_string()).collect(),
324                })
325                .collect();
326
327            files.push(FileAst {
328                path: path_str,
329                symbols,
330            });
331        }
332    }
333
334    // Sort files by path for consistent output
335    files.sort_by(|a, b| a.path.cmp(&b.path));
336
337    let export = AstExport { files };
338    serde_json::to_string_pretty(&export).map_err(|e| format!("Failed to serialize AST: {}", e))
339}
340
341/// Export analysis result as JSON.
342pub fn export_json(result: &AnalysisResult) -> Result<String, String> {
343    serde_json::to_string_pretty(result).map_err(|e| format!("Failed to serialize result: {}", e))
344}
345
346// ============================================================================
347// INTERCHANGE EXPORT
348// ============================================================================
349
350/// Export a model to an interchange format.
351///
352/// Supported formats:
353/// - `xmi` - XML Model Interchange
354/// - `kpar` - Kernel Package Archive (ZIP)
355/// - `jsonld` - JSON-LD
356///
357/// # Arguments
358/// * `input` - Path to a file or directory to analyze
359/// * `format` - Output format (xmi, kpar, jsonld)
360/// * `verbose` - Enable verbose output
361/// * `load_stdlib` - Whether to load the standard library
362/// * `stdlib_path` - Optional custom path to the standard library
363///
364/// # Returns
365/// The serialized model as bytes.
366#[cfg(feature = "interchange")]
367pub fn export_model(
368    input: &Path,
369    format: &str,
370    verbose: bool,
371    load_stdlib: bool,
372    stdlib_path: Option<&Path>,
373) -> Result<Vec<u8>, String> {
374    use syster::interchange::{
375        JsonLd, Kpar, ModelFormat, Xmi, model_from_symbols, restore_ids_from_symbols,
376    };
377
378    let mut host = AnalysisHost::new();
379
380    // 1. Load stdlib if requested
381    if load_stdlib {
382        load_stdlib_files(&mut host, stdlib_path, verbose)?;
383    }
384
385    // 2. Load input file(s)
386    load_input(&mut host, input, verbose)?;
387
388    // 2.5. Load metadata if present (for ID preservation on round-trip)
389    #[cfg(feature = "interchange")]
390    {
391        use syster::project::WorkspaceLoader;
392        let loader = WorkspaceLoader::new();
393
394        // If input is a file, check for companion metadata
395        if input.is_file() {
396            let parent_dir = input.parent().unwrap_or(input);
397            if let Err(e) = loader.load_metadata_from_directory(parent_dir, &mut host) {
398                if verbose {
399                    eprintln!("Note: Could not load metadata: {}", e);
400                }
401            } else if verbose {
402                println!("Loaded metadata from {}", parent_dir.display());
403            }
404        } else if input.is_dir() {
405            // For directories, load metadata from that directory
406            if let Err(e) = loader.load_metadata_from_directory(input, &mut host) {
407                if verbose {
408                    eprintln!("Note: Could not load metadata: {}", e);
409                }
410            } else if verbose {
411                println!("Loaded metadata from {}", input.display());
412            }
413        }
414    }
415
416    // 3. Trigger index rebuild
417    let analysis = host.analysis();
418
419    // 4. Get all symbols from the index
420    let symbols: Vec<_> = analysis.symbol_index().all_symbols().cloned().collect();
421
422    // 5. Convert to interchange model
423    let mut model = model_from_symbols(&symbols);
424
425    // 6. Restore original element IDs from symbols (if they exist)
426    model = restore_ids_from_symbols(model, analysis.symbol_index());
427    if verbose {
428        println!("Restored element IDs from symbol database");
429    }
430
431    if verbose {
432        println!(
433            "Exported model: {} elements, {} relationships",
434            model.elements.len(),
435            model.relationships.len()
436        );
437    }
438
439    // 8. Serialize to requested format
440    match format.to_lowercase().as_str() {
441        "xmi" => Xmi.write(&model).map_err(|e| e.to_string()),
442        "kpar" => Kpar.write(&model).map_err(|e| e.to_string()),
443        "jsonld" | "json-ld" => JsonLd.write(&model).map_err(|e| e.to_string()),
444        _ => Err(format!(
445            "Unsupported format: {}. Use xmi, kpar, or jsonld.",
446            format
447        )),
448    }
449}
450
451/// Result of importing a model from an interchange format.
452#[cfg(feature = "interchange")]
453#[derive(Debug)]
454pub struct ImportResult {
455    /// Number of elements imported.
456    pub element_count: usize,
457    /// Number of relationships imported.
458    pub relationship_count: usize,
459    /// Number of validation errors.
460    pub error_count: usize,
461    /// Validation messages.
462    pub messages: Vec<String>,
463}
464
465/// Import a model from an interchange format file (validation only).
466///
467/// This validates the model but doesn't load it into a workspace.
468/// For importing into a workspace, use `import_model_into_host()`.
469///
470/// Supported formats are detected from file extension:
471/// - `.xmi` - XML Model Interchange
472/// - `.kpar` - Kernel Package Archive (ZIP)
473/// - `.jsonld`, `.json` - JSON-LD
474///
475/// # Arguments
476/// * `input` - Path to the interchange file
477/// * `format` - Optional format override (otherwise detected from extension)
478/// * `verbose` - Enable verbose output
479///
480/// # Returns
481/// An `ImportResult` with element count and symbol info.
482#[cfg(feature = "interchange")]
483pub fn import_model_into_host(
484    host: &mut AnalysisHost,
485    input: &Path,
486    format: Option<&str>,
487    verbose: bool,
488) -> Result<ImportResult, String> {
489    use syster::interchange::{JsonLd, Kpar, ModelFormat, Xmi, detect_format, symbols_from_model};
490
491    // Read the input file
492    let bytes =
493        std::fs::read(input).map_err(|e| format!("Failed to read {}: {}", input.display(), e))?;
494
495    // Determine format
496    let format_str = format.map(String::from).unwrap_or_else(|| {
497        input
498            .extension()
499            .and_then(|e| e.to_str())
500            .unwrap_or("xmi")
501            .to_string()
502    });
503
504    if verbose {
505        println!(
506            "Importing {} as {} into workspace",
507            input.display(),
508            format_str
509        );
510    }
511
512    // Parse the model
513    let model = match format_str.to_lowercase().as_str() {
514        "xmi" | "sysmlx" | "kermlx" => Xmi.read(&bytes).map_err(|e| e.to_string())?,
515        "kpar" => Kpar.read(&bytes).map_err(|e| e.to_string())?,
516        "jsonld" | "json-ld" | "json" => JsonLd.read(&bytes).map_err(|e| e.to_string())?,
517        _ => {
518            // Try to detect from file extension
519            if let Some(format_impl) = detect_format(input) {
520                format_impl.read(&bytes).map_err(|e| e.to_string())?
521            } else {
522                return Err(format!(
523                    "Unknown format: {}. Use xmi, sysmlx, kermlx, kpar, or jsonld.",
524                    format_str
525                ));
526            }
527        }
528    };
529
530    // Convert model to symbols
531    let symbols = symbols_from_model(&model);
532    let symbol_count = symbols.len();
533
534    if verbose {
535        println!(
536            "Converted {} elements to {} symbols",
537            model.elements.len(),
538            symbol_count
539        );
540    }
541
542    // Add symbols to host
543    host.add_symbols_from_model(symbols);
544
545    if verbose {
546        println!("Loaded symbols into workspace with preserved element IDs");
547    }
548
549    Ok(ImportResult {
550        element_count: model.elements.len(),
551        relationship_count: model.relationships.len(),
552        error_count: 0,
553        messages: vec![format!("Successfully imported {} symbols", symbol_count)],
554    })
555}
556
557/// Import and validate a model from an interchange format (legacy version).
558///
559/// This validates the model but doesn't load it into a workspace.
560/// For importing into a workspace, use `import_model_into_host()`.
561///
562/// # Arguments
563/// * `input` - Path to the model file
564/// * `format` - Optional format override (xmi, kpar, jsonld)
565/// * `verbose` - Enable verbose output
566///
567/// # Returns
568/// An `ImportResult` with element count and validation info.
569#[cfg(feature = "interchange")]
570pub fn import_model(
571    input: &Path,
572    format: Option<&str>,
573    verbose: bool,
574) -> Result<ImportResult, String> {
575    use syster::interchange::{JsonLd, Kpar, ModelFormat, Xmi, detect_format};
576
577    // Read the input file
578    let bytes =
579        std::fs::read(input).map_err(|e| format!("Failed to read {}: {}", input.display(), e))?;
580
581    // Determine format
582    let format_str = format.map(String::from).unwrap_or_else(|| {
583        input
584            .extension()
585            .and_then(|e| e.to_str())
586            .unwrap_or("xmi")
587            .to_string()
588    });
589
590    if verbose {
591        println!("Importing {} as {}", input.display(), format_str);
592    }
593
594    // Parse the model
595    let model = match format_str.to_lowercase().as_str() {
596        "xmi" | "sysmlx" | "kermlx" => Xmi.read(&bytes).map_err(|e| e.to_string())?,
597        "kpar" => Kpar.read(&bytes).map_err(|e| e.to_string())?,
598        "jsonld" | "json-ld" | "json" => JsonLd.read(&bytes).map_err(|e| e.to_string())?,
599        _ => {
600            // Try to detect from file extension
601            if let Some(format_impl) = detect_format(input) {
602                format_impl.read(&bytes).map_err(|e| e.to_string())?
603            } else {
604                return Err(format!(
605                    "Unknown format: {}. Use xmi, sysmlx, kermlx, kpar, or jsonld.",
606                    format_str
607                ));
608            }
609        }
610    };
611
612    // Basic validation
613    let mut messages = Vec::new();
614    let mut error_count = 0;
615
616    // Check for orphan relationships (references to non-existent elements)
617    for rel in &model.relationships {
618        if model.elements.get(&rel.source).is_none() {
619            messages.push(format!(
620                "Warning: Relationship source '{}' not found",
621                rel.source
622            ));
623            error_count += 1;
624        }
625        if model.elements.get(&rel.target).is_none() {
626            messages.push(format!(
627                "Warning: Relationship target '{}' not found",
628                rel.target
629            ));
630            error_count += 1;
631        }
632    }
633
634    if verbose {
635        println!(
636            "Imported: {} elements, {} relationships, {} validation issues",
637            model.elements.len(),
638            model.relationships.len(),
639            error_count
640        );
641        for msg in &messages {
642            println!("  {}", msg);
643        }
644    }
645
646    Ok(ImportResult {
647        element_count: model.elements.len(),
648        relationship_count: model.relationships.len(),
649        error_count,
650        messages,
651    })
652}
653
654/// Result of decompiling a model to SysML files.
655#[cfg(feature = "interchange")]
656#[derive(Debug)]
657pub struct DecompileResult {
658    /// Generated SysML text.
659    pub sysml_text: String,
660    /// Metadata JSON for preserving element IDs.
661    pub metadata_json: String,
662    /// Number of elements decompiled.
663    pub element_count: usize,
664    /// Source file path.
665    pub source_path: String,
666}
667
668/// Decompile an interchange file to SysML text with metadata.
669///
670/// This function converts an XMI/KPAR/JSON-LD file to SysML text plus
671/// a companion metadata JSON file that preserves element IDs for
672/// lossless round-tripping.
673///
674/// # Arguments
675/// * `input` - Path to the interchange file
676/// * `format` - Optional format override (otherwise detected from extension)
677/// * `verbose` - Enable verbose output
678///
679/// # Returns
680/// A `DecompileResult` with SysML text and metadata JSON.
681#[cfg(feature = "interchange")]
682pub fn decompile_model(
683    input: &Path,
684    format: Option<&str>,
685    verbose: bool,
686) -> Result<DecompileResult, String> {
687    use syster::interchange::{
688        JsonLd, Kpar, ModelFormat, SourceInfo, Xmi, decompile_with_source, detect_format,
689    };
690
691    // Read the input file
692    let bytes =
693        std::fs::read(input).map_err(|e| format!("Failed to read {}: {}", input.display(), e))?;
694
695    // Determine format
696    let format_str = format.map(String::from).unwrap_or_else(|| {
697        input
698            .extension()
699            .and_then(|e| e.to_str())
700            .unwrap_or("xmi")
701            .to_string()
702    });
703
704    if verbose {
705        println!("Decompiling {} as {}", input.display(), format_str);
706    }
707
708    // Parse the model
709    let model = match format_str.to_lowercase().as_str() {
710        "xmi" | "sysmlx" | "kermlx" => Xmi.read(&bytes).map_err(|e| e.to_string())?,
711        "kpar" => Kpar.read(&bytes).map_err(|e| e.to_string())?,
712        "jsonld" | "json-ld" | "json" => JsonLd.read(&bytes).map_err(|e| e.to_string())?,
713        _ => {
714            if let Some(format_impl) = detect_format(input) {
715                format_impl.read(&bytes).map_err(|e| e.to_string())?
716            } else {
717                return Err(format!(
718                    "Unknown format: {}. Use xmi, sysmlx, kermlx, kpar, or jsonld.",
719                    format_str
720                ));
721            }
722        }
723    };
724
725    let element_count = model.elements.len();
726
727    // Create source info
728    let source = SourceInfo::from_path(input.to_string_lossy()).with_format(&format_str);
729
730    // Decompile to SysML
731    let result = decompile_with_source(&model, source);
732
733    if verbose {
734        println!(
735            "Decompiled: {} elements -> {} chars of SysML, {} metadata entries",
736            element_count,
737            result.text.len(),
738            result.metadata.elements.len()
739        );
740    }
741
742    // Serialize metadata to JSON
743    let metadata_json = serde_json::to_string_pretty(&result.metadata)
744        .map_err(|e| format!("Failed to serialize metadata: {}", e))?;
745
746    Ok(DecompileResult {
747        sysml_text: result.text,
748        metadata_json,
749        element_count,
750        source_path: input.to_string_lossy().to_string(),
751    })
752}