rspyts-cli 3.0.3

Compiler and build orchestrator for rspyts
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
//! Cargo project resolution, bridge compilation, and package generation.
//!
//! A [`Project`] is an immutable snapshot of Cargo metadata and application
//! settings for one command invocation. Builds use a synthetic `cdylib` bridge
//! outside the source tree to link every selected package, discover the
//! contract on the native target, and compile the same exports for Wasm.

use std::ffi::{CStr, c_char};
use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command as ProcessCommand;

use anyhow::{Context, Result, bail};
use libloading::{Library, Symbol};
use rspyts::ir::{Manifest, Namespace};
use serde_json::Value;
use tempfile::TempDir;

use crate::cargo::{self, Metadata as CargoMetadata, Package as CargoPackage};
use crate::config::Config;
use crate::contract::{
    named_identities, namespace_refs, namespaces, tagged_variant_name, type_namespace, type_refs,
};
use crate::output::{
    check_generated_files, project_lock, reconcile_generated_files, source_fingerprint,
    write_generated_gitignores,
};
use crate::{python, typescript};

mod validation;

/// Cross-language validation and identifier helpers used by generators.
pub(crate) use validation::*;

const CONTRACT_SYMBOL: &str = "rspyts_discovery_v1_contract";
const FREE_SYMBOL: &str = "rspyts_discovery_v1_contract_free";

/// Fully resolved inputs for one CLI invocation.
///
/// The snapshot is constructed once from `rspyts.toml` and Cargo metadata.
/// Commands that perform a long-running build re-read the configuration before
/// publication so a concurrent edit cannot produce output from mixed inputs.
#[derive(Debug, Clone)]
pub(super) struct Project {
    /// Directory containing `rspyts.toml` and the primary Cargo package.
    pub(super) root: PathBuf,
    /// Cargo workspace root used for source fingerprinting and path remapping.
    pub(super) workspace_root: PathBuf,
    /// Cargo target directory shared by the generated bridge builds.
    target_directory: PathBuf,
    /// Ordered application packages linked into the bridge.
    linked_packages: Vec<LinkedPackage>,
    /// Exact `rspyts` dependency used by every linked package.
    rspyts_dependency: ResolvedDependency,
    /// Public application/package name.
    pub(super) package_name: String,
    /// Version inherited from the primary Cargo package.
    pub(super) package_version: String,
    /// Python distribution and import package name.
    pub(super) python_package: String,
    /// npm package name.
    pub(super) typescript_package: String,
    /// User-owned Python project root.
    python_source: PathBuf,
    /// User-owned TypeScript project root.
    typescript_source: PathBuf,
    /// Configuration snapshot used to construct this project.
    pub(super) config: Config,
}

/// Cargo package linked into the synthetic application bridge.
#[derive(Debug, Clone)]
struct LinkedPackage {
    name: String,
    manifest: PathBuf,
}

/// Reproducible dependency specification for the generated bridge manifest.
#[derive(Debug, Clone)]
enum ResolvedDependency {
    Path(PathBuf),
    CratesIo { version: String },
}

