TransJLC 0.4.0

TransJLC is a tool for converting Gerber files from other EDAs to JLCEDA style
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
//! Archive handling for ZIP file operations
//!
//! This module provides functionality for extracting input ZIP files
//! and creating output ZIP files with proper progress tracking.

use crate::error::{Result, ResultExt, TransJlcError};
use anyhow::Context;
use indicatif::{ProgressBar, ProgressStyle};
use std::collections::HashMap;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use tempfile::TempDir;
use tracing::{info, warn};
use zip::ZipArchive;

/// Archive extractor for handling ZIP input files
pub struct ArchiveExtractor {
    temp_dir: Option<TempDir>,
}

impl Default for ArchiveExtractor {
    fn default() -> Self {
        Self::new()
    }
}

impl ArchiveExtractor {
    /// Create a new archive extractor
    pub fn new() -> Self {
        Self { temp_dir: None }
    }

    /// Extract ZIP file if the input path is a ZIP file
    /// Returns the path to use for processing (original path or extracted directory)
    pub fn extract_if_needed(&mut self, input_path: &Path, show_progress: bool) -> Result<PathBuf> {
        if !self.is_zip_file(input_path) {
            info!(
                "Input is not a ZIP file, using as directory: {}",
                input_path.display()
            );
            return Ok(input_path.to_path_buf());
        }

        info!("Extracting archive: {}", input_path.display());

        // Create temporary directory
        let temp_dir =
            TempDir::new().context("Failed to create temporary directory for ZIP extraction")?;

        let temp_path = temp_dir.path();

        // Extract ZIP file
        self.extract_zip_to_directory(input_path, temp_path, show_progress)
            .with_path_context("extract ZIP file", input_path)?;
        self.recursive_unzip(temp_path)
            .with_path_context("extract nested ZIP files", input_path)?;
        let flat_path = temp_path.join("_flat");
        fs::create_dir_all(&flat_path)
            .with_path_context("create flat extraction directory", &flat_path)?;
        let flattened_count = self
            .flatten_processable_files(temp_path, &flat_path)
            .with_path_context("flatten extracted files", input_path)?;

        let extracted_path = if flattened_count > 0 {
            flat_path
        } else {
            temp_path.to_path_buf()
        };
        self.temp_dir = Some(temp_dir);

        info!("ZIP file extracted to: {}", extracted_path.display());
        Ok(extracted_path)
    }

    /// Check if a file is a ZIP file based on extension
    fn is_zip_file(&self, path: &Path) -> bool {
        path.is_file()
            && path
                .extension()
                .and_then(|ext| ext.to_str())
                .map(|ext| ext.to_lowercase() == "zip")
                .unwrap_or(false)
    }

    /// Extract ZIP file to the specified directory
    fn extract_zip_to_directory(
        &self,
        zip_path: &Path,
        target_dir: &Path,
        show_progress: bool,
    ) -> Result<()> {
        let file = fs::File::open(zip_path).with_path_context("open ZIP file", zip_path)?;

        let mut archive =
            ZipArchive::new(file).map_err(|e| TransJlcError::ZipExtractionFailed {
                reason: format!("Invalid ZIP file: {}", e),
            })?;

        let total_files = archive.len();
        info!("Extracted {} entries from archive", total_files);

        let progress = if show_progress {
            let pb = ProgressBar::new(total_files as u64);
            pb.set_style(
                ProgressStyle::default_bar()
                    .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}")?
                    .progress_chars("#>-")
            );
            pb.set_message("Extracting files...");
            Some(pb)
        } else {
            None
        };

        for i in 0..archive.len() {
            let mut file = archive
                .by_index(i)
                .map_err(|e| TransJlcError::ZipExtractionFailed {
                    reason: format!("Failed to read file at index {}: {}", i, e),
                })?;

            let Some(enclosed_name) = file.enclosed_name().map(|p| p.to_path_buf()) else {
                warn!("Skipping unsafe ZIP entry path: {}", file.name());
                continue;
            };
            let outpath = target_dir.join(enclosed_name);

            if file.is_dir() {
                fs::create_dir_all(&outpath).with_path_context("create directory", &outpath)?;
            } else {
                // Create parent directories if needed
                if let Some(parent) = outpath.parent() {
                    fs::create_dir_all(parent)
                        .with_path_context("create parent directory", parent)?;
                }

                // Extract file
                let mut outfile =
                    fs::File::create(&outpath).with_path_context("create output file", &outpath)?;

                io::copy(&mut file, &mut outfile)
                    .with_path_context("write extracted file", &outpath)?;
            }

            if let Some(ref pb) = progress {
                pb.inc(1);
            }
        }

        if let Some(pb) = progress {
            pb.finish_with_message("Extraction completed");
        }

