cargo-plushie 0.7.1

Cargo subcommand for building and downloading Plushie renderer binaries
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
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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Scaffolders for `cargo plushie new-widget` and `cargo plushie init`.
//!
//! `new-widget` produces a widget crate with the conventional
//! `[package.metadata.plushie.widget]` layout and an optional `impl`
//! feature that pulls in `plushie-widget-sdk` for the renderer-side
//! implementation. `init` produces a plushie app crate with the
//! `plushie::cli::run` easy-path main, an automation-script example,
//! and a sample `.plushie` script under `scripts/`.
//!
//! The scaffolders are intentionally template-based rather than
//! shelling out to `cargo new`: the generated files are tiny, and
//! keeping the file generation in-process sidesteps Cargo's
//! interactive prompts and VCS behaviour (init-git, ignored files,
//! etc.) that would otherwise need to be unwound.

use crate::{Error, Result};
use std::path::{Path, PathBuf};

/// Produce the snake_case form of a kebab-case identifier.
fn to_snake_case(name: &str) -> String {
    name.replace('-', "_")
}

/// Produce the PascalCase form of a kebab- or snake-case identifier.
fn to_pascal_case(name: &str) -> String {
    let mut out = String::with_capacity(name.len());
    let mut upper_next = true;
    for ch in name.chars() {
        if ch == '-' || ch == '_' {
            upper_next = true;
            continue;
        }
        if upper_next {
            out.extend(ch.to_uppercase());
            upper_next = false;
        } else {
            out.push(ch);
        }
    }
    out
}

/// Validate that `name` is a well-formed kebab-case identifier
/// suitable for both a Cargo crate name and a widget type name.
///
/// Rules (matching Cargo's own package-name rules, minus the
/// underscore-allowed form to keep the snake_case -> kebab-case
/// conversion deterministic):
///
/// - Non-empty.
/// - ASCII letters, digits, and `-` only.
/// - First character must be an ASCII letter.
/// - No consecutive `-`.
/// - Does not start or end with `-`.
fn validate_kebab_name(name: &str) -> Result<()> {
    if name.is_empty() {
        return Err(Error::Other(anyhow::anyhow!("name must not be empty")));
    }
    let first = name.as_bytes()[0];
    if !first.is_ascii_alphabetic() {
        return Err(Error::Other(anyhow::anyhow!(
            "name `{name}` must start with an ASCII letter"
        )));
    }
    let mut prev_dash = false;
    for (i, b) in name.bytes().enumerate() {
        match b {
            b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' => prev_dash = false,
            b'-' => {
                if prev_dash {
                    return Err(Error::Other(anyhow::anyhow!(
                        "name `{name}` contains consecutive `-`"
                    )));
                }
                if i + 1 == name.len() {
                    return Err(Error::Other(anyhow::anyhow!(
                        "name `{name}` must not end with `-`"
                    )));
                }
                prev_dash = true;
            }
            _ => {
                return Err(Error::Other(anyhow::anyhow!(
                    "name `{name}` contains invalid character `{}`",
                    b as char
                )));
            }
        }
    }
    Ok(())
}

/// Resolve an optional `PLUSHIE_RUST_SOURCE_PATH` override to an absolute
/// plushie-rust checkout. Returns `None` if the env var is unset or
/// the path does not exist.
fn source_path_override() -> Option<PathBuf> {
    let path = std::env::var_os("PLUSHIE_RUST_SOURCE_PATH")?;
    let buf = PathBuf::from(path);
    std::fs::canonicalize(&buf).ok()
}

/// Input for `cargo plushie new-widget`.
pub struct NewWidgetOpts<'a> {
    /// Kebab-case widget crate name (e.g. `my-gauge`).
    pub name: &'a str,
    /// Destination directory; defaults to `./native/<name>`.
    pub path: Option<&'a Path>,
    /// Reserved widget type names the scaffold must not conflict with.
    pub builtin_type_names: &'a [&'a str],
}

/// Input for `cargo plushie init`.
pub struct InitOpts<'a> {
    /// Kebab-case app crate name (e.g. `my-app`).
    pub name: &'a str,
    /// Destination directory; defaults to `./<name>`.
    pub path: Option<&'a Path>,
}