impl Project {
    /// Resolve configuration and Cargo metadata for the package at `path`.
    pub(super) fn read(path: &Path) -> Result<Self> {
        let config = Config::read(path)?;
        let requested_manifest = config.root().join("Cargo.toml");
        let requested_manifest = requested_manifest
            .canonicalize()
            .with_context(|| format!("cannot find Cargo.toml beside {}", config.path.display()))?;
        let metadata = cargo::metadata(&requested_manifest, false)?;
        let selected = select_application_packages(
            &metadata,
            &requested_manifest,
            &config.application.additional_packages,
        )?;
        let primary = selected
            .first()
            .copied()
            .context("an rspyts application must select a primary Cargo package")?;
        let rspyts_dependency = resolve_rspyts_dependency(&metadata, &selected)?;

        let primary_package_name = primary.name.clone();
        let package_name = config
            .application
            .name
            .clone()
            .unwrap_or_else(|| primary_package_name.clone());
        validate_application_name(&package_name)?;
        let package_version = primary.version.clone();
        let python_package = config
            .application
            .python_package
            .clone()
            .unwrap_or_else(|| package_name.replace('-', "_"));
        let typescript_package = config
            .application
            .typescript_package
            .clone()
            .unwrap_or_else(|| package_name.clone());
        validate_python_package(&python_package)?;
        validate_typescript_package(&typescript_package)?;

        let root = config.root().to_path_buf();
        let workspace_root = metadata.workspace_root.clone();
        let target_directory = metadata.target_directory.clone();
        let python_source = root.join("src-py");
        let typescript_source = root.join("src-ts");
        let linked_packages = selected
            .iter()
            .map(|package| LinkedPackage {
                name: package.name.clone(),
                manifest: package.manifest_path.clone(),
            })
            .collect();

        Ok(Self {
            root,
            workspace_root,
            target_directory,
            linked_packages,
            rspyts_dependency,
            package_name,
            package_version,
            python_package,
            typescript_package,
            python_source,
            typescript_source,
            config,
        })
    }

    /// Return the Python source project root.
    pub(super) fn python_source(&self) -> &Path {
        &self.python_source
    }

    /// Return the TypeScript source project root.
    pub(super) fn typescript_source(&self) -> &Path {
        &self.typescript_source
    }

    /// Return the authoritative configuration path.
    pub(super) fn config_path(&self) -> &Path {
        &self.config.path
    }
}

/// Machine-readable result emitted after a successful build.
#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub(super) struct BuildReport {
    status: &'static str,
    python_source: PathBuf,
    typescript_source: PathBuf,
    python_package: String,
    typescript_package: String,
}

/// Generate, validate, and transactionally publish both language packages.
pub(super) fn build(project: &Project) -> Result<BuildReport> {
    let _lock = project_lock(project)?;
    current_config(project)?;
    let (generated, manifest) = generate(project)?;
    python::validate_project(project, &manifest)?;
    typescript::validate_project(project)?;
    let config = current_config(project)?;
    reconcile_generated_files(
        generated.path(),
        &project.root,
        &source_fingerprint(&project.workspace_root, &config)?,
        &config,
    )?;
    Ok(BuildReport {
        status: "ok",
        python_source: project.python_source.clone(),
        typescript_source: project.typescript_source.clone(),
        python_package: project.python_package.clone(),
        typescript_package: project.typescript_package.clone(),
    })
}

/// Verify that every owned output matches the current Rust sources.
pub(super) fn check(project: &Project) -> Result<()> {
    let _lock = project_lock(project)?;
    current_config(project)?;
    let (generated, _) = generate(project)?;
    let config = current_config(project)?;
    check_generated_files(
        generated.path(),
        &project.root,
        &source_fingerprint(&project.workspace_root, &config)?,
        &config,
    )
}

/// Re-read configuration and reject application changes made mid-command.
fn current_config(project: &Project) -> Result<Config> {
    let config = Config::read(project.config_path())?;
    if config.application != project.config.application {
        bail!("[application] in rspyts.toml changed during the command; run it again");
    }
    Ok(config)
}

/// Build both targets and stage generated packages in a temporary directory.
fn generate(project: &Project) -> Result<(TempDir, Manifest)> {
    let temporary = tempfile::Builder::new()
        .prefix(".rspyts-")
        .tempdir_in(&project.root)?;
    let bridge = prepare_bridge(project)?;
    let native = compile(project, &bridge, CompileKind::Native)?;
    let manifest = read_contract(&native, &bridge.package_name)?;
    validate_contract(project, &manifest)?;
    let wasm = compile(project, &bridge, CompileKind::Wasm)?;

    python::emit(project, &manifest, &native, temporary.path())?;
    typescript::emit(project, &manifest, &wasm, temporary.path())?;
    if project.config.application.gitignore {
        write_generated_gitignores(temporary.path())?;
    }
    Ok((temporary, manifest))
}

