cargo-slide 0.1.3

Code-driven presentation engine combining Typst typesetting with a native Rust viewer, developed as part of the Apich workspace backend.
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
use slide_core::compiler::SlideCompiler;
use std::fs::create_dir_all;
use std::fs::write;
use std::io::BufRead;
use std::io::BufReader;
use std::path::Path;
use std::path::PathBuf;
use std::process::Command;
use std::process::Stdio;
use tempfile::tempdir;

/// Compile the presentation into a standalone release binary. `target`, when given, cross-compiles
/// for that Rust target triple (e.g. `x86_64-pc-windows-msvc`) instead of the host's own platform;
/// the triple must already be installed via `rustup target add` and have a working linker
/// configured for a non-host target.
#[allow(clippy::too_many_lines)]
pub fn execute(
    file: &Path,
    output: Option<PathBuf>,
    animation: &str,
    target: Option<&str>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    // The *target* platform decides the binary's extension, not the host `cargo-slide` itself
    // runs on (`cfg!(windows)` reflects the host, which is always false when cross-compiling
    // from this Linux sandbox to a Windows target) -- a real bug in the original upstream
    // implementation, fixed here as part of adding cross-compilation support at all.
    let targets_windows = target.is_some_and(|t| t.contains("windows"));
    if !file.exists() {
        return Err(format!("File does not exist: {}", file.display()).into());
    }

    slide_core::logger::log_event(
        "info",
        &format!(
            "📦 Packaging presentation into single binary: {}",
            file.display()
        ),
        Some(serde_json::json!({
            "stage": "package_start",
            "source_file": file.display().to_string(),
            "percent": 1,
        })),
    );
    let compiler = SlideCompiler::new()?;
    let deck = compiler.compile_file(file)?;

    let stem = file
        .file_stem()
        .and_then(|s| s.to_str())
        .unwrap_or("presentation");
    let default_name = if targets_windows {
        format!("{stem}-presentation.exe")
    } else {
        format!("{stem}-presentation")
    };
    let target_binary = output.unwrap_or_else(|| PathBuf::from(default_name));

    slide_core::logger::log_event(
        "info",
        &format!(
            "⏳ Generating self-contained Rust bundle with {} slides...",
            deck.total_slides()
        ),
        Some(serde_json::json!({
            "stage": "bundle_generate",
            "total_slides": deck.total_slides(),
            "percent": 3,
        })),
    );

    // Discover repository root for workspace dependencies
    let current_exe = std::env::current_exe().unwrap_or_default();
    let repo_root = find_repo_root(&current_exe)
        .unwrap_or_else(|| std::env::current_dir().unwrap_or_else(|_| PathBuf::from(".")));

    // Create temporary build project in target/.build_tmp if possible to avoid tmpfs size exhaustion
    let build_dir_parent = if repo_root.join("target").exists() {
        let p = repo_root.join("target/.build_tmp");
        let _ = create_dir_all(&p);
        p
    } else {
        std::env::temp_dir()
    };
    let build_dir = tempfile::Builder::new()
        .prefix("slide_build_")
        .tempdir_in(&build_dir_parent)
        .or_else(|_| tempdir())?;
    let build_path = build_dir.path();
    let src_dir = build_path.join("src");
    create_dir_all(&src_dir)?;

    // Serialize deck to JSON to be embedded via include_str!
    let deck_json = serde_json::to_string(&deck)?;
    write(build_path.join("deck.json"), deck_json)?;

    let core_path = repo_root.join("crates/slide-core");
    let player_path = repo_root.join("crates/slide-player");

    // Format paths with forward slashes for cross-platform TOML compatibility (avoids backslash escaping on Windows)
    let core_str = core_path.to_string_lossy().replace('\\', "/");
    let player_str = player_path.to_string_lossy().replace('\\', "/");

    let pkg_version = env!("CARGO_PKG_VERSION");
    let (core_dep, player_dep) = if core_path.exists() && player_path.exists() {
        (
            format!(r#"slide-core = {{ path = "{core_str}", version = "{pkg_version}" }}"#),
            format!(r#"slide-player = {{ path = "{player_str}", version = "{pkg_version}" }}"#),
        )
    } else {
        (
            format!(r#"slide-core = "{pkg_version}""#),
            format!(r#"slide-player = "{pkg_version}""#),
        )
    };

    let cargo_toml = format!(
        r#"[package]
name = "slide-standalone-runner"
version = "{pkg_version}"
edition = "2024"

[workspace]

[dependencies]
{core_dep}
{player_dep}
serde_json = "1.0"
"#
    );
    write(build_path.join("Cargo.toml"), cargo_toml)?;

    let runner_main = format!(
        r#"use slide_player::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {{
    let deck_json = include_str!("../deck.json");
    let deck: SlideDeck = serde_json::from_str(deck_json)?;

    SlideApp::from_deck(deck)
        .default_animation("{animation}")
        .run()?;

    Ok(())
}}
"#
    );
    write(src_dir.join("main.rs"), runner_main)?;

    slide_core::logger::log_event(
        "info",
        "🔨 Compiling release binary with Cargo...",
        Some(serde_json::json!({
            "stage": "cargo_build_release",
            "percent": 5,
        })),
    );
    let target_dir = repo_root.join("target/standalone_target");
    run_cargo_build_with_progress(build_path, &target_dir, target)?;

    let bin_filename = if targets_windows {
        "slide-standalone-runner.exe"
    } else {
        "slide-standalone-runner"
    };
    // `cargo build --target <triple>` (even for the host's own triple) nests output under
    // `<target-dir>/<triple>/release/`, not `<target-dir>/release/` -- only the no-`--target`
    // invocation uses the flat layout.
    let release_dir = target.map_or_else(
        || target_dir.join("release"),
        |triple| target_dir.join(triple).join("release"),
    );
    let built_bin = release_dir.join(bin_filename);
    if !built_bin.exists() {
        return Err(format!("Compiled binary not found at {}", built_bin.display()).into());
    }

    // Copy to target location
    std::fs::copy(&built_bin, &target_binary)?;

    // Ensure executable permissions on Unix
    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        if let Ok(metadata) = std::fs::metadata(&target_binary) {
            let mut perms = metadata.permissions();
            perms.set_mode(0o755);
            let _ = std::fs::set_permissions(&target_binary, perms);
        }
    }

    let size_bytes = std::fs::metadata(&target_binary).map_or(0, |m| m.len());
    #[allow(clippy::cast_precision_loss)]
    let size_mb = size_bytes as f64 / (1024.0 * 1024.0);

    slide_core::logger::log_event(
        "success",
        &format!(
            "✅ Single binary generated successfully!\n\
             📍 Output: {} ({:.2} MB)\n\
             💡 You can now distribute and run this standalone presentation on any machine!",
            target_binary.display(),
            size_mb
        ),
        Some(serde_json::json!({
            "stage": "package_complete",
            "status": "success",
            "output": target_binary.display().to_string(),
            "size_mb": size_mb,
            "total_slides": deck.total_slides(),
            "percent": 100,
        })),
    );

    Ok(())
}

fn find_repo_root(exe: &Path) -> Option<PathBuf> {
    // 1. Explicit environment variable override
    if let Ok(env_root) = std::env::var("CARGO_SLIDE_REPO_ROOT") {
        let p = PathBuf::from(env_root);
        if p.join("crates").join("slide-core").exists() {
            return Some(p);
        }
    }

    // 2. Search upwards from current working directory
    if let Ok(cwd) = std::env::current_dir() {
        let mut cur = Some(cwd.as_path());
        while let Some(dir) = cur {
            if dir.join("crates").join("slide-core").exists() {
                return Some(dir.to_path_buf());
            }
            cur = dir.parent();
        }
    }

    // 3. Search upwards from compile-time manifest dir (for local dev installs)
    let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
    let mut cur = Some(manifest_dir);
    while let Some(dir) = cur {
        if dir.join("crates").join("slide-core").exists() {
            return Some(dir.to_path_buf());
        }
        cur = dir.parent();
    }

    // 4. Search upwards from executable location
    let mut cur = exe.parent();
    while let Some(dir) = cur {
        if dir.join("crates").join("slide-core").exists() {
            return Some(dir.to_path_buf());
        }
        cur = dir.parent();
    }

    None
}

/// Resolved package-graph size for `build_path`'s generated standalone-runner project, used as a
/// rough denominator for build progress (see `run_cargo_build_with_progress`) -- not an exact
/// rustc-invocation count (a build-script or proc-macro crate can compile more than once), just
/// close enough that a caller polling `--log-format json`'s progress events sees a percentage
/// that moves at roughly the right pace instead of standing still for minutes then jumping to
/// 100%. Returns 0 (caller then treats every artifact as 1% until 100 are seen) if `cargo
/// metadata` itself fails for any reason, rather than aborting the whole build over a
/// progress-estimate step that was never essential to begin with.
fn estimate_total_units(
    build_path: &Path,
    target: Option<&str>,
) -> usize {
    let mut cmd = Command::new("cargo");
    cmd.arg("metadata")
        .arg("--format-version")
        .arg("1")
        .current_dir(build_path);
    if let Some(triple) = target {
        cmd.arg("--filter-platform").arg(triple);
    }
    let Ok(output) = cmd.output() else {
        return 0;
    };
    if !output.status.success() {
        return 0;
    }
    let Ok(json) = serde_json::from_slice::<serde_json::Value>(&output.stdout) else {
        return 0;
    };
    json.get("resolve")
        .and_then(|r| r.get("nodes"))
        .and_then(serde_json::Value::as_array)
        .map_or(0, Vec::len)
}

/// Runs the actual `cargo build --release` for the generated standalone-runner project, emitting
/// a real (not simulated) progress percentage as compilation proceeds -- each unit of work cargo
/// itself reports finishing (`--message-format=json`'s `"reason":"compiler-artifact"` events)
/// advances the count, scaled against `estimate_total_units`'s rough total. Reserves 0-5% for
/// work already done before this function is called (Typst compilation, bundle generation) and
/// 95-100% for copying the finished binary out afterward, so a caller watching the percentage
/// climb sees it start above 0 and top out just under 100 while this function itself is running.
///
/// Real compiler diagnostics (errors and warnings) are forwarded too, via `"reason":
/// "compiler-message"` events -- `--message-format=json` alone would otherwise swallow them
/// entirely compared to the plain (non-JSON) invocation this replaces, since cargo routes
/// per-file diagnostics through the JSON message stream rather than stderr in that mode.
fn run_cargo_build_with_progress(
    build_path: &Path,
    target_dir: &Path,
    target: Option<&str>,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
    let total = estimate_total_units(build_path, target);

    let mut cargo_cmd = Command::new("cargo");
    cargo_cmd
        .arg("build")
        .arg("--release")
        .arg("--target-dir")
        .arg(target_dir)
        .arg("--message-format=json")
        .current_dir(build_path)
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit());
    if let Some(triple) = target {
        cargo_cmd.arg("--target").arg(triple);
    }

    let mut child = cargo_cmd.spawn()?;
    let Some(stdout) = child.stdout.take() else {
        return Err("Failed to capture cargo build output".into());
    };
    let reader = BufReader::new(stdout);

    let mut compiled: usize = 0;
    let mut last_reported_percent: u8 = 0;
    for line in reader.lines() {
        let Ok(line) = line else { continue };
        let Ok(value) = serde_json::from_str::<serde_json::Value>(&line) else {
            continue;
        };
        let Some(reason) = value.get("reason").and_then(|r| r.as_str()) else {
            continue;
        };
        match reason {
            | "compiler-artifact" => {
                compiled = compiled.saturating_add(1);
                let denom = if total == 0 {
                    compiled.max(100)
                } else {
                    total
                };
                #[allow(clippy::cast_precision_loss)]
                let fraction = (compiled as f64 / denom as f64).min(1.0);
                #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
                let percent = (5.0 + fraction * 90.0) as u8;
                if percent != last_reported_percent {
                    last_reported_percent = percent;
                    let pkg_name = value
                        .get("target")
                        .and_then(|t| t.get("name"))
                        .and_then(|n| n.as_str())
                        .unwrap_or("dependency");
                    slide_core::logger::log_event(
                        "info",
                        &format!(
                            "🔨 Compiling {pkg_name} ({compiled}/{}, cross-target: {})",
                            if total == 0 {
                                "?".to_string()
                            } else {
                                total.to_string()
                            },
                            target.unwrap_or("host")
                        ),
                        Some(serde_json::json!({
                            "stage": "cargo_build_release",
                            "compiled": compiled,
                            "total": total,
                            "percent": percent,
                        })),
                    );
                }
            },
            | "compiler-message" => {
                let Some(message) = value.get("message") else {
                    continue;
                };
                let level = message.get("level").and_then(|l| l.as_str()).unwrap_or("");
                let Some(rendered) = message.get("rendered").and_then(|r| r.as_str()) else {
                    continue;
                };
                if level == "error" {
                    slide_core::logger::log_event("error", rendered, None);
                }
            },
            | _ => {},
        }
    }

    let status = child.wait()?;
    if !status.success() {
        return Err("Cargo compilation failed for single binary output".into());
    }
    Ok(())
}