/// Outcome returned from [`scaffold_widget`]. The caller is expected
/// to print a summary to stdout using the paths it carries.
#[derive(Debug)]
pub struct ScaffoldResult {
    /// Absolute path to the new crate root.
    pub crate_root: PathBuf,
}

/// Create a new widget crate rooted at `opts.path` (or `./native/<name>`).
///
/// # Errors
///
/// - `name` must be kebab-case (ASCII letters, digits, and `-`; must
///   start with a letter; no consecutive or trailing `-`).
/// - `name` must not shadow a built-in widget type name.
/// - The destination must not already exist.
pub fn scaffold_widget(opts: &NewWidgetOpts<'_>) -> Result<ScaffoldResult> {
    validate_kebab_name(opts.name)?;
    let type_name = to_snake_case(opts.name);
    if opts.builtin_type_names.contains(&type_name.as_str()) {
        return Err(Error::BuiltinCollision {
            crate_name: opts.name.to_string(),
            type_name,
        });
    }

    let default_path = PathBuf::from("native").join(opts.name);
    let target = opts.path.map(Path::to_path_buf).unwrap_or(default_path);
    if target.exists() {
        return Err(Error::Other(anyhow::anyhow!(
            "destination `{}` already exists; pick another path",
            target.display()
        )));
    }

    std::fs::create_dir_all(target.join("src"))?;

    let struct_name = to_pascal_case(opts.name);
    let module_name = to_snake_case(opts.name);
    let factory_name = format!("{struct_name}Factory");
    let cargo_toml = render_widget_cargo_toml(opts.name, &type_name, &module_name, &factory_name);
    let lib_rs = render_widget_lib_rs(&type_name, &struct_name, &factory_name);

    std::fs::write(target.join("Cargo.toml"), cargo_toml)?;
    std::fs::write(target.join("src").join("lib.rs"), lib_rs)?;
    maybe_write_iced_paths_override(&target)?;

    let crate_root = std::fs::canonicalize(&target).unwrap_or(target);
    Ok(ScaffoldResult { crate_root })
}

/// When `PLUSHIE_RUST_SOURCE_PATH` is set and a sibling `plushie-iced`
/// checkout exists, scaffold a `.cargo/config.toml` that forwards the
/// `paths = [".../plushie-iced"]` override so the scaffolded crate
/// can compile against the fork the same way the source workspace
/// does. Silently skipped when either piece is missing; apps that
/// publish against crates.io pick up the registry version.
fn maybe_write_iced_paths_override(target: &Path) -> Result<()> {
    let Some(source) = source_path_override() else {
        return Ok(());
    };
    let iced = source
        .parent()
        .map(|p| p.join("plushie-iced"))
        .filter(|p| p.is_dir());
    let Some(iced) = iced else {
        return Ok(());
    };
    let cargo_dir = target.join(".cargo");
    std::fs::create_dir_all(&cargo_dir)?;
    let body = format!(
        "# Forwards the PLUSHIE_RUST_SOURCE_PATH workspace's plushie-iced\n\
         # override so the scaffold compiles against the fork locally.\n\
         # Delete this file before publishing to crates.io.\n\
         paths = [{path:?}]\n",
        path = iced.display().to_string(),
    );
    std::fs::write(cargo_dir.join("config.toml"), body)?;
    Ok(())
}