// Bridge generation and compilation

/// Target-specific form of the generated application bridge.
#[derive(Clone, Copy)]
enum CompileKind {
    Native,
    Wasm,
}

/// Synthetic Cargo package that links all selected application crates.
struct Bridge {
    manifest: PathBuf,
    package_id: String,
    package_name: String,
}

/// Materialize the stable synthetic bridge package under Cargo's target tree.
fn prepare_bridge(project: &Project) -> Result<Bridge> {
    let key = stable_key(project.config_path().to_string_lossy().as_bytes());
    let root = project
        .target_directory
        .join("rspyts")
        .join(format!("{key:016x}"))
        .join("bridge");
    let source_root = root.join("src");
    fs::create_dir_all(&source_root)?;
    let package_name = format!("rspyts-bridge-{key:016x}");

    let mut manifest = format!(
        "[package]\nname = {}\nversion = \"0.0.0\"\nedition = \"2024\"\npublish = false\n\n[lib]\ncrate-type = [\"cdylib\"]\n\n[dependencies]\n",
        toml_string(&package_name)
    );
    writeln!(
        manifest,
        "rspyts = {}",
        bridge_dependency(&project.rspyts_dependency)
    )?;
    for (index, package) in project.linked_packages.iter().enumerate() {
        let root = package
            .manifest
            .parent()
            .context("linked Cargo manifest has no parent")?;
        writeln!(
            manifest,
            "rspyts_application_{index} = {{ package = {}, path = {} }}",
            toml_string(&package.name),
            toml_string(&root.to_string_lossy())
        )?;
    }
    manifest.push_str("\n[workspace]\n");

    let mut source = String::from("rspyts::application!(\n");
    for index in 0..project.linked_packages.len() {
        writeln!(source, "    rspyts_application_{index},")?;
    }
    source.push_str(");\n");
    write_if_changed(&root.join("Cargo.toml"), &manifest)?;
    write_if_changed(&source_root.join("lib.rs"), &source)?;

    let bridge_manifest = root.join("Cargo.toml");
    let metadata = cargo::metadata(&bridge_manifest, true)
        .context("failed to inspect the generated rspyts bridge")?;
    let package = metadata
        .packages
        .first()
        .context("generated rspyts bridge metadata has no package")?;
    Ok(Bridge {
        manifest: bridge_manifest,
        package_id: package.id.clone(),
        package_name,
    })
}

