motto 0.4.3

Compiler-as-a-Service: Turn Rust schema.rs into multi-platform SDK toolkits
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
//! Motto CLI - Code generation tool for multi-platform SDKs

use anyhow::{Context, Result};
use clap::{Args, Parser, Subcommand};
use motto::prelude::*;
use std::path::PathBuf;
use tracing::{Level, info};
use tracing_subscriber::FmtSubscriber;

#[derive(Parser)]
#[command(name = "motto")]
#[command(author, version, about = "Generate multi-platform SDKs from Rust schema", long_about = None)]
struct Cli {
    /// Enable verbose logging
    #[arg(short, long, global = true)]
    verbose: bool,

    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Initialize a new motto project
    Init(InitArgs),

    /// Generate SDK code from schema.rs
    Generate(GenerateArgs),

    /// Check schema for breaking changes
    Check(CheckArgs),

    /// Update motto.lock with new schema fingerprint
    Lock(LockArgs),

    /// Watch for schema changes and regenerate
    Watch(WatchArgs),

    /// Sniff transport traffic for debugging
    Sniff(SniffArgs),
}

#[derive(Args)]
struct InitArgs {
    /// Project directory (defaults to current directory)
    #[arg(short, long)]
    path: Option<PathBuf>,

    /// Template to use (minimal, full)
    #[arg(short, long, default_value = "minimal")]
    template: String,
}

#[derive(Args)]
struct GenerateArgs {
    /// Path to schema.rs file
    #[arg(short, long, default_value = "src/schema.rs")]
    schema: PathBuf,

    /// Output directory for generated code
    #[arg(short, long, default_value = "generated")]
    output: PathBuf,

    /// Target platforms to generate (comma-separated)
    /// Options: typescript, swift, kotlin, unity, rust, all
    #[arg(short, long, default_value = "all")]
    targets: String,

    /// Force regeneration even if fingerprint matches
    #[arg(short, long)]
    force: bool,

    /// Generate WASM bindings for TypeScript target
    #[arg(long)]
    wasm: bool,

    /// Generate native addon bindings (napi-rs) for TypeScript target
    #[arg(long)]
    native: bool,

    /// Transport mode for generated SDKs: runtime (pure-language, default) or ffi (shared Rust core)
    #[arg(long, default_value = "runtime")]
    transport: String,
}

#[derive(Args)]
struct CheckArgs {
    /// Path to schema.rs file
    #[arg(short, long, default_value = "src/schema.rs")]
    schema: PathBuf,

    /// Path to motto.lock file
    #[arg(short, long, default_value = "motto.lock")]
    lock: PathBuf,

    /// Strict mode: treat warnings as errors
    #[arg(long)]
    strict: bool,
}

#[derive(Args)]
struct LockArgs {
    /// Path to schema.rs file
    #[arg(short, long, default_value = "src/schema.rs")]
    schema: PathBuf,

    /// Path to motto.lock file
    #[arg(short, long, default_value = "motto.lock")]
    lock: PathBuf,

    /// Bump version (major, minor, patch)
    #[arg(short, long, default_value = "patch")]
    bump: String,
}

#[derive(Args)]
struct WatchArgs {
    /// Path to schema.rs file
    #[arg(short, long, default_value = "src/schema.rs")]
    schema: PathBuf,

    /// Output directory for generated code
    #[arg(short, long, default_value = "generated")]
    output: PathBuf,

    /// Target platforms to generate
    #[arg(short, long, default_value = "all")]
    targets: String,
}

#[derive(Args)]
struct SniffArgs {
    /// Sniff mode: tap (listen to FFI transport events) or proxy (WS relay with frame logging)
    #[arg(short, long, default_value = "tap")]
    mode: String,

    /// Output format: pretty (human-readable), json, or hex
    #[arg(short, long, default_value = "pretty")]
    format: String,

    /// Path to schema.rs for protocol-aware decoding
    #[arg(short, long, default_value = "src/schema.rs")]
    schema: PathBuf,

    /// Disable schema-aware packet decoding
    #[arg(long)]
    no_decode: bool,

    /// Redact field values in output (show structure only)
    #[arg(long)]
    redact: bool,

    /// Maximum bytes to display per packet (0 = unlimited)
    #[arg(long, default_value = "0")]
    max_bytes: usize,

    /// Proxy listen address (only used in proxy mode)
    #[arg(long, default_value = "127.0.0.1:9901")]
    listen: String,

    /// Proxy upstream address (only used in proxy mode)
    #[arg(long)]
    upstream: Option<String>,
}

fn main() -> Result<()> {
    let cli = Cli::parse();

    // Initialize logging
    let level = if cli.verbose {
        Level::DEBUG
    } else {
        Level::INFO
    };
    let subscriber = FmtSubscriber::builder()
        .with_max_level(level)
        .with_target(false)
        .finish();
    tracing::subscriber::set_global_default(subscriber).context("Failed to set up logging")?;

    match cli.command {
        Commands::Init(args) => cmd_init(args),
        Commands::Generate(args) => cmd_generate(args),
        Commands::Check(args) => cmd_check(args),
        Commands::Lock(args) => cmd_lock(args),
        Commands::Watch(args) => cmd_watch(args),
        Commands::Sniff(args) => cmd_sniff(args),
    }
}