/// Render the widget crate's `Cargo.toml`.
///
/// When `PLUSHIE_RUST_SOURCE_PATH` is set the scaffold emits path
/// dependencies pointing at the local checkout so the generated
/// crate compiles against workspace crates that haven't been
/// published yet. Without the env var, the scaffold targets
/// crates.io.
///
/// The `constructor` field names the factory type (not the builder)
/// so the custom renderer can register it with a zero-arg call.
fn render_widget_cargo_toml(
    name: &str,
    type_name: &str,
    module_name: &str,
    factory_name: &str,
) -> String {
    let mut out = String::new();
    out.push_str("[package]\n");
    out.push_str(&format!("name = \"{name}\"\n"));
    out.push_str("version = \"0.1.0\"\n");
    out.push_str("edition = \"2024\"\n\n");
    out.push_str("[package.metadata.plushie.widget]\n");
    out.push_str(&format!("type_name = \"{type_name}\"\n"));
    out.push_str(&format!(
        "constructor = \"{module_name}::factory::{factory_name}::new()\"\n\n"
    ));
    out.push_str("[features]\n");
    out.push_str("impl = [\"dep:plushie-widget-sdk\"]\n\n");
    out.push_str("[dependencies]\n");
    if let Some(source) = source_path_override() {
        let core = source.join("crates/plushie-core");
        let macros = source.join("crates/plushie-core-macros");
        let sdk = source.join("crates/plushie-widget-sdk");
        out.push_str(&format!(
            "plushie-core = {{ path = {:?} }}\n",
            core.display().to_string()
        ));
        out.push_str(&format!(
            "plushie-core-macros = {{ path = {:?} }}\n",
            macros.display().to_string()
        ));
        out.push_str(&format!(
            "plushie-widget-sdk = {{ path = {:?}, optional = true }}\n",
            sdk.display().to_string()
        ));
    } else {
        out.push_str("plushie-core = \"0.6\"\n");
        out.push_str("plushie-core-macros = \"0.6\"\n");
        out.push_str("plushie-widget-sdk = { version = \"0.6\", optional = true }\n");
    }
    out
}

/// Render the widget crate's `src/lib.rs`.
///
/// The stub (default features) is iced-free: the `widget!` builder
/// emits a `TreeNode` and the metadata constant the build tool reads.
/// Enabling the `impl` feature pulls in `plushie-widget-sdk` and
/// exposes a paired factory type that the custom renderer registers.
/// The factory's zero-arg `new()` is what `cargo plushie build`
/// injects into `PlushieAppBuilder::widget(...)`. The constructor
/// string lives in the crate's Cargo.toml as the single source of
/// truth; the `widget!` attribute carries only the wire type name.
///
/// The scaffolded `render` body does real work: it reads the typed
/// `value` / `max` props, computes a ratio, and renders a padded
/// container around a formatted label. Authors see the prop
/// extraction + iced composition pattern from the first run instead
/// of staring at a `todo!()`. A comment points at canvas drawing
/// for custom visuals.
fn render_widget_lib_rs(type_name: &str, struct_name: &str, factory_name: &str) -> String {
    format!(
        r#"//! {type_name} - a custom plushie widget.
//!
//! The stub (default features) is iced-free and can be used by any
//! plushie app in wire mode. The `impl` feature adds the iced-based
//! renderer implementation used by the custom renderer binary.

use plushie_core::widget;

widget! {{
    /// {struct_name} widget builder. Used in `App::view` to declare
    /// a {type_name} node in the view tree.
    #[widget(type_name = "{type_name}")]
    pub struct {struct_name} {{
        /// Current value; clamped to `[0.0, max]` at render time.
        pub value: f32,
        /// Full-scale value for the gauge.
        pub max: f32,
    }}
}}

#[cfg(feature = "impl")]
pub mod factory {{
    //! Renderer-side factory for the {type_name} widget.
    //!
    //! Compiled only under the `impl` feature so the stub crate
    //! stays iced-free. The custom renderer generated by
    //! `cargo plushie build` imports this module and calls
    //! [`{factory_name}::new`] to register the widget.
    //!
    //! For custom 2D drawing, reach for the canvas path via
    //! `plushie_widget_sdk::iced::widget::canvas` plus a
    //! `canvas::Program` impl. See `docs/custom-widgets.md` in the
    //! plushie-rust repository for a canvas-based walk-through.

    use plushie_widget_sdk::iced;
    use plushie_widget_sdk::prelude::*;

    /// Zero-sized factory marker.
    #[derive(PlushieWidget, ::core::default::Default)]
    #[plushie_widget(type_name = "{type_name}")]
    pub struct {factory_name};

    impl {factory_name} {{
        /// Fresh factory instance. Invoked by the custom renderer's
        /// generated `main.rs` at startup.
        #[must_use]
        pub const fn new() -> Self {{
            Self
        }}
    }}

    impl PlushieWidgetRender for {factory_name} {{
        fn render<'a>(
            &'a self,
            node: &'a TreeNode,
            _ctx: &RenderCtx<'a>,
        ) -> PlushieElement<'a> {{
            // Pull typed props the app set via the builder in
            // App::view. `prop_f32` handles JSON's loose number
            // shapes for us.
            let value = node.prop_f32("value").unwrap_or(0.0);
            let max = node.prop_f32("max").unwrap_or(1.0).max(0.0001);
            let ratio = (value / max).clamp(0.0, 1.0);

            // Built-in widgets from the prelude. Swap this for a
            // canvas widget when custom drawing is needed.
            let label = text(format!("{{:.0}}%", ratio * 100.0));
            container(label)
                .padding(iced::Padding::from(8))
                .into()
        }}
    }}
}}
"#
    )
}