/// Compile one bridge target and return Cargo's reported `cdylib` artifact.
fn compile(project: &Project, bridge: &Bridge, kind: CompileKind) -> Result<PathBuf> {
    let (feature, label) = match kind {
        CompileKind::Native => (Some("rspyts/python-extension"), "Python"),
        CompileKind::Wasm => (None, "WebAssembly"),
    };
    let mut command = ProcessCommand::new(cargo::executable());
    command
        .arg("build")
        .arg("--manifest-path")
        .arg(&bridge.manifest)
        .arg("--release")
        .arg("--message-format=json-render-diagnostics");
    if let Some(feature) = feature {
        command.arg("--features").arg(feature);
    }
    if matches!(kind, CompileKind::Wasm) {
        command.args(["--target", "wasm32-unknown-unknown"]);
    }
    let mut flags = std::env::var("RUSTFLAGS").unwrap_or_default();
    append_rust_flag(
        &mut flags,
        &format!(
            "--remap-path-prefix={}=/workspace",
            project.workspace_root.display()
        ),
    );
    if let Some(cargo_home) = std::env::var_os("CARGO_HOME")
        .map(PathBuf::from)
        .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(".cargo")))
    {
        append_rust_flag(
            &mut flags,
            &format!("--remap-path-prefix={}=/cargo", cargo_home.display()),
        );
    }
    if matches!(kind, CompileKind::Native) && cfg!(target_os = "macos") {
        append_rust_flag(&mut flags, "-C");
        append_rust_flag(&mut flags, "link-arg=-undefined");
        append_rust_flag(&mut flags, "-C");
        append_rust_flag(&mut flags, "link-arg=dynamic_lookup");
        append_rust_flag(&mut flags, "-C");
        append_rust_flag(&mut flags, "link-arg=-Wl,-install_name,@rpath/native.so");
    }
    command
        .env("RUSTFLAGS", flags)
        .env("CARGO_TARGET_DIR", &project.target_directory)
        .env("RSPYTS_APPLICATION_NAME", &project.package_name)
        .env("RSPYTS_APPLICATION_VERSION", &project.package_version);
    let output = command
        .output()
        .with_context(|| format!("failed to compile {label}"))?;
    if !output.status.success() {
        bail!(
            "Cargo failed to compile the {label}\n{}{}",
            cargo_diagnostics(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
    }
    artifact_from_messages(&output.stdout, &bridge.package_id, kind).with_context(|| {
        format!(
            "Cargo did not report the {label} cdylib for `{}`",
            project.package_name
        )
    })
}

/// Append one rustc flag while preserving flags supplied by the caller.
fn append_rust_flag(flags: &mut String, value: &str) {
    if !flags.is_empty() {
        flags.push(' ');
    }
    flags.push_str(value);
}

/// Find the requested bridge artifact in Cargo's JSON message stream.
fn artifact_from_messages(bytes: &[u8], package_id: &str, kind: CompileKind) -> Option<PathBuf> {
    String::from_utf8_lossy(bytes)
        .lines()
        .filter_map(|line| serde_json::from_str::<Value>(line).ok())
        .find_map(|message| {
            if message["reason"] != "compiler-artifact" || message["package_id"] != package_id {
                return None;
            }
            let crate_types = message["target"]["crate_types"].as_array()?;
            if !crate_types.iter().any(|value| value == "cdylib") {
                return None;
            }
            message["filenames"]
                .as_array()?
                .iter()
                .filter_map(Value::as_str)
                .map(PathBuf::from)
                .find(|path| match kind {
                    CompileKind::Native => path
                        .extension()
                        .and_then(|value| value.to_str())
                        .is_some_and(|value| matches!(value, "dylib" | "so" | "dll")),
                    CompileKind::Wasm => path.extension().is_some_and(|value| value == "wasm"),
                })
        })
}

/// Extract rendered compiler diagnostics from Cargo's JSON message stream.
fn cargo_diagnostics(bytes: &[u8]) -> String {
    String::from_utf8_lossy(bytes)
        .lines()
        .filter_map(|line| serde_json::from_str::<Value>(line).ok())
        .filter_map(|message| message["message"]["rendered"].as_str().map(str::to_owned))
        .collect()
}

// Discovery ABI

/// Load the native bridge and deserialize its discovered application contract.
fn read_contract(library_path: &Path, package_name: &str) -> Result<Manifest> {
    type ContractFn = unsafe extern "C" fn() -> rspyts::__private::DiscoveryResult;
    type FreeFn = unsafe extern "C" fn(*mut c_char);

    // SAFETY: the library remains live while both symbols and the returned payload are used.
    let library = unsafe { Library::new(library_path) }
        .with_context(|| format!("failed to load {}", library_path.display()))?;
    let contract_name = format!("{CONTRACT_SYMBOL}__{package_name}\0");
    let free_name = format!("{FREE_SYMBOL}__{package_name}\0");
    // SAFETY: the application macro emits these exact ABI signatures.
    let contract: Symbol<'_, ContractFn> = unsafe { library.get(contract_name.as_bytes()) }
        .with_context(|| {
            format!(
                "generated rspyts bridge is missing `{}`",
                contract_name.trim_end_matches('\0')
            )
        })?;
    // SAFETY: the application macro emits these exact ABI signatures.
    let free: Symbol<'_, FreeFn> = unsafe { library.get(free_name.as_bytes()) }
        .with_context(|| format!("missing `{}`", free_name.trim_end_matches('\0')))?;
    // SAFETY: the loaded function follows the discovery ABI.
    let result = unsafe { contract() };
    if result.payload.is_null() {
        bail!("the generated rspyts bridge panicked during contract discovery");
    }
    // SAFETY: the discovery ABI returns a live NUL-terminated string.
    let payload = unsafe { CStr::from_ptr(result.payload) }
        .to_bytes()
        .to_vec();
    // SAFETY: the payload came from this library and is freed once.
    unsafe { free(result.payload) };
    match result.status {
        rspyts::__private::DISCOVERY_SUCCESS => Ok(serde_json::from_slice(&payload)?),
        rspyts::__private::DISCOVERY_ERROR => {
            bail!(
                "invalid rspyts application: {}",
                String::from_utf8_lossy(&payload)
            )
        }
        status => bail!("rspyts discovery returned unknown status {status}"),
    }
}

// Cargo dependency rendering

/// Select and validate the primary and configured application packages.
fn select_application_packages<'a>(
    metadata: &'a CargoMetadata,
    requested_manifest: &Path,
    additional_packages: &[String],
) -> Result<Vec<&'a CargoPackage>> {
    let primary = metadata
        .packages
        .iter()
        .find(|package| {
            package
                .manifest_path
                .canonicalize()
                .is_ok_and(|path| path == requested_manifest)
        })
        .context("rspyts.toml must be beside a Cargo package manifest")?;
    if !metadata.workspace_members.contains(&primary.id) {
        bail!("the Cargo package beside rspyts.toml must be a workspace member");
    }

    let mut selected = vec![primary];
    for name in additional_packages {
        if selected.iter().any(|package| &package.name == name) {
            bail!("rspyts application package `{name}` is listed more than once");
        }
        let mut matches = metadata.packages.iter().filter(|package| {
            &package.name == name && metadata.workspace_members.contains(&package.id)
        });
        let package = matches.next().with_context(|| {
            format!("additional rspyts package `{name}` is not a workspace member")
        })?;
        if matches.next().is_some() {
            bail!("additional rspyts package name `{name}` is ambiguous");
        }
        selected.push(package);
    }
    for package in &selected {
        if !has_library_target(package) {
            bail!(
                "rspyts application package `{}` must have a library target",
                package.name
            );
        }
    }
    Ok(selected)
}