fn cmd_init(args: InitArgs) -> Result<()> {
    let path = args.path.unwrap_or_else(|| PathBuf::from("."));
    info!(
        "Initializing motto project in {:?} with template '{}'",
        path, args.template
    );

    // Create directory structure
    std::fs::create_dir_all(path.join("src"))?;
    std::fs::create_dir_all(path.join("generated"))?;

    // Create example schema.rs
    let schema_content = include_str!("../templates/example_schema.rs");
    std::fs::write(path.join("src/schema.rs"), schema_content)?;

    // Create initial motto.lock
    let lock_content = motto::ir::lock::MottoLock::new().to_string()?;
    std::fs::write(path.join("motto.lock"), lock_content)?;

    info!("✓ Created motto project structure");
    info!("  - src/schema.rs: Your schema definitions");
    info!("  - motto.lock: Version tracking file");
    info!("  - generated/: Output directory for SDK code");
    info!("\nRun 'motto generate' to create your first SDK");

    Ok(())
}

fn cmd_generate(args: GenerateArgs) -> Result<()> {
    info!("Generating SDKs from {:?}", args.schema);

    // Parse transport mode
    let transport_mode: motto::emitters::TransportMode = args
        .transport
        .parse()
        .map_err(|e: String| anyhow::anyhow!(e))?;
    info!("Transport mode: {}", transport_mode);

    // Parse schema (use parse_file to get proper schema name from file)
    let parser = SchemaParser::new();
    let schema = parser.parse_file(&args.schema)?;

    // Generate fingerprint
    let fingerprint = SchemaFingerprint::compute(&schema);
    info!("Schema fingerprint: {}", fingerprint.short());

    // Generate IR
    let ir_gen = IrGenerator::new();
    let manifest = ir_gen.generate(&schema)?;

    // Parse targets
    let targets: Vec<&str> = if args.targets == "all" {
        #[allow(clippy::vec_init_then_push)]
        let t = {
            let mut t = Vec::new();
            #[cfg(feature = "emitter-typescript")]
            t.push("typescript");
            #[cfg(feature = "emitter-swift")]
            t.push("swift");
            #[cfg(feature = "emitter-kotlin")]
            t.push("kotlin");
            #[cfg(feature = "emitter-unity")]
            t.push("unity");
            t.push("rust");
            t
        };
        t
    } else {
        args.targets.split(',').map(|s| s.trim()).collect()
    };

    // Create output directory
    std::fs::create_dir_all(&args.output)?;

    // Generate for each target
    for target in targets {
        info!("Generating {} SDK...", target);

        let config = motto::emitters::EmitterConfig {
            output_dir: args.output.clone(),
            wasm_bindings: args.wasm,
            native_bindings: args.native,
            transport_mode,
            manifest: manifest.clone(),
        };

        match target {
            #[cfg(feature = "emitter-typescript")]
            "typescript" => motto::emitters::typescript::emit(&config)?,
            #[cfg(feature = "emitter-swift")]
            "swift" => motto::emitters::swift::emit(&config)?,
            #[cfg(feature = "emitter-kotlin")]
            "kotlin" => motto::emitters::kotlin::emit(&config)?,
            #[cfg(feature = "emitter-unity")]
            "unity" => motto::emitters::unity::emit(&config)?,
            "rust" => motto::emitters::rust::emit(&config)?,
            _ => anyhow::bail!(
                "Unknown target: {}. Make sure the corresponding emitter feature is enabled.",
                target
            ),
        }
    }

    info!("✓ SDK generation complete!");
    info!("  Output: {:?}", args.output);

    Ok(())
}

fn cmd_check(args: CheckArgs) -> Result<()> {
    info!("Checking schema compatibility...");

    // Parse current schema
    let parser = SchemaParser::new();
    let schema = parser.parse_file(&args.schema)?;
    let fingerprint = SchemaFingerprint::compute(&schema);

    // Load motto.lock
    let lock_content = std::fs::read_to_string(&args.lock).context("Failed to read motto.lock")?;
    let lock = motto::ir::lock::MottoLock::parse_str(&lock_content)?;

    // Compare fingerprints
    if fingerprint.hash() == lock.fingerprint() {
        info!(
            "✓ Schema matches motto.lock (fingerprint: {})",
            fingerprint.short()
        );
        Ok(())
    } else {
        let msg = format!(
            "Schema fingerprint mismatch!\n  Current: {}\n  Locked:  {}\n\nRun 'motto lock' to update",
            fingerprint.short(),
            &lock.fingerprint()[..16]
        );
        if args.strict {
            anyhow::bail!(msg);
        } else {
            eprintln!("{}", msg);
            Ok(())
        }
    }
}