/// Create a new plushie app crate rooted at `opts.path` (or `./<name>`).
///
/// The scaffolded crate's `main.rs` wires the `plushie::cli::run`
/// easy path, so the user gets `--plushie-script`,
/// `--plushie-replay`, `--plushie-inspect`, and mode selection for
/// free. A sample automation script and a thin example entry point
/// are scaffolded alongside.
///
/// # Errors
///
/// - `name` must be kebab-case (ASCII letters, digits, and `-`; must
///   start with a letter; no consecutive or trailing `-`).
/// - The destination must not already exist.
pub fn scaffold_app(opts: &InitOpts<'_>) -> Result<ScaffoldResult> {
    validate_kebab_name(opts.name)?;

    let default_path = PathBuf::from(opts.name);
    let target = opts.path.map(Path::to_path_buf).unwrap_or(default_path);
    if target.exists() {
        return Err(Error::Other(anyhow::anyhow!(
            "destination `{}` already exists; pick another path",
            target.display()
        )));
    }

    std::fs::create_dir_all(target.join("src"))?;
    std::fs::create_dir_all(target.join("examples"))?;
    std::fs::create_dir_all(target.join("scripts"))?;

    let struct_name = to_pascal_case(opts.name);
    let cargo_toml = render_app_cargo_toml(opts.name);
    let main_rs = render_app_main_rs(&struct_name);
    let script_example = render_script_example_rs(&struct_name);
    let sample_script = render_sample_script(&struct_name);

    std::fs::write(target.join("Cargo.toml"), cargo_toml)?;
    std::fs::write(target.join("src").join("main.rs"), main_rs)?;
    std::fs::write(
        target.join("examples").join("plushie_script.rs"),
        script_example,
    )?;
    std::fs::write(target.join("scripts").join("smoke.plushie"), sample_script)?;
    maybe_write_iced_paths_override(&target)?;

    let crate_root = std::fs::canonicalize(&target).unwrap_or(target);
    Ok(ScaffoldResult { crate_root })
}

/// Render the app crate's `Cargo.toml`.
fn render_app_cargo_toml(name: &str) -> String {
    let mut out = String::new();
    out.push_str("[package]\n");
    out.push_str(&format!("name = \"{name}\"\n"));
    out.push_str("version = \"0.1.0\"\n");
    out.push_str("edition = \"2024\"\n\n");
    out.push_str("[package.metadata.plushie]\n");
    out.push_str("# App marker; cargo-plushie uses the presence of this\n");
    out.push_str("# section to detect plushie apps in the workspace.\n");
    out.push_str("app = true\n\n");
    out.push_str("[dependencies]\n");
    if let Some(source) = source_path_override() {
        let plushie = source.join("crates/plushie");
        out.push_str(&format!(
            "plushie = {{ path = {:?} }}\n",
            plushie.display().to_string()
        ));
    } else {
        out.push_str("plushie = \"0.6\"\n");
    }
    out
}

