oxirs 0.3.1

Command-line interface for OxiRS - import, export, migration, and benchmarking tools
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
//! `run()` function and subcommand dispatch for aspect_analyzer
use crate::cli::CliResult;
use crate::commands::aspect_analyzer_formats::{from_dtdl, prettyprint, usage, validate};
use crate::{AspectAction, EditAction};
use std::path::PathBuf;

/// Run an Aspect command (Java ESMF SDK compatible)
pub async fn run(action: AspectAction) -> CliResult<()> {
    match action {
        AspectAction::Validate {
            file,
            detailed,
            format,
        } => validate(file, detailed, format).await,
        AspectAction::Prettyprint {
            file,
            output,
            format,
            comments,
        } => prettyprint(file, output, format, comments).await,
        AspectAction::To {
            file,
            format,
            output,
            examples,
            format_variant,
        } => convert(file, format, output, examples, format_variant).await,
        AspectAction::Edit { action } => edit(action).await,
        AspectAction::Usage { input, models_root } => usage(input, models_root).await,
        AspectAction::From {
            file,
            output,
            format,
        } => from_dtdl(file, output, format).await,
    }
}

/// Convert SAMM Aspect model to other formats (Java ESMF SDK compatible)
pub(crate) async fn convert(
    file: PathBuf,
    format: String,
    _output: Option<PathBuf>,
    _examples: bool,
    format_variant: Option<String>,
) -> CliResult<()> {
    use crate::commands::aspect_analyzer_formats::{
        convert_aas, convert_asyncapi, convert_diagram, convert_dtdl, convert_graphql,
        convert_html, convert_java, convert_jsonld, convert_jsonschema, convert_markdown,
        convert_openapi, convert_payload, convert_python, convert_rust, convert_scala, convert_sql,
        convert_typescript,
    };

    let aspect = oxirs_samm::parser::parse_aspect_model(&file)
        .await
        .map_err(|e| format!("SAMM conversion error: {}", e))?;

    match format.as_str() {
        "rust" => convert_rust(&aspect),
        "markdown" => convert_markdown(&aspect),
        "jsonschema" => convert_jsonschema(&aspect),
        "openapi" => convert_openapi(&aspect),
        "asyncapi" => convert_asyncapi(&aspect),
        "html" => convert_html(&aspect),
        "aas" => convert_aas(&aspect, &format_variant),
        "diagram" => convert_diagram(&aspect, &format_variant),
        "sql" => convert_sql(&aspect, &format_variant),
        "jsonld" => convert_jsonld(&aspect),
        "payload" => convert_payload(&aspect, _examples),
        "graphql" => convert_graphql(&aspect),
        "typescript" | "ts" => convert_typescript(&aspect),
        "dtdl" => convert_dtdl(&aspect),
        "python" | "py" => convert_python(&aspect),
        "java" => convert_java(&aspect),
        "scala" => convert_scala(&aspect),
        _ => {
            eprintln!("Error: Unsupported format '{}'", format);
            eprintln!("Supported formats: rust, markdown, jsonschema, openapi, asyncapi, html, aas, diagram, sql, jsonld, payload, graphql, typescript, dtdl, python, java, scala");
            Err(format!("Unsupported format: {}", format).into())
        }
    }
}

/// Edit Aspect model (move elements or create new version)
async fn edit(action: EditAction) -> CliResult<()> {
    match action {
        EditAction::Move {
            file,
            element,
            namespace,
            dry_run,
            details,
            force,
            copy_file_header,
        } => {
            edit_move(
                file,
                element,
                namespace,
                dry_run,
                details,
                force,
                copy_file_header,
            )
            .await
        }
        EditAction::Newversion {
            file,
            major,
            minor,
            micro,
            dry_run,
            details,
            force,
        } => edit_newversion(file, major, minor, micro, dry_run, details, force).await,
    }
}