        Ok(())
    }

    /// Recursively extract nested ZIP files under the extraction root
    fn recursive_unzip(&self, root: &Path) -> Result<()> {
        loop {
            let mut zips = Vec::new();
            Self::collect_files(root, &mut zips, |path| {
                path.is_file()
                    && path
                        .extension()
                        .and_then(|ext| ext.to_str())
                        .map(|ext| ext.eq_ignore_ascii_case("zip"))
                        .unwrap_or(false)
            })?;

            if zips.is_empty() {
                return Ok(());
            }

            for zip_path in zips {
                let Some(parent) = zip_path.parent() else {
                    continue;
                };
                self.extract_zip_to_directory(&zip_path, parent, false)
                    .with_path_context("extract nested ZIP file", &zip_path)?;
                if let Err(err) = fs::remove_file(&zip_path) {
                    warn!(
                        "Failed to remove nested ZIP after extraction ({}): {}",
                        zip_path.display(),
                        err
                    );
                }
            }
        }
    }

    /// Flatten processable files into one working directory
    fn flatten_processable_files(&self, root: &Path, dest: &Path) -> Result<usize> {
        let mut files = Vec::new();
        Self::collect_files(root, &mut files, |path| {
            path.is_file() && Self::looks_processable(path)
        })?;

        let mut candidates: HashMap<String, PathBuf> = HashMap::new();
        for path in files {
            if path.starts_with(dest) {
                continue;
            }
            if path
                .strip_prefix(root)
                .ok()
                .map(|relative| {
                    relative.components().any(|component| {
                        component
                            .as_os_str()
                            .to_str()
                            .map(|part| part.starts_with("__"))
                            .unwrap_or(false)
                    })
                })
                .unwrap_or(false)
            {
                continue;
            }

            let Some(filename) = path.file_name().and_then(|name| name.to_str()) else {
                continue;
            };

            match candidates.get(filename) {
                None => {
                    candidates.insert(filename.to_string(), path);
                }
                Some(existing) if Self::prefer_candidate(root, &path, existing) => {
                    candidates.insert(filename.to_string(), path);
                }
                Some(_) => {}
            }
        }

        let count = candidates.len();
        for (filename, source) in candidates {
            fs::copy(&source, dest.join(filename))
                .with_path_context("copy flattened file", &source)?;
        }

        info!("Flattened {} processable files", count);
        Ok(count)
    }

    fn collect_files<F>(root: &Path, files: &mut Vec<PathBuf>, predicate: F) -> Result<()>
    where
        F: Fn(&Path) -> bool + Copy,
    {
        for entry in fs::read_dir(root).with_path_context("read directory", root)? {
            let entry = entry.with_path_context("read directory entry", root)?;
            let path = entry.path();
            if path.is_dir() {
                Self::collect_files(&path, files, predicate)?;
            } else if predicate(&path) {
                files.push(path);
            }
        }

        Ok(())
    }

    fn prefer_candidate(root: &Path, new_path: &Path, old_path: &Path) -> bool {
        let new_depth = new_path
            .strip_prefix(root)
            .map(|path| path.components().count())
            .unwrap_or(usize::MAX);
        let old_depth = old_path
            .strip_prefix(root)
            .map(|path| path.components().count())
            .unwrap_or(usize::MAX);

        if new_depth != old_depth {
            return new_depth < old_depth;
        }

        let new_mtime = new_path.metadata().and_then(|meta| meta.modified()).ok();
        let old_mtime = old_path.metadata().and_then(|meta| meta.modified()).ok();
        new_mtime > old_mtime
    }

    fn looks_processable(path: &Path) -> bool {
        if let Some(filename) = path.file_name().and_then(|name| name.to_str()) {
            if filename.eq_ignore_ascii_case("FlyingProbeTesting.json") {
                return true;
            }
        }

        let Some(ext) = path.extension().and_then(|ext| ext.to_str()) else {
            return false;
        };
        let ext = ext.to_ascii_lowercase();
        matches!(
            ext.as_str(),
            "gtl"
                | "gbl"
                | "gto"
                | "gbo"
                | "gts"
                | "gbs"
                | "gtp"
                | "gbp"
                | "gko"
                | "gm1"
                | "gm2"
                | "gm3"
                | "gm4"
                | "gm5"
                | "gm9"
                | "gm10"
                | "gm12"
                | "gd1"
                | "gg1"
                | "gbr"
                | "drl"
                | "txt"
                | "tap"
                | "nc"
                | "gdd"
                | "pho"
                | "art"
        ) || (ext.len() > 1
            && ext.starts_with('g')
            && ext[1..].chars().all(|ch| ch.is_ascii_digit()))
    }

    /// Get the temporary directory path if ZIP was extracted
    pub fn temp_path(&self) -> Option<&Path> {
        self.temp_dir.as_ref().map(|dir| dir.path())
    }
}