/// Render the app crate's `src/main.rs`.
///
/// Uses the `plushie::cli::run` easy path so `--plushie-script`,
/// `--plushie-replay`, `--plushie-inspect`, and mode / socket
/// selection all work out of the box. The seeded app wires a
/// counter-style state transition so new users can run the binary,
/// click a button, and see the model update without writing any
/// code first.
fn render_app_main_rs(struct_name: &str) -> String {
    format!(
        r#"use plushie::prelude::*;

/// Application state. Replace these fields with whatever your app
/// actually needs to track.
#[derive(Default)]
pub struct {struct_name} {{
    count: i32,
}}

impl App for {struct_name} {{
    type Model = Self;

    fn init() -> (Self, Command) {{
        (Self::default(), Command::none())
    }}

    fn update(model: &mut Self, event: Event) -> Command {{
        match event.widget_match() {{
            Some(Click("inc")) => model.count += 1,
            Some(Click("dec")) => model.count -= 1,
            Some(Click("reset")) => model.count = 0,
            _ => {{}}
        }}
        Command::none()
    }}

    fn view(model: &Self, _widgets: &mut WidgetRegistrar) -> View {{
        window("main")
            .title("{struct_name}")
            .child(
                column()
                    .padding(16)
                    .spacing(8.0)
                    .child(text(&format!("Count: {{}}", model.count)).id("count"))
                    .child(
                        row()
                            .spacing(8.0)
                            .children([
                                button("inc", "+"),
                                button("dec", "-"),
                                button("reset", "reset"),
                            ]),
                    ),
            )
            .into()
    }}
}}

fn main() -> plushie::Result {{
    plushie::cli::run::<{struct_name}>()
}}

#[cfg(test)]
mod tests {{
    use super::*;
    use plushie::test::TestSession;

    #[test]
    fn starts_at_zero() {{
        let session = TestSession::<{struct_name}>::start();
        session.assert_text("count", "Count: 0");
    }}

    #[test]
    fn increment_then_reset() {{
        let mut session = TestSession::<{struct_name}>::start();
        session.click("inc");
        session.click("inc");
        assert_eq!(session.model().count, 2);
        session.click("reset");
        assert_eq!(session.model().count, 0);
    }}
}}
"#
    )
}

/// Render the scaffolded automation-script example.
///
/// Cargo examples can't reach the binary's private items, so the
/// example redeclares the app type. Keeping it self-contained means
/// `cargo run --example plushie_script -- --plushie-script ...`
/// works in a fresh checkout without any wiring.
fn render_script_example_rs(struct_name: &str) -> String {
    format!(
        r#"//! Entry point that exposes the app to `--plushie-script`,
//! `--plushie-replay`, and `--plushie-inspect` without invoking
//! the main binary. Useful when iterating on automation stubs.
//!
//! Example usage:
//!
//! ```sh
//! cargo run --example plushie_script -- --plushie-script scripts/smoke.plushie
//! ```

use plushie::prelude::*;

#[derive(Default)]
struct {struct_name} {{
    count: i32,
}}

impl App for {struct_name} {{
    type Model = Self;

    fn init() -> (Self, Command) {{
        (Self::default(), Command::none())
    }}

    fn update(model: &mut Self, event: Event) -> Command {{
        match event.widget_match() {{
            Some(Click("inc")) => model.count += 1,
            Some(Click("dec")) => model.count -= 1,
            Some(Click("reset")) => model.count = 0,
            _ => {{}}
        }}
        Command::none()
    }}

    fn view(model: &Self, _widgets: &mut WidgetRegistrar) -> View {{
        window("main")
            .title("{struct_name} automation")
            .child(
                column()
                    .padding(16)
                    .spacing(8.0)
                    .child(text(&format!("Count: {{}}", model.count)).id("count"))
                    .child(
                        row()
                            .spacing(8.0)
                            .children([
                                button("inc", "+"),
                                button("dec", "-"),
                                button("reset", "reset"),
                            ]),
                    ),
            )
            .into()
    }}
}}

fn main() -> plushie::Result {{
    plushie::cli::run::<{struct_name}>()
}}
"#
    )
}