/// Move element to different namespace
async fn edit_move(
    file: PathBuf,
    element: String,
    namespace: Option<String>,
    dry_run: bool,
    details: bool,
    force: bool,
    copy_file_header: bool,
) -> CliResult<()> {
    use tokio::fs;

    println!("Moving element in Aspect model...");
    println!("  File: {}", file.display());
    println!("  Element: {}", element);

    let content = fs::read_to_string(&file)
        .await
        .map_err(|e| format!("Failed to read file: {}", e))?;

    let mut current_namespace = String::new();
    let mut current_version = String::new();

    for line in content.lines() {
        if let Some(urn_start) = line.find("urn:samm:") {
            let urn_part = &line[urn_start..];
            if let Some(urn_end) = urn_part.find(['>', ' ', ';']) {
                let urn = &urn_part[..urn_end];
                if let Some(samm_part) = urn.strip_prefix("urn:samm:") {
                    if !samm_part.contains("esmf.samm") {
                        let parts: Vec<&str> = samm_part.splitn(2, '#').collect();
                        let urn_base = parts[0];
                        let ns_version: Vec<&str> = urn_base.rsplitn(2, ':').collect();
                        if ns_version.len() == 2 {
                            current_version = ns_version[0].to_string();
                            current_namespace = ns_version[1].to_string();
                            break;
                        }
                    }
                }
            }
        }
    }

    if current_namespace.is_empty() {
        return Err("Could not determine namespace from file".into());
    }

    let target_namespace = if let Some(ns) = &namespace {
        println!("  Target Namespace: {}", ns);
        ns.clone()
    } else {
        println!(
            "  Target Namespace: {} (same namespace, new file)",
            current_namespace
        );
        current_namespace.clone()
    };

    if dry_run {
        println!("  Mode: DRY RUN");
    }
    if details {
        println!("  Details: ENABLED");
    }
    println!();

    let element_urn_pattern = format!("#{}", element);
    let mut element_found = false;
    let mut element_lines: Vec<String> = Vec::new();
    let mut in_element_block = false;
    let mut brace_depth = 0i32;

    println!("Searching for element '{}'...", element);

    for line in content.lines() {
        if line.contains(&element_urn_pattern) && line.contains("a samm:") {
            element_found = true;
            in_element_block = true;
            element_lines.push(line.to_string());
            brace_depth += line.matches('[').count() as i32;
            brace_depth -= line.matches(']').count() as i32;
            if line.trim().ends_with('.') && brace_depth == 0 {
                in_element_block = false;
            }
            continue;
        }
        if in_element_block {
            element_lines.push(line.to_string());
            brace_depth += line.matches('[').count() as i32;
            brace_depth -= line.matches(']').count() as i32;
            if line.trim().ends_with('.') && brace_depth == 0 {
                in_element_block = false;
            }
        }
    }

    if !element_found {
        return Err(format!("Element '{}' not found in file", element).into());
    }

    println!(
        "✓ Found element '{}' ({} lines)",
        element,
        element_lines.len()
    );
    println!();

    let old_urn = format!(
        "urn:samm:{}:{}#{}",
        current_namespace, current_version, element
    );
    let new_urn = format!(
        "urn:samm:{}:{}#{}",
        target_namespace, current_version, element
    );

    let mut new_element_content = element_lines.join("\n");
    new_element_content = new_element_content.replace(&old_urn, &new_urn);

    if details {
        println!("Element URN Update:");
        println!("  Old: {}", old_urn);
        println!("  New: {}", new_urn);
        println!();
        println!("Element Content ({} lines):", element_lines.len());
        for (i, line) in element_lines.iter().take(5).enumerate() {
            println!("  {}: {}", i + 1, line);
        }
        if element_lines.len() > 5 {
            println!("  ... ({} more lines)", element_lines.len() - 5);
        }
        println!();
    }

    let mut source_lines: Vec<String> = Vec::new();
    let mut skip_lines = false;
    let mut skip_depth = 0i32;

    for line in content.lines() {
        if line.contains(&element_urn_pattern) && line.contains("a samm:") {
            skip_lines = true;
            skip_depth = line.matches('[').count() as i32 - line.matches(']').count() as i32;
            if line.trim().ends_with('.') && skip_depth == 0 {
                skip_lines = false;
            }
            continue;
        }
        if skip_lines {
            skip_depth += line.matches('[').count() as i32;
            skip_depth -= line.matches(']').count() as i32;
            if line.trim().ends_with('.') && skip_depth == 0 {
                skip_lines = false;
            }
            continue;
        }
        source_lines.push(line.to_string());
    }

    let new_source_content = source_lines.join("\n");

    if !dry_run {
        let target_file = if namespace.is_some() {
            let target_dir = PathBuf::from(&target_namespace).join(&current_version);
            let target_filename = format!("{}Element.ttl", element);
            target_dir.join(&target_filename)
        } else {
            let target_filename = format!("{}Element.ttl", element);
            file.parent()
                .unwrap_or(std::path::Path::new("."))
                .join(&target_filename)
        };

        if target_file.exists() && !force {
            return Err(format!(
                "Target file already exists: {} (use --force to overwrite)",
                target_file.display()
            )
            .into());
        }

        let mut target_content = String::new();
        if copy_file_header {
            for line in content.lines() {
                if line.starts_with('@') || line.trim().is_empty() || line.starts_with('#') {
                    target_content.push_str(line);
                    target_content.push('\n');
                } else {
                    break;
                }
            }
            target_content.push('\n');
        } else {
            target_content.push_str(&format!(
                "@prefix samm: <urn:samm:org.eclipse.esmf.samm:meta-model:{}#> .\n",
                current_version
            ));
            target_content.push_str(&format!(
                "@prefix : <urn:samm:{}:{}#> .\n\n",
                target_namespace, current_version
            ));
        }

        target_content.push_str(&new_element_content);

        if let Some(parent) = target_file.parent() {
            fs::create_dir_all(parent)
                .await
                .map_err(|e| format!("Failed to create directory: {}", e))?;
        }

        fs::write(&target_file, target_content)
            .await
            .map_err(|e| format!("Failed to write target file: {}", e))?;

        println!("✓ Element moved successfully");
        println!("  Target: {}", target_file.display());

        if force {
            fs::write(&file, new_source_content)
                .await
                .map_err(|e| format!("Failed to update source file: {}", e))?;
            println!("  Source: {} (element removed)", file.display());
        } else {
            println!(
                "  Source: {} (unchanged - use --force to remove element)",
                file.display()
            );
        }
    } else {
        println!("DRY RUN - No files written");
        println!(
            "  Would create: {}/{}/{}Element.ttl",
            target_namespace, current_version, element
        );
        if force {
            println!("  Would update: {} (remove element)", file.display());
        } else {
            println!("  Source unchanged (--force not specified)");
        }
    }

    Ok(())
}

