mecha10-core 0.6.5

Mecha10 core runtime and foundational types
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
//! Example usage of Message Versioning and Schema Evolution
//!
//! This example demonstrates how to handle backward-compatible schema evolution
//! in distributed robotic systems.

use mecha10::prelude::*;

/// Example 1: Basic schema versioning
pub fn basic_versioning_example() -> Result<()> {
    info!("Example 1: Basic schema versioning");

    let v1 = MessageSchema::new("LaserScan", 1)
        .with_description("2D laser scan data")
        .with_field("timestamp", "integer", true)
        .with_field("ranges", "array", true)
        .with_field("intensities", "array", true);

    info!("LaserScan v1:");
    info!("  Fields: timestamp (required), ranges (required), intensities (required)");

    let v2 = MessageSchema::new("LaserScan", 2)
        .with_description("2D laser scan data with angle info")
        .with_field("timestamp", "integer", true)
        .with_field("ranges", "array", true)
        .with_field("intensities", "array", true)
        .with_field("angle_min", "number", false)
        .with_field("angle_max", "number", false);

    info!("\nLaserScan v2:");
    info!("  Added: angle_min (optional), angle_max (optional)");

    Ok(())
}

/// Example 2: Compatibility checking
pub fn compatibility_checking_example() -> Result<()> {
    info!("Example 2: Compatibility checking");

    let v1 = MessageSchema::new("Image", 1)
        .with_field("width", "integer", true)
        .with_field("height", "integer", true)
        .with_field("data", "array", true);

    let v2 = MessageSchema::new("Image", 2)
        .with_field("width", "integer", true)
        .with_field("height", "integer", true)
        .with_field("data", "array", true)
        .with_field("encoding", "string", false);

    let check = CompatibilityCheck::check(&v1, &v2, CompatibilityMode::Backward);

    info!("\nBackward compatibility check (v1 → v2):");
    info!("  Compatible: {}", check.compatible);
    info!("  Evolutions:");
    for evolution in &check.evolutions {
        info!("    {:?}", evolution);
    }
    info!("  Warnings:");
    for warning in &check.warnings {
        info!("    {}", warning);
    }

    Ok(())
}

/// Example 3: Backward compatibility mode
pub fn backward_compatibility_example() -> Result<()> {
    info!("Example 3: Backward compatibility");
    info!("  New schema can read old data");
    info!("  Rules:");
    info!("    ✓ Can add optional fields");
    info!("    ✓ Can make required fields optional");
    info!("    ✗ Cannot remove required fields");
    info!("    ✗ Cannot add required fields");
    info!("    ✗ Cannot change field types");

    let v1 = MessageSchema::new("Odometry", 1)
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true);

    let v2_valid = MessageSchema::new("Odometry", 2)
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("covariance", "array", false);

    let check = CompatibilityCheck::check(&v1, &v2_valid, CompatibilityMode::Backward);
    info!("\nAdding optional field:");
    info!("  Compatible: {} ✓", check.compatible);

    let v2_invalid = MessageSchema::new("Odometry", 2)
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("velocity", "number", true);

    let check = CompatibilityCheck::check(&v1, &v2_invalid, CompatibilityMode::Backward);
    info!("\nAdding required field:");
    info!("  Compatible: {} ✗", check.compatible);
    info!("  Incompatibilities:");
    for incomp in &check.incompatibilities {
        info!("    {}", incomp);
    }

    Ok(())
}

/// Example 4: Forward compatibility mode
pub fn forward_compatibility_example() -> Result<()> {
    info!("Example 4: Forward compatibility");
    info!("  Old schema can read new data");
    info!("  Rules:");
    info!("    ✓ Can remove optional fields");
    info!("    ✓ Can make optional fields required");
    info!("    ✗ Cannot add required fields");
    info!("    ✗ Cannot remove required fields");
    info!("    ✗ Cannot change field types");

    let v1 = MessageSchema::new("Point", 1)
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("label", "string", false);

    let v2_valid = MessageSchema::new("Point", 2)
        .with_field("x", "number", true)
        .with_field("y", "number", true);

    let check = CompatibilityCheck::check(&v1, &v2_valid, CompatibilityMode::Forward);
    info!("\nRemoving optional field:");
    info!("  Compatible: {} ✓", check.compatible);

    Ok(())
}

