rs-web 0.4.2

A fast, Lua-driven static site generator with encryption support
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
//! Parallel processing module (rs.parallel) - rayon-backed parallel operations

use super::helpers::{is_path_within_root, parse_frontmatter_content, resolve_path};
use super::portable::LuaPortable;
use crate::tracker::SharedTracker;
use image::DynamicImage;
use mlua::{Function, Lua, LuaSerdeExt, Result, Table, Value};
use rayon::prelude::*;
use std::cell::RefCell;
use std::path::Path;

// Thread-local Lua VM for parallel execution
// Each rayon thread gets its own Lua state
thread_local! {
    static WORKER_LUA: RefCell<Option<Lua>> = const { RefCell::new(None) };
}

/// Get or initialize the thread-local Lua VM
fn get_or_init_worker_lua() -> &'static Lua {
    WORKER_LUA.with(|cell| {
        let mut borrow = cell.borrow_mut();
        if borrow.is_none() {
            let lua = Lua::new();
            // Load standard safe libraries (no debug needed - we use explicit context)
            lua.load_std_libs(mlua::StdLib::ALL_SAFE).ok();
            *borrow = Some(lua);
        }
        // SAFETY: The Lua VM is thread-local and lives for the duration of the thread
        unsafe { &*(borrow.as_ref().unwrap() as *const Lua) }
    })
}