/// Render a sample `.plushie` automation script.
fn render_sample_script(struct_name: &str) -> String {
    format!(
        "# Sample plushie automation script for {struct_name}. Run with:\n\
         #\n\
         #     cargo run --example plushie_script -- --plushie-script scripts/smoke.plushie\n\
         #\n\
         # Actions are one per line. Blank lines and lines starting\n\
         # with `#` are ignored.\n\
         \n\
         assert_text count \"Count: 0\"\n\
         click inc\n\
         click inc\n\
         click inc\n\
         assert_text count \"Count: 3\"\n\
         click reset\n\
         assert_text count \"Count: 0\"\n"
    )
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn to_snake_case_replaces_dashes() {
        assert_eq!(to_snake_case("my-gauge"), "my_gauge");
        assert_eq!(to_snake_case("plain"), "plain");
    }

    #[test]
    fn to_pascal_case_capitalises_parts() {
        assert_eq!(to_pascal_case("my-gauge"), "MyGauge");
        assert_eq!(to_pascal_case("my_gauge"), "MyGauge");
        assert_eq!(to_pascal_case("gauge"), "Gauge");
        assert_eq!(to_pascal_case("my-colour-picker"), "MyColourPicker");
    }

    #[test]
    fn validate_kebab_name_accepts_standard_forms() {
        assert!(validate_kebab_name("gauge").is_ok());
        assert!(validate_kebab_name("my-gauge").is_ok());
        assert!(validate_kebab_name("gauge-v2").is_ok());
    }

    #[test]
    fn validate_kebab_name_rejects_invalid_forms() {
        assert!(validate_kebab_name("").is_err());
        assert!(validate_kebab_name("-leading").is_err());
        assert!(validate_kebab_name("trailing-").is_err());
        assert!(validate_kebab_name("double--dash").is_err());
        assert!(validate_kebab_name("1bad").is_err());
        assert!(validate_kebab_name("has_underscore").is_err());
        assert!(validate_kebab_name("has space").is_err());
    }

    #[test]
    fn scaffold_widget_refuses_builtin_shadow() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("button");
        let opts = NewWidgetOpts {
            name: "button",
            path: Some(&target),
            builtin_type_names: &["button", "text"],
        };
        let err = scaffold_widget(&opts).unwrap_err();
        assert!(matches!(err, Error::BuiltinCollision { .. }));
    }

    #[test]
    fn scaffold_widget_refuses_existing_destination() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("gauge");
        std::fs::create_dir_all(&target).unwrap();
        let opts = NewWidgetOpts {
            name: "gauge",
            path: Some(&target),
            builtin_type_names: &[],
        };
        assert!(scaffold_widget(&opts).is_err());
    }

    #[test]
    fn scaffold_app_writes_expected_files() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("my-app");
        let opts = InitOpts {
            name: "my-app",
            path: Some(&target),
        };
        let result = scaffold_app(&opts).unwrap();
        assert!(result.crate_root.join("Cargo.toml").is_file());
        assert!(result.crate_root.join("src/main.rs").is_file());
        assert!(
            result
                .crate_root
                .join("examples/plushie_script.rs")
                .is_file()
        );
        assert!(result.crate_root.join("scripts/smoke.plushie").is_file());
        let cargo = std::fs::read_to_string(result.crate_root.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("name = \"my-app\""));
        assert!(cargo.contains("[package.metadata.plushie]"));
        let main = std::fs::read_to_string(result.crate_root.join("src/main.rs")).unwrap();
        assert!(main.contains("impl App for MyApp"));
        assert!(main.contains("plushie::cli::run::<MyApp>"));
    }

    #[test]
    fn scaffold_widget_writes_expected_files() {
        let tmp = tempfile::tempdir().unwrap();
        let target = tmp.path().join("my-gauge");
        let opts = NewWidgetOpts {
            name: "my-gauge",
            path: Some(&target),
            builtin_type_names: &[],
        };
        let result = scaffold_widget(&opts).unwrap();
        assert!(result.crate_root.join("Cargo.toml").is_file());
        assert!(result.crate_root.join("src/lib.rs").is_file());
        let cargo = std::fs::read_to_string(result.crate_root.join("Cargo.toml")).unwrap();
        assert!(cargo.contains("name = \"my-gauge\""));
        assert!(cargo.contains("type_name = \"my_gauge\""));
        assert!(cargo.contains("my_gauge::factory::MyGaugeFactory::new()"));
        assert!(cargo.contains("impl = [\"dep:plushie-widget-sdk\"]"));
        let lib = std::fs::read_to_string(result.crate_root.join("src/lib.rs")).unwrap();
        assert!(lib.contains("pub struct MyGauge"));
        assert!(lib.contains("pub struct MyGaugeFactory"));
        assert!(lib.contains("impl PlushieWidgetRender for MyGaugeFactory"));
        assert!(lib.contains("#[cfg(feature = \"impl\")]"));
    }
}