/// Example 5: Full compatibility mode
pub fn full_compatibility_example() -> Result<()> {
    info!("Example 5: Full compatibility (Backward + Forward)");
    info!("  Both old and new schemas can read each other's data");
    info!("  Rules:");
    info!("    ✓ Can add/remove optional fields");
    info!("    ✗ Cannot change field types");
    info!("    ✗ Cannot change required status");

    let v1 = MessageSchema::new("Config", 1)
        .with_field("timeout", "integer", true)
        .with_field("retries", "integer", true)
        .with_field("debug", "boolean", false);

    let v2_valid = MessageSchema::new("Config", 2)
        .with_field("timeout", "integer", true)
        .with_field("retries", "integer", true)
        .with_field("debug", "boolean", false)
        .with_field("verbose", "boolean", false);

    let check = CompatibilityCheck::check(&v1, &v2_valid, CompatibilityMode::Full);
    info!("\nAdding optional field:");
    info!("  Compatible: {} ✓", check.compatible);

    let v2_invalid = MessageSchema::new("Config", 2)
        .with_field("timeout", "integer", true)
        .with_field("retries", "integer", true)
        .with_field("debug", "boolean", true);

    let check = CompatibilityCheck::check(&v1, &v2_invalid, CompatibilityMode::Full);
    info!("\nChanging field to required:");
    info!("  Compatible: {} ✗", check.compatible);
    info!("  Incompatibilities:");
    for incomp in &check.incompatibilities {
        info!("    {}", incomp);
    }

    Ok(())
}

/// Example 6: Strict compatibility mode
pub fn strict_compatibility_example() -> Result<()> {
    info!("Example 6: Strict compatibility");
    info!("  No schema changes allowed");
    info!("  Rules:");
    info!("    ✗ No changes of any kind");

    let v1 = MessageSchema::new("StrictMessage", 1)
        .with_field("id", "integer", true)
        .with_field("data", "string", true);

    let v2 = MessageSchema::new("StrictMessage", 2)
        .with_field("id", "integer", true)
        .with_field("data", "string", true)
        .with_field("metadata", "object", false);

    let check = CompatibilityCheck::check(&v1, &v2, CompatibilityMode::Strict);
    info!("\nAdding any field:");
    info!("  Compatible: {} ✗", check.compatible);
    info!("  Incompatibilities:");
    for incomp in &check.incompatibilities {
        info!("    {}", incomp);
    }

    Ok(())
}

/// Example 7: Schema evolution operations
pub fn evolution_operations_example() -> Result<()> {
    info!("Example 7: Schema evolution operations");

    let v1 = MessageSchema::new("Sensor", 1)
        .with_field("id", "integer", true)
        .with_field("value", "number", true)
        .with_field("deprecated_field", "string", true);

    let v2 = MessageSchema::new("Sensor", 2)
        .with_field("id", "integer", true)
        .with_field("value", "number", true)
        .with_field("unit", "string", false)
        .with_field("quality", "number", false);

    let check = CompatibilityCheck::check(&v1, &v2, CompatibilityMode::Backward);

    info!("\nDetected evolutions:");
    for evolution in &check.evolutions {
        match evolution {
            SchemaEvolution::FieldAdded {
                name,
                field_type,
                required,
                ..
            } => {
                info!(
                    "  + Field added: {} ({}, {})",
                    name,
                    field_type,
                    if *required { "required" } else { "optional" }
                );
            }
            SchemaEvolution::FieldRemoved { name } => {
                info!("  - Field removed: {}", name);
            }
            SchemaEvolution::FieldMadeOptional { name } => {
                info!("  ~ Field made optional: {}", name);
            }
            SchemaEvolution::FieldMadeRequired { name, .. } => {
                info!("  ~ Field made required: {}", name);
            }
            SchemaEvolution::FieldTypeChanged {
                name,
                old_type,
                new_type,
            } => {
                info!("  ⚠ Field type changed: {} ({} → {})", name, old_type, new_type);
            }
            SchemaEvolution::FieldRenamed { old_name, new_name } => {
                info!("  → Field renamed: {} → {}", old_name, new_name);
            }
        }
    }

    Ok(())
}