fn cmd_sniff(args: SniffArgs) -> Result<()> {
    use motto::runtime::sniff::{OutputFormat, SniffConfig};

    info!("Starting motto sniff in '{}' mode...", args.mode);
    info!("Output format: {}", args.format);

    // Parse output format
    let format: OutputFormat = args
        .format
        .parse()
        .map_err(|e: String| anyhow::anyhow!(e))?;

    // Load schema manifest for decoding (enabled by default)
    let decode = !args.no_decode;
    let manifest = if decode {
        let schema_path = &args.schema;
        if schema_path.exists() {
            info!("Loading schema from {:?} for decode mode", schema_path);
            let parser = SchemaParser::new();
            match parser.parse_file(schema_path) {
                Ok(schema) => {
                    let ir_gen = IrGenerator::new();
                    match ir_gen.generate(&schema) {
                        Ok(m) => Some(m),
                        Err(e) => {
                            eprintln!(
                                "motto sniff: failed to generate IR from {:?}: {} (decoding disabled)",
                                schema_path, e
                            );
                            None
                        }
                    }
                }
                Err(e) => {
                    eprintln!(
                        "motto sniff: failed to parse {:?}: {} (decoding disabled)",
                        schema_path, e
                    );
                    None
                }
            }
        } else {
            info!(
                "Schema file {:?} not found, running without decode",
                schema_path
            );
            None
        }
    } else {
        None
    };

    let config = SniffConfig {
        format,
        manifest,
        decode,
        redact: args.redact,
        max_bytes: args.max_bytes,
    };

    // Build a tokio runtime for the async sniff operations
    let rt = tokio::runtime::Runtime::new().context("Failed to create tokio runtime")?;

    match args.mode.as_str() {
        "tap" => {
            // Tap mode requires --upstream (the WS endpoint to connect to and listen on)
            let url = args.upstream.as_deref().ok_or_else(|| {
                anyhow::anyhow!(
                    "tap mode requires --upstream <ws://host:port> to specify the endpoint to listen on"
                )
            })?;
            info!("Tap mode: connecting to {} to listen for frames", url);
            rt.block_on(async {
                motto::runtime::sniff::run_tap(url, &config)
                    .await
                    .map_err(|e| anyhow::anyhow!("sniff tap error: {}", e))
            })
        }
        "proxy" => {
            let upstream = args.upstream.as_deref().ok_or_else(|| {
                anyhow::anyhow!("proxy mode requires --upstream <ws://host:port>")
            })?;
            info!(
                "Proxy mode: {} -> {} (relay with frame logging)",
                args.listen, upstream
            );
            rt.block_on(async {
                motto::runtime::sniff::run_proxy(&args.listen, upstream, &config)
                    .await
                    .map_err(|e| anyhow::anyhow!("sniff proxy error: {}", e))
            })
        }
        other => anyhow::bail!("Unknown sniff mode '{}': expected 'tap' or 'proxy'", other),
    }
}

fn cmd_lock(args: LockArgs) -> Result<()> {
    info!("Updating motto.lock...");

    // Parse schema
    let parser = SchemaParser::new();
    let schema = parser.parse_file(&args.schema)?;
    let fingerprint = SchemaFingerprint::compute(&schema);

    // Load or create motto.lock
    let mut lock = if args.lock.exists() {
        let content = std::fs::read_to_string(&args.lock)?;
        motto::ir::lock::MottoLock::parse_str(&content)?
    } else {
        motto::ir::lock::MottoLock::new()
    };

    // Bump version
    match args.bump.as_str() {
        "major" => lock.bump_major(),
        "minor" => lock.bump_minor(),
        "patch" => lock.bump_patch(),
        _ => anyhow::bail!("Invalid bump type: {}", args.bump),
    }

    // Update fingerprint
    lock.set_fingerprint(fingerprint.hash());

    // Write lock file
    std::fs::write(&args.lock, lock.to_string()?)?;

    info!("✓ Updated motto.lock");
    info!("  Version: {}", lock.version());
    info!("  Fingerprint: {}", fingerprint.short());

    Ok(())
}

fn cmd_watch(args: WatchArgs) -> Result<()> {
    info!("Watching {:?} for changes...", args.schema);
    info!("Press Ctrl+C to stop");

    use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
    use std::sync::mpsc::channel;

    let (tx, rx) = channel();

    let mut watcher = RecommendedWatcher::new(
        move |res| {
            if let Ok(event) = res {
                let _ = tx.send(event);
            }
        },
        Config::default(),
    )?;

    watcher.watch(&args.schema, RecursiveMode::NonRecursive)?;

    loop {
        match rx.recv() {
            Ok(_event) => {
                info!("Schema changed, regenerating...");
                let gen_args = GenerateArgs {
                    schema: args.schema.clone(),
                    output: args.output.clone(),
                    targets: args.targets.clone(),
                    force: true,
                    wasm: false,
                    native: false,
                    transport: "runtime".to_string(),
                };
                if let Err(e) = cmd_generate(gen_args) {
                    eprintln!("Generation error: {}", e);
                }
            }
            Err(e) => {
                eprintln!("Watch error: {}", e);
                break;
            }
        }
    }

    Ok(())
}