/// Require every application package to use the same direct `rspyts` package.
fn resolve_rspyts_dependency(
    metadata: &CargoMetadata,
    selected: &[&CargoPackage],
) -> Result<ResolvedDependency> {
    let mut identities = selected
        .iter()
        .map(|package| direct_rspyts_id(metadata, &package.id));
    let identity = identities
        .next()
        .context("an rspyts application must select at least one Cargo package")??;
    for candidate in identities {
        if candidate? != identity {
            bail!("all rspyts application packages must resolve the same `rspyts` package");
        }
    }
    let package = metadata
        .packages
        .iter()
        .find(|package| package.id == identity)
        .context("Cargo metadata omitted the resolved rspyts package")?;
    resolved_dependency(package)
}

/// Return whether a Cargo package exposes a linkable library target.
fn has_library_target(package: &CargoPackage) -> bool {
    package.targets.iter().any(|target| {
        target.kind.iter().any(|kind| kind == "lib")
            || target
                .crate_types
                .iter()
                .any(|kind| matches!(kind.as_str(), "lib" | "rlib" | "cdylib"))
    })
}

/// Resolve the package ID of a package's direct normal `rspyts` dependency.
fn direct_rspyts_id(metadata: &CargoMetadata, package_id: &str) -> Result<String> {
    let nodes = &metadata
        .resolve
        .as_ref()
        .context("Cargo metadata has no dependency graph")?;
    let node = nodes
        .nodes
        .iter()
        .find(|node| node.id == package_id)
        .context("Cargo metadata omitted an application package from its dependency graph")?;
    let mut matches = node
        .deps
        .iter()
        .filter(|dependency| dependency.dep_kinds.iter().any(|kind| kind.kind.is_none()))
        .filter(|dependency_id| {
            metadata
                .packages
                .iter()
                .any(|package| package.id == dependency_id.pkg && package.name == "rspyts")
        })
        .map(|dependency| dependency.pkg.as_str());
    let dependency = matches
        .next()
        .context("each rspyts application package must directly depend on `rspyts`")?;
    if matches.next().is_some() {
        bail!("an rspyts application package resolves multiple direct `rspyts` dependencies");
    }
    Ok(dependency.to_owned())
}