/// Example 8: Real-world migration scenario
pub fn migration_scenario_example() -> Result<()> {
    info!("Example 8: Real-world migration scenario");
    info!("  Evolving a robot pose message over time");

    let v1 = MessageSchema::new("RobotPose", 1)
        .with_description("Robot 2D pose")
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("timestamp", "integer", true);

    info!("\nV1 (Initial): 2D pose only");

    let v2 = MessageSchema::new("RobotPose", 2)
        .with_description("Robot 2D pose with uncertainty")
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("timestamp", "integer", true)
        .with_field("covariance", "array", false);

    let check_v1_v2 = CompatibilityCheck::check(&v1, &v2, CompatibilityMode::Backward);
    info!("\nV2: Added covariance - Compatible: {} ✓", check_v1_v2.compatible);

    let v3 = MessageSchema::new("RobotPose", 3)
        .with_description("Robot pose with optional 3D")
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("timestamp", "integer", true)
        .with_field("covariance", "array", false)
        .with_field("z", "number", false)
        .with_field("roll", "number", false)
        .with_field("pitch", "number", false);

    let check_v2_v3 = CompatibilityCheck::check(&v2, &v3, CompatibilityMode::Backward);
    info!("V3: Added 3D fields - Compatible: {} ✓", check_v2_v3.compatible);

    let v4 = MessageSchema::new("RobotPose", 4)
        .with_description("Robot pose with optional timestamp")
        .with_field("x", "number", true)
        .with_field("y", "number", true)
        .with_field("theta", "number", true)
        .with_field("timestamp", "integer", false)
        .with_field("covariance", "array", false)
        .with_field("z", "number", false)
        .with_field("roll", "number", false)
        .with_field("pitch", "number", false);

    let check_v3_v4 = CompatibilityCheck::check(&v3, &v4, CompatibilityMode::Backward);
    info!("V4: Timestamp optional - Compatible: {} ✓", check_v3_v4.compatible);

    info!("\nMigration path: v1 → v2 → v3 → v4");
    info!("  All transitions backward compatible");
    info!("  Old code continues to work with new messages");

    Ok(())
}

/// Example 9: Deprecation workflow
pub fn deprecation_example() -> Result<()> {
    info!("Example 9: Deprecation workflow");

    let v1 = MessageSchema::new("OldAPI", 1)
        .with_description("Legacy API - deprecated")
        .with_field("data", "string", true)
        .with_metadata("deprecated", "true")
        .with_metadata("deprecation_message", "Use NewAPI instead. Will be removed in v3.0");

    info!("\nDeprecated schema:");
    info!("  Version: {}", v1.version);
    if v1.metadata.contains_key("deprecated") {
        info!("  ⚠ DEPRECATED");
        if let Some(msg) = v1.metadata.get("deprecation_message") {
            info!("  Message: {}", msg);
        }
    }

    let v2 = MessageSchema::new("NewAPI", 1)
        .with_description("Replacement API")
        .with_field("data", "string", true)
        .with_field("version", "integer", true)
        .with_field("checksum", "string", true);

    info!("\nNew API:");
    info!("  Version: {}", v2.version);
    info!("  Description: {}", v2.description);
    info!("  Migration: Add version and checksum fields");

    Ok(())
}

/// Example 10: Best practices
pub fn best_practices_example() -> Result<()> {
    info!("Example 10: Schema evolution best practices");

    info!("\n1. Always add new fields as optional:");
    info!("   ✓ field: data, type: string, required: false");
    info!("   ✗ field: data, type: string, required: true");

    info!("\n2. Never change field types:");
    info!("   ✗ old: timestamp (integer) → new: timestamp (string)");
    info!("   ✓ Add new field with different name instead");

    info!("\n3. Make fields optional before removing:");
    info!("   v1: field required");
    info!("   v2: field optional (transition)");
    info!("   v3: field removed");

    info!("\n4. Use metadata for compatibility hints:");
    info!("   - deprecated: true");
    info!("   - deprecation_message: Migration instructions");
    info!("   - min_supported_version: 2");
    info!("   - compatibility_mode: Backward");

    info!("\n5. Version incrementally:");
    info!("   ✓ v1 → v2 → v3 (gradual changes)");
    info!("   ✗ v1 → v10 (big bang)");

    info!("\n6. Test compatibility before deployment:");
    info!("   let check = CompatibilityCheck::check(&old, &new, mode);");
    info!("   assert!(check.compatible);");

    Ok(())
}

fn main() -> Result<()> {
    tracing_subscriber::fmt().with_max_level(tracing::Level::INFO).init();

    println!("Schema Versioning and Evolution Examples");
    println!("========================================\n");
    println!("Backward-compatible schema evolution for distributed systems.");
    println!();

    basic_versioning_example()?;
    println!();

    compatibility_checking_example()?;
    println!();

    backward_compatibility_example()?;
    println!();

    forward_compatibility_example()?;
    println!();

    full_compatibility_example()?;
    println!();

    strict_compatibility_example()?;
    println!();

    evolution_operations_example()?;
    println!();

    migration_scenario_example()?;
    println!();

    deprecation_example()?;
    println!();

    best_practices_example()?;
    println!();

    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("All examples completed!");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");

    println!("Compatibility Modes Summary:");
    println!("  • None - No checking");
    println!("  • Backward - New code reads old data");
    println!("  • Forward - Old code reads new data");
    println!("  • Full - Both directions work");
    println!("  • Strict - No changes allowed");
    println!();
    println!("Common Patterns:");
    println!("  ✓ Add optional fields");
    println!("  ✓ Make required → optional");
    println!("  ✓ Deprecate before removing");
    println!("  ✗ Change field types");
    println!("  ✗ Add required fields");
    println!("  ✗ Remove required fields");

    Ok(())
}