impl Drop for ArchiveExtractor {
    fn drop(&mut self) {
        if self.temp_dir.is_some() {
            info!("Cleaning up temporary extraction directory");
        }
    }
}

/// Archive creator for building output ZIP files
pub struct ArchiveCreator;

impl ArchiveCreator {
    /// Create a ZIP file from a collection of files
    pub fn create_zip<P: AsRef<Path>, I: IntoIterator<Item = P>>(
        files: I,
        output_path: P,
        show_progress: bool,
    ) -> Result<()> {
        let output_path = output_path.as_ref();
        let files: Vec<PathBuf> = files
            .into_iter()
            .map(|p| p.as_ref().to_path_buf())
            .collect();

        info!("Creating archive: {}", output_path.display());

        // Create output directory if it doesn't exist
        if let Some(parent) = output_path.parent() {
            fs::create_dir_all(parent).with_path_context("create output directory", parent)?;
        }

        let file =
            fs::File::create(output_path).with_path_context("create ZIP file", output_path)?;

        let mut zip = zip::ZipWriter::new(file);
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored)
            .unix_permissions(0o755);

        let progress = if show_progress {
            let pb = ProgressBar::new(files.len() as u64);
            pb.set_style(
                ProgressStyle::default_bar()
                    .template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len} {msg}")?
                    .progress_chars("#>-")
            );
            pb.set_message("Creating ZIP file...");
            Some(pb)
        } else {
            None
        };

        for file_path in files {
            let file_name = file_path
                .file_name()
                .and_then(|name| name.to_str())
                .context("Invalid filename")?;

            zip.start_file(file_name, options)
                .context("Failed to start ZIP file entry")?;

            let content =
                fs::read(&file_path).with_path_context("read file for ZIP", &file_path)?;

            use std::io::Write;
            zip.write_all(&content)
                .context("Failed to write file content to ZIP")?;

            if let Some(ref pb) = progress {
                pb.inc(1);
            }
        }

        zip.finish().context("Failed to finalize ZIP file")?;

        if let Some(pb) = progress {
            pb.finish_with_message("ZIP file created successfully");
        }

        info!("ZIP file created successfully: {}", output_path.display());
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write;
    use tempfile::tempdir;

    #[test]
    fn test_is_zip_file() {
        let _extractor = ArchiveExtractor::new();

        // Test ZIP file detection
        let zip_path = Path::new("test.zip");
        let txt_path = Path::new("test.txt");
        let _dir_path = Path::new("test_dir");

        // Note: These tests would need actual files to be fully functional
        // This is testing the logic, not actual file access
        assert!(zip_path.extension().unwrap() == "zip");
        assert!(txt_path.extension().unwrap() != "zip");
    }

    #[test]
    fn test_archive_creator_options() {
        let temp_dir = tempdir().expect("create temp dir");
        let source_path = temp_dir.path().join("source.txt");
        let zip_path = temp_dir.path().join("output.zip");

        fs::write(&source_path, "test").expect("write source file");
        ArchiveCreator::create_zip([source_path.as_path()], zip_path.as_path(), false)
            .expect("create zip");

        let file = fs::File::open(zip_path).expect("open zip");
        let archive = zip::ZipArchive::new(file).expect("read zip");
        assert_eq!(archive.len(), 1);
    }

    #[test]
    fn test_nested_zip_is_flattened() {
        let temp_dir = tempdir().expect("create temp dir");
        let inner_zip = temp_dir.path().join("inner.zip");
        write_test_zip(
            &inner_zip,
            vec![
                ("nested/project.GTL", b"top".to_vec()),
                ("nested/FlyingProbeTesting.json", b"{\"ok\":true}".to_vec()),
            ],
        );

        let outer_zip = temp_dir.path().join("outer.zip");
        write_test_zip(
            &outer_zip,
            vec![
                (
                    "wrapper/inner.zip",
                    fs::read(&inner_zip).expect("read inner zip"),
                ),
                ("wrapper/project.GKO", b"outline".to_vec()),
            ],
        );

        let mut extractor = ArchiveExtractor::new();
        let working = extractor
            .extract_if_needed(&outer_zip, false)
            .expect("extract outer zip");

        assert!(working.join("project.GTL").exists());
        assert!(working.join("project.GKO").exists());
        assert!(working.join("FlyingProbeTesting.json").exists());
    }

    fn write_test_zip(path: &Path, entries: Vec<(&str, Vec<u8>)>) {
        let file = fs::File::create(path).expect("create test zip");
        let mut zip = zip::ZipWriter::new(file);
        let options = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);

        for (name, bytes) in entries {
            zip.start_file(name, options).expect("start zip entry");
            zip.write_all(&bytes).expect("write zip entry");
        }

        zip.finish().expect("finish test zip");
    }
}