/// Create new version of Aspect model
async fn edit_newversion(
    file: PathBuf,
    major: bool,
    minor: bool,
    micro: bool,
    dry_run: bool,
    details: bool,
    force: bool,
) -> CliResult<()> {
    use std::collections::HashMap;
    use tokio::fs;

    println!("Creating new version of Aspect model...");
    println!("  File: {}", file.display());

    let version_type = if major {
        "MAJOR"
    } else if minor {
        "MINOR"
    } else if micro {
        "MICRO"
    } else {
        "MINOR (default)"
    };
    println!("  Version Update: {}", version_type);

    if dry_run {
        println!("  Mode: DRY RUN");
    }
    if details {
        println!("  Details: ENABLED");
    }
    println!();

    let content = fs::read_to_string(&file)
        .await
        .map_err(|e| format!("Failed to read file: {}", e))?;

    let mut urn_map: HashMap<String, String> = HashMap::new();
    let mut namespace = String::new();
    let mut old_version = String::new();

    for line in content.lines() {
        if let Some(urn_start) = line.find("urn:samm:") {
            let urn_part = &line[urn_start..];
            if let Some(urn_end) = urn_part.find(['>', ' ', ';']) {
                let urn = &urn_part[..urn_end];
                if let Some(samm_part) = urn.strip_prefix("urn:samm:") {
                    if samm_part.contains("esmf.samm") {
                        continue;
                    }
                    let parts: Vec<&str> = samm_part.splitn(2, '#').collect();
                    let urn_base = parts[0];
                    let ns_version: Vec<&str> = urn_base.rsplitn(2, ':').collect();
                    if ns_version.len() == 2 {
                        let ver = ns_version[0];
                        let ns = ns_version[1];
                        if namespace.is_empty() {
                            namespace = ns.to_string();
                            old_version = ver.to_string();
                        }
                        urn_map.insert(urn.to_string(), format!("{}:{}", ns, ver));
                    }
                }
            }
        }
    }

    if old_version.is_empty() {
        return Err("Could not find version in URN".into());
    }

    let version_parts: Vec<&str> = old_version.split('.').collect();
    if version_parts.len() != 3 {
        return Err(format!("Invalid version format: {} (expected X.Y.Z)", old_version).into());
    }

    let mut ver_major: u32 = version_parts[0]
        .parse()
        .map_err(|_| format!("Invalid major version: {}", version_parts[0]))?;
    let mut ver_minor: u32 = version_parts[1]
        .parse()
        .map_err(|_| format!("Invalid minor version: {}", version_parts[1]))?;
    let mut ver_micro: u32 = version_parts[2]
        .parse()
        .map_err(|_| format!("Invalid micro version: {}", version_parts[2]))?;

    if major {
        ver_major += 1;
        ver_minor = 0;
        ver_micro = 0;
    } else if micro {
        ver_micro += 1;
    } else {
        ver_minor += 1;
        ver_micro = 0;
    }

    let new_version = format!("{}.{}.{}", ver_major, ver_minor, ver_micro);

    println!("Version Update:");
    println!("  {}{}", old_version, new_version);
    println!("  Namespace: {}", namespace);
    println!();

    let mut new_content = content.clone();
    for (old_urn, ns_ver) in &urn_map {
        let new_urn = old_urn.replace(
            &format!(
                "{}:{}",
                ns_ver.split(':').next_back().unwrap_or(""),
                old_version
            ),
            &format!(
                "{}:{}",
                ns_ver.split(':').next_back().unwrap_or(""),
                new_version
            ),
        );
        new_content = new_content.replace(old_urn, &new_urn);
    }

    if details {
        println!("URN Updates:");
        let mut updates: Vec<_> = urn_map.keys().collect();
        updates.sort();
        for old_urn in updates {
            let ns_ver = &urn_map[old_urn];
            let new_urn = old_urn.replace(
                &format!(
                    "{}:{}",
                    ns_ver.split(':').next_back().unwrap_or(""),
                    old_version
                ),
                &format!(
                    "{}:{}",
                    ns_ver.split(':').next_back().unwrap_or(""),
                    new_version
                ),
            );
            println!("  {}", old_urn);
            println!("    {}", new_urn);
        }
        println!();
    }

    if !dry_run {
        let filename = file.file_name().ok_or("Invalid filename")?;

        let output_dir = if let Some(parent) = file.parent() {
            if let Some(version_dir) = parent.file_name() {
                if version_dir.to_string_lossy() == old_version {
                    if let Some(ns_dir) = parent.parent() {
                        ns_dir.join(&new_version)
                    } else {
                        parent
                            .parent()
                            .unwrap_or(parent)
                            .join(&namespace)
                            .join(&new_version)
                    }
                } else {
                    parent.join(&namespace).join(&new_version)
                }
            } else {
                parent.join(&namespace).join(&new_version)
            }
        } else {
            PathBuf::from(&namespace).join(&new_version)
        };

        let output_file = output_dir.join(filename);

        if output_file.exists() && !force {
            return Err(format!(
                "Output file already exists: {} (use --force to overwrite)",
                output_file.display()
            )
            .into());
        }

        fs::create_dir_all(&output_dir)
            .await
            .map_err(|e| format!("Failed to create directory: {}", e))?;

        fs::write(&output_file, new_content)
            .await
            .map_err(|e| format!("Failed to write file: {}", e))?;

        println!("✓ New version created successfully");
        println!("  Output: {}", output_file.display());
    } else {
        println!("DRY RUN - No files written");
        println!(
            "  Would create: {}/{}/{}",
            namespace,
            new_version,
            file.file_name()
                .expect("file path should have a file name")
                .to_string_lossy()
        );
    }

    Ok(())
}