/// Convert Cargo's resolved package into an exact bridge dependency.
fn resolved_dependency(package: &CargoPackage) -> Result<ResolvedDependency> {
    if let Some(source) = package.source.as_deref() {
        if matches!(
            source,
            "registry+https://github.com/rust-lang/crates.io-index"
                | "registry+sparse+https://index.crates.io/"
        ) {
            return Ok(ResolvedDependency::CratesIo {
                version: package.version.clone(),
            });
        }
        bail!("the selected rspyts package uses unsupported Cargo source `{source}`");
    }
    let root = package
        .manifest_path
        .parent()
        .context("resolved rspyts manifest has no parent")?
        .canonicalize()
        .context("cannot resolve the selected rspyts package source")?;
    Ok(ResolvedDependency::Path(root))
}

/// Render an exact Cargo dependency specification for the synthetic bridge.
fn bridge_dependency(dependency: &ResolvedDependency) -> String {
    match dependency {
        ResolvedDependency::Path(root) => {
            format!("{{ path = {} }}", toml_string(&root.to_string_lossy()))
        }
        ResolvedDependency::CratesIo { version } => {
            format!("{{ version = {} }}", toml_string(&format!("={version}")))
        }
    }
}

/// Compute a stable FNV-1a key for target-directory isolation.
fn stable_key(bytes: &[u8]) -> u64 {
    let mut hash = 0xcbf2_9ce4_8422_2325_u64;
    for byte in bytes {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(0x0000_0100_0000_01b3);
    }
    hash
}

/// Quote a TOML string through serde's compatible JSON string encoder.
fn toml_string(value: &str) -> String {
    serde_json::to_string(value).expect("serializing a string cannot fail")
}

/// Replace a generated bridge file only when its bytes changed.
fn write_if_changed(path: &Path, source: &str) -> Result<()> {
    if fs::read(path).is_ok_and(|current| current == source.as_bytes()) {
        return Ok(());
    }
    if let Some(parent) = path.parent() {
        fs::create_dir_all(parent)?;
    }
    fs::write(path, source).with_context(|| format!("failed to write {}", path.display()))
}

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

    #[test]
    fn crates_io_dependency_keeps_its_cargo_identity() {
        let package: CargoPackage = serde_json::from_value(serde_json::json!({
            "id": "rspyts 3.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
            "name": "rspyts",
            "manifest_path": "/cargo/registry/rspyts-3.0.1/Cargo.toml",
            "source": "registry+https://github.com/rust-lang/crates.io-index",
            "version": "3.0.1",
            "targets": [],
        }))
        .expect("deserialize package metadata");

        let dependency = resolved_dependency(&package).expect("resolve crates.io dependency");

        assert_eq!(bridge_dependency(&dependency), "{ version = \"=3.0.1\" }");
    }

    #[test]
    fn path_dependency_keeps_its_canonical_path() {
        let manifest = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("../rspyts/Cargo.toml")
            .canonicalize()
            .expect("canonical rspyts manifest");
        let package: CargoPackage = serde_json::from_value(serde_json::json!({
            "id": "path+file:///workspace/crates/rspyts#3.0.1",
            "name": "rspyts",
            "manifest_path": manifest,
            "source": null,
            "version": "3.0.1",
            "targets": [],
        }))
        .expect("deserialize package metadata");

        let dependency = resolved_dependency(&package).expect("resolve path dependency");
        let rendered = bridge_dependency(&dependency);

        assert!(rendered.starts_with("{ path = "));
        assert!(rendered.contains("/crates/rspyts"));
    }
}