/// Create the parallel module table
pub fn create_module(
    lua: &Lua,
    project_root: &Path,
    sandbox: bool,
    tracker: SharedTracker,
) -> Result<Table> {
    let parallel = lua.create_table()?;
    let root = project_root.to_path_buf();

    // parallel.load_json(paths) - Load multiple JSON files in parallel
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let load_json_parallel = lua.create_function(move |lua, paths: Table| {
        // Collect paths from Lua table
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        // Process in parallel
        let tracker_ref = &tracker_clone;
        let results: Vec<Option<serde_json::Value>> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, &root_clone);
                if sandbox && !is_path_within_root(&resolved, &root_clone) {
                    return None;
                }
                std::fs::read_to_string(&resolved).ok().and_then(|content| {
                    // Canonicalize path for consistent tracking
                    let canonical = resolved.canonicalize().unwrap_or(resolved);
                    tracker_ref.record_read(canonical, content.as_bytes());
                    serde_json::from_str(&content).ok()
                })
            })
            .collect();

        // Convert results back to Lua table
        let result_table = lua.create_table()?;
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Some(v) => result_table.set(i + 1, lua.to_value(&v)?)?,
                None => result_table.set(i + 1, Value::Nil)?,
            }
        }
        Ok(result_table)
    })?;
    parallel.set("load_json", load_json_parallel)?;

    // parallel.read_files(paths) - Read multiple files in parallel
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let read_files_parallel = lua.create_function(move |lua, paths: Table| {
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        let tracker_ref = &tracker_clone;
        let results: Vec<Option<String>> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, &root_clone);
                if sandbox && !is_path_within_root(&resolved, &root_clone) {
                    return None;
                }
                std::fs::read_to_string(&resolved).ok().inspect(|content| {
                    let canonical = resolved.canonicalize().unwrap_or(resolved);
                    tracker_ref.record_read(canonical, content.as_bytes());
                })
            })
            .collect();

        let result_table = lua.create_table()?;
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Some(content) => result_table.set(i + 1, lua.create_string(&content)?)?,
                None => result_table.set(i + 1, Value::Nil)?,
            }
        }
        Ok(result_table)
    })?;
    parallel.set("read_files", read_files_parallel)?;

    // parallel.file_exists(paths) - Check multiple files exist in parallel
    let root_clone = root.clone();
    let file_exists_parallel = lua.create_function(move |lua, paths: Table| {
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        let results: Vec<bool> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, &root_clone);
                if sandbox && !is_path_within_root(&resolved, &root_clone) {
                    return false;
                }
                resolved.exists()
            })
            .collect();

        let result_table = lua.create_table()?;
        for (i, exists) in results.into_iter().enumerate() {
            result_table.set(i + 1, exists)?;
        }
        Ok(result_table)
    })?;
    parallel.set("file_exists", file_exists_parallel)?;

    // parallel.load_yaml(paths) - Load multiple YAML files in parallel
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let load_yaml_parallel = lua.create_function(move |lua, paths: Table| {
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        let tracker_ref = &tracker_clone;
        let results: Vec<Option<serde_json::Value>> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, &root_clone);
                if sandbox && !is_path_within_root(&resolved, &root_clone) {
                    return None;
                }
                std::fs::read_to_string(&resolved).ok().and_then(|content| {
                    let canonical = resolved.canonicalize().unwrap_or(resolved);
                    tracker_ref.record_read(canonical, content.as_bytes());
                    serde_yaml::from_str(&content).ok()
                })
            })
            .collect();

        let result_table = lua.create_table()?;
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Some(v) => result_table.set(i + 1, lua.to_value(&v)?)?,
                None => result_table.set(i + 1, Value::Nil)?,
            }
        }
        Ok(result_table)
    })?;
    parallel.set("load_yaml", load_yaml_parallel)?;

    // parallel.read_frontmatter(paths) - Parse frontmatter from multiple files in parallel
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let read_frontmatter_parallel = lua.create_function(move |lua, paths: Table| {
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        let tracker_ref = &tracker_clone;
        let results: Vec<Option<(Option<serde_json::Value>, String, String)>> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, &root_clone);
                if sandbox && !is_path_within_root(&resolved, &root_clone) {
                    return None;
                }
                std::fs::read_to_string(&resolved).ok().map(|raw| {
                    let canonical = resolved.canonicalize().unwrap_or(resolved);
                    tracker_ref.record_read(canonical, raw.as_bytes());
                    let (fm, content) = parse_frontmatter_content(&raw);
                    (fm, content, raw)
                })
            })
            .collect();

        let result_table = lua.create_table()?;
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Some((fm, content, raw)) => {
                    let entry = lua.create_table()?;
                    entry.set("raw", raw)?;
                    entry.set("content", content)?;
                    match fm {
                        Some(v) => entry.set("frontmatter", lua.to_value(&v)?)?,
                        None => entry.set("frontmatter", lua.create_table()?)?,
                    }
                    result_table.set(i + 1, entry)?;
                }
                None => result_table.set(i + 1, Value::Nil)?,
            }
        }
        Ok(result_table)
    })?;
    parallel.set("read_frontmatter", read_frontmatter_parallel)?;

    // parallel.map(items, fn, ctx?) - True parallel map using thread-local Lua VMs
    // Items, function, and optional context are serialized, then executed in parallel.
    // Usage: rs.parallel.map(items, function(item, ctx) ... end, {key = value})
    // Note: Function upvalues are NOT captured. Pass any needed context explicitly.
    let map_fn = lua.create_function(
        |lua, (items, func, ctx): (Table, Function, Option<Table>)| {
            // Serialize the function to bytecode
            let portable_func = LuaPortable::from_lua(&Value::Function(func), lua)?;

            // Serialize the context if provided
            let portable_ctx = match ctx {
                Some(t) => Some(LuaPortable::from_lua(&Value::Table(t), lua)?),
                None => None,
            };

            // Serialize all items
            let portable_items: Vec<LuaPortable> = items
                .sequence_values::<Value>()
                .filter_map(|v| v.ok())
                .map(|v| LuaPortable::from_lua(&v, lua))
                .collect::<Result<Vec<_>>>()?;

            // Execute in parallel using rayon
            let results: Vec<std::result::Result<LuaPortable, String>> = portable_items
                .par_iter()
                .map(|item| {
                    let worker_lua = get_or_init_worker_lua();

                    // Load function from bytecode in this thread's VM
                    let func_value = portable_func
                        .to_lua(worker_lua)
                        .map_err(|e| format!("Failed to load function: {}", e))?;
                    let func = func_value
                        .as_function()
                        .ok_or_else(|| "Expected function".to_string())?;

                    // Convert item to Lua value
                    let lua_item = item
                        .to_lua(worker_lua)
                        .map_err(|e| format!("Failed to convert item: {}", e))?;

                    // Convert context to Lua value (or nil)
                    let lua_ctx = match &portable_ctx {
                        Some(ctx) => ctx
                            .to_lua(worker_lua)
                            .map_err(|e| format!("Failed to convert context: {}", e))?,
                        None => Value::Nil,
                    };

                    // Call function with (item, ctx)
                    let result: Value = func
                        .call((lua_item, lua_ctx))
                        .map_err(|e| format!("Function call failed: {}", e))?;

                    // Convert result back to portable
                    LuaPortable::from_lua(&result, worker_lua)
                        .map_err(|e| format!("Failed to serialize result: {}", e))
                })
                .collect();

            // Convert back to Lua table
            let result_table = lua.create_table()?;
            for (i, result) in results.into_iter().enumerate() {
                match result {
                    Ok(portable) => {
                        let value = portable.to_lua(lua)?;
                        result_table.set(i + 1, value)?;
                    }
                    Err(e) => {
                        return Err(mlua::Error::RuntimeError(format!(
                            "parallel.map failed at index {}: {}",
                            i + 1,
                            e
                        )));
                    }
                }
            }
            Ok(result_table)
        },
    )?;
    parallel.set("map", map_fn)?;

    // parallel.map_seq(items, fn) - Sequential map (for non-serializable items)
    // Use this when items contain userdata or other non-portable values
    let map_seq_fn = lua.create_function(|lua, (items, func): (Table, Function)| {
        let result_table = lua.create_table()?;
        let mut i = 1;
        for v in items.sequence_values::<Value>().flatten() {
            let res: Value = func.call(v)?;
            result_table.set(i, res)?;
            i += 1;
        }
        Ok(result_table)
    })?;
    parallel.set("map_seq", map_seq_fn)?;

    // parallel.filter(items, fn, ctx?) - True parallel filter using thread-local Lua VMs
    // Usage: rs.parallel.filter(items, function(item, ctx) ... end, {key = value})
    // Note: Function upvalues are NOT captured. Pass any needed context explicitly.
    let filter_fn = lua.create_function(
        |lua, (items, func, ctx): (Table, Function, Option<Table>)| {
            // Serialize the function to bytecode
            let portable_func = LuaPortable::from_lua(&Value::Function(func), lua)?;

            // Serialize the context if provided
            let portable_ctx = match ctx {
                Some(t) => Some(LuaPortable::from_lua(&Value::Table(t), lua)?),
                None => None,
            };

            // Serialize all items with their original indices
            let portable_items: Vec<(usize, LuaPortable)> = items
                .sequence_values::<Value>()
                .enumerate()
                .filter_map(|(i, v)| v.ok().map(|v| (i, v)))
                .map(|(i, v)| LuaPortable::from_lua(&v, lua).map(|p| (i, p)))
                .collect::<Result<Vec<_>>>()?;

            // Execute filter in parallel
            let results: Vec<std::result::Result<Option<LuaPortable>, String>> = portable_items
                .par_iter()
                .map(|(_, item)| {
                    let worker_lua = get_or_init_worker_lua();

                    // Load function
                    let func_value = portable_func
                        .to_lua(worker_lua)
                        .map_err(|e| format!("Failed to load function: {}", e))?;
                    let func = func_value
                        .as_function()
                        .ok_or_else(|| "Expected function".to_string())?;

                    // Convert item
                    let lua_item = item
                        .to_lua(worker_lua)
                        .map_err(|e| format!("Failed to convert item: {}", e))?;

                    // Convert context to Lua value (or nil)
                    let lua_ctx = match &portable_ctx {
                        Some(ctx) => ctx
                            .to_lua(worker_lua)
                            .map_err(|e| format!("Failed to convert context: {}", e))?,
                        None => Value::Nil,
                    };

                    // Call predicate with (item, ctx)
                    let keep: bool = func
                        .call((lua_item, lua_ctx))
                        .map_err(|e| format!("Filter predicate failed: {}", e))?;

                    if keep {
                        Ok(Some(item.clone()))
                    } else {
                        Ok(None)
                    }
                })
                .collect();

            // Collect kept items maintaining order
            let result_table = lua.create_table()?;
            let mut i = 1;
            for result in results.into_iter() {
                match result {
                    Ok(Some(portable)) => {
                        let value = portable.to_lua(lua)?;
                        result_table.set(i, value)?;
                        i += 1;
                    }
                    Ok(None) => {} // Filtered out
                    Err(e) => {
                        return Err(mlua::Error::RuntimeError(format!(
                            "parallel.filter failed: {}",
                            e
                        )));
                    }
                }
            }
            Ok(result_table)
        },
    )?;
    parallel.set("filter", filter_fn)?;

    // parallel.filter_seq(items, fn) - Sequential filter (for non-serializable items)
    let filter_seq_fn = lua.create_function(|lua, (items, func): (Table, Function)| {
        let result_table = lua.create_table()?;
        let mut i = 1;
        for v in items.sequence_values::<Value>().flatten() {
            let keep: bool = func.call(v.clone())?;
            if keep {
                result_table.set(i, v)?;
                i += 1;
            }
        }
        Ok(result_table)
    })?;
    parallel.set("filter_seq", filter_seq_fn)?;

    // parallel.reduce(items, initial, fn) - Reduce items to single value
    let reduce_fn =
        lua.create_function(|_, (items, initial, func): (Table, Value, Function)| {
            let mut acc = initial;
            for v in items.sequence_values::<Value>().flatten() {
                acc = func.call((acc, v))?;
            }
            Ok(acc)
        })?;
    parallel.set("reduce", reduce_fn)?;

    // parallel.create_dirs(paths) - Create multiple directories in parallel
    let root_clone = root.clone();
    let create_dirs_parallel = lua.create_function(move |lua, paths: Table| {
        let path_list: Vec<String> = paths
            .sequence_values::<String>()
            .filter_map(|r| r.ok())
            .collect();

        let root_ref = &root_clone;
        let results: Vec<std::result::Result<(), String>> = path_list
            .par_iter()
            .map(|path| {
                let resolved = resolve_path(path, root_ref);
                std::fs::create_dir_all(&resolved)
                    .map_err(|e| format!("Failed to create {}: {}", path, e))
            })
            .collect();

        let result_table = lua.create_table()?;
        for (i, result) in results.into_iter().enumerate() {
            match result {
                Ok(()) => result_table.set(i + 1, true)?,
                Err(e) => result_table.set(i + 1, e)?,
            }
        }
        Ok(result_table)
    })?;
    parallel.set("create_dirs", create_dirs_parallel)?;

    // parallel.copy_files(sources, destinations) - Copy multiple files in parallel
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let copy_files_parallel =
        lua.create_function(move |lua, (sources, dests): (Table, Table)| {
            let source_list: Vec<String> = sources
                .sequence_values::<String>()
                .filter_map(|r| r.ok())
                .collect();
            let dest_list: Vec<String> = dests
                .sequence_values::<String>()
                .filter_map(|r| r.ok())
                .collect();

            if source_list.len() != dest_list.len() {
                return Err(mlua::Error::external(
                    "parallel.copy_files: sources and destinations must have same length",
                ));
            }

            let pairs: Vec<_> = source_list.into_iter().zip(dest_list).collect();
            let tracker_ref = &tracker_clone;
            let root_ref = &root_clone;

            let results: Vec<std::result::Result<(), String>> = pairs
                .par_iter()
                .map(|(src, dest)| {
                    let src_path = resolve_path(src, root_ref);
                    let dest_path = resolve_path(dest, root_ref);

                    if sandbox && !is_path_within_root(&src_path, root_ref) {
                        return Err(format!("Source path outside sandbox: {}", src));
                    }

                    // Create parent directory
                    if let Some(parent) = dest_path.parent() {
                        std::fs::create_dir_all(parent)
                            .map_err(|e| format!("Failed to create directory: {}", e))?;
                    }

                    // Read and copy
                    let content = std::fs::read(&src_path)
                        .map_err(|e| format!("Failed to read {}: {}", src, e))?;

                    let src_canonical = src_path.canonicalize().unwrap_or(src_path);
                    tracker_ref.record_read(src_canonical, &content);

                    std::fs::write(&dest_path, &content)
                        .map_err(|e| format!("Failed to write {}: {}", dest, e))?;

                    let dest_canonical = dest_path.canonicalize().unwrap_or(dest_path);
                    tracker_ref.record_write(dest_canonical, &content);

                    Ok(())
                })
                .collect();

            // Return table of results (true for success, error string for failure)
            let result_table = lua.create_table()?;
            for (i, result) in results.into_iter().enumerate() {
                match result {
                    Ok(()) => result_table.set(i + 1, true)?,
                    Err(e) => result_table.set(i + 1, e)?,
                }
            }
            Ok(result_table)
        })?;
    parallel.set("copy_files", copy_files_parallel)?;

    // parallel.image_convert(sources, destinations, options?) - Convert multiple images in parallel
    // options: { quality?: number }
    let root_clone = root.clone();
    let tracker_clone = tracker.clone();
    let image_convert_parallel = lua.create_function(
        move |lua, (sources, dests, options): (Table, Table, Option<Table>)| {
            let source_list: Vec<String> = sources
                .sequence_values::<String>()
                .filter_map(|r| r.ok())
                .collect();
            let dest_list: Vec<String> = dests
                .sequence_values::<String>()
                .filter_map(|r| r.ok())
                .collect();

            if source_list.len() != dest_list.len() {
                return Err(mlua::Error::external(
                    "parallel.image_convert: sources and destinations must have same length",
                ));
            }

            let quality: f32 = options
                .as_ref()
                .and_then(|t| t.get("quality").ok())
                .unwrap_or(85.0);

            let pairs: Vec<_> = source_list.into_iter().zip(dest_list).collect();
            let tracker_ref = &tracker_clone;
            let root_ref = &root_clone;

            let results: Vec<std::result::Result<(), String>> = pairs
                .par_iter()
                .map(|(src, dest)| {
                    let src_path = resolve_path(src, root_ref);
                    let dest_path = resolve_path(dest, root_ref);

                    if sandbox && !is_path_within_root(&src_path, root_ref) {
                        return Err(format!("Source path outside sandbox: {}", src));
                    }

                    // Read source
                    let content = std::fs::read(&src_path)
                        .map_err(|e| format!("Failed to read {}: {}", src, e))?;

                    let src_canonical = src_path.canonicalize().unwrap_or(src_path.clone());
                    tracker_ref.record_read(src_canonical, &content);

                    // Open and apply EXIF orientation
                    let img = image::open(&src_path)
                        .map_err(|e| format!("Failed to open image {}: {}", src, e))?;
                    let img = apply_exif_orientation(img, &src_path);

                    // Create parent directory
                    if let Some(parent) = dest_path.parent() {
                        std::fs::create_dir_all(parent)
                            .map_err(|e| format!("Failed to create directory: {}", e))?;
                    }

                    // Determine format and save
                    let ext = dest_path
                        .extension()
                        .and_then(|e| e.to_str())
                        .map(|e| e.to_lowercase())
                        .unwrap_or_else(|| "png".to_string());

                    let output_bytes = match ext.as_str() {
                        "webp" => {
                            let encoder = webp::Encoder::from_image(&img)
                                .map_err(|e| format!("WebP encoder error: {}", e))?;
                            encoder.encode(quality).to_vec()
                        }
                        "jpg" | "jpeg" => {
                            let mut buf = Vec::new();
                            let mut cursor = std::io::Cursor::new(&mut buf);
                            img.write_to(&mut cursor, image::ImageFormat::Jpeg)
                                .map_err(|e| format!("JPEG encode error: {}", e))?;
                            buf
                        }
                        "png" => {
                            let mut buf = Vec::new();
                            let mut cursor = std::io::Cursor::new(&mut buf);
                            img.write_to(&mut cursor, image::ImageFormat::Png)
                                .map_err(|e| format!("PNG encode error: {}", e))?;
                            buf
                        }
                        _ => {
                            return Err(format!("Unsupported output format: {}", ext));
                        }
                    };

                    std::fs::write(&dest_path, &output_bytes)
                        .map_err(|e| format!("Failed to write {}: {}", dest, e))?;

                    let dest_canonical = dest_path.canonicalize().unwrap_or(dest_path);
                    tracker_ref.record_write(dest_canonical, &output_bytes);

                    Ok(())
                })
                .collect();

            // Return table of results
            let result_table = lua.create_table()?;
            for (i, result) in results.into_iter().enumerate() {
                match result {
                    Ok(()) => result_table.set(i + 1, true)?,
                    Err(e) => result_table.set(i + 1, e)?,
                }
            }
            Ok(result_table)
        },
    )?;
    parallel.set("image_convert", image_convert_parallel)?;

    Ok(parallel)
}

/// Apply EXIF orientation to an image
fn apply_exif_orientation(img: DynamicImage, path: &Path) -> DynamicImage {
    use std::fs::File;
    use std::io::BufReader;

    let file = match File::open(path) {
        Ok(f) => f,
        Err(_) => return img,
    };

    let exif = match exif::Reader::new().read_from_container(&mut BufReader::new(file)) {
        Ok(e) => e,
        Err(_) => return img,
    };

    let orientation = exif
        .get_field(exif::Tag::Orientation, exif::In::PRIMARY)
        .and_then(|f| f.value.get_uint(0))
        .unwrap_or(1);

    match orientation {
        1 => img,
        2 => img.fliph(),
        3 => img.rotate180(),
        4 => img.flipv(),
        5 => img.rotate90().fliph(),
        6 => img.rotate90(),
        7 => img.rotate270().fliph(),
        8 => img.rotate270(),
        _ => img,
    }
}