seex 1.0.0

Clipboard event tracker and GUI bridge for exporting LCSC component libraries through nlbn and npnp.
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
use nlbn::checkpoint::{append_checkpoint, load_checkpoint};
use nlbn::{Cli, EasyedaApi, LibraryManager};
use serde::Serialize;
use std::path::PathBuf;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Emitter};
use tokio::sync::Semaphore;
use tokio::task::JoinSet;

use crate::monitor::MonitorState;

const DEFAULT_OUTPUT_DIR: &str = "~/lib";

#[derive(Clone, Serialize)]
struct ExportFinishedPayload {
    tool: &'static str,
    success: bool,
    message: String,
}

#[derive(Clone, Serialize)]
struct ExportProgressPayload {
    tool: &'static str,
    message: String,
    determinate: bool,
    current: Option<usize>,
    total: Option<usize>,
}

struct ItemOutcome {
    id: String,
    result: Result<(), String>,
}

pub struct ExportRequest {
    pub ids: Vec<String>,
    pub output_path: String,
    pub mode: String,
    pub append: bool,
    pub library_name: String,
    pub parallel: usize,
    pub continue_on_error: bool,
    pub overwrite: bool,
    pub project_relative: bool,
}

pub fn spawn_export(state: Arc<Mutex<MonitorState>>, req: ExportRequest, app_handle: AppHandle) {
    if let Ok(mut s) = state.lock() {
        s.nlbn_running = true;
        s.nlbn_last_result = None;
    }

    emit_progress(
        &app_handle,
        "Preparing nlbn export...",
        false,
        None,
        Some(req.ids.len()),
    );

    tauri::async_runtime::spawn(async move {
        let result = run_export(&req, &app_handle).await;
        let (success, message) = match result {
            Ok(message) => (true, message),
            Err(error) => (false, error),
        };

        if let Ok(mut s) = state.lock() {
            s.nlbn_running = false;
            s.nlbn_last_result = Some(message.clone());
            s.add_debug_log(message.clone());
        }

        let _ = app_handle.emit("clipboard-changed", ());
        let _ = app_handle.emit(
            "export-finished",
            ExportFinishedPayload {
                tool: "nlbn",
                success,
                message,
            },
        );
    });
}

fn emit_progress(
    app_handle: &AppHandle,
    message: impl Into<String>,
    determinate: bool,
    current: Option<usize>,
    total: Option<usize>,
) {
    let _ = app_handle.emit(
        "export-progress",
        ExportProgressPayload {
            tool: "nlbn",
            message: message.into(),
            determinate,
            current,
            total,
        },
    );
}

async fn run_export(req: &ExportRequest, app_handle: &AppHandle) -> Result<String, String> {
    let args = Arc::new(build_cli(req));
    let lib_manager = Arc::new(LibraryManager::from_cli(&args).map_err(|e| e.to_string())?);
    lib_manager
        .create_directories()
        .map_err(|e| e.to_string())?;

    let checkpoint_path = args.output.join(".checkpoint");
    let completed_ids = load_checkpoint(&checkpoint_path);
    let total_requested = req.ids.len();
    let pending_ids: Vec<String> = req
        .ids
        .iter()
        .filter(|id| !completed_ids.contains(id.as_str()))
        .cloned()
        .collect();
    let skipped_by_checkpoint = total_requested.saturating_sub(pending_ids.len());

    if pending_ids.is_empty() {
        return Ok(format_summary(
            req,
            total_requested,
            skipped_by_checkpoint,
            0,
            0,
            &[],
        ));
    }

    let total = pending_ids.len();
    emit_progress(
        app_handle,
        running_message(req, total),
        false,
        None,
        Some(total),
    );

    let api = Arc::new(EasyedaApi::new());
    let success_count = Arc::new(AtomicUsize::new(0));
    let failed_count = Arc::new(AtomicUsize::new(0));
    let processed_count = Arc::new(AtomicUsize::new(0));
    let failed_ids = Arc::new(Mutex::new(Vec::new()));

    if total > 1 && req.parallel.max(1) > 1 {
        let semaphore = Arc::new(Semaphore::new(req.parallel.max(1)));
        let mut join_set = JoinSet::new();

        for id in pending_ids {
            let semaphore = semaphore.clone();
            let args = args.clone();
            let api = api.clone();
            let lib_manager = lib_manager.clone();

            join_set.spawn(async move {
                let _permit = semaphore.acquire_owned().await.map_err(|e| e.to_string())?;
                let result = process_component(&args, &api, &lib_manager, &id)
                    .await
                    .map_err(|e| e.to_string());
                Ok::<ItemOutcome, String>(ItemOutcome { id, result })
            });
        }

        while let Some(joined) = join_set.join_next().await {
            let outcome = match joined {
                Ok(Ok(outcome)) => outcome,
                Ok(Err(error)) => ItemOutcome {
                    id: "<task>".to_string(),
                    result: Err(error),
                },
                Err(error) => ItemOutcome {
                    id: "<task>".to_string(),
                    result: Err(error.to_string()),
                },
            };

            register_outcome(
                app_handle,
                &outcome,
                total,
                &processed_count,
                &success_count,
                &failed_count,
                &failed_ids,
            );
        }
    } else {
        for id in pending_ids {
            let outcome = ItemOutcome {
                id: id.clone(),
                result: process_component(&args, &api, &lib_manager, &id)
                    .await
                    .map_err(|e| e.to_string()),
            };

            register_outcome(
                app_handle,
                &outcome,
                total,
                &processed_count,
                &success_count,
                &failed_count,
                &failed_ids,
            );

            if outcome.result.is_err() && !req.continue_on_error {
                return Err(format_stopped_error(
                    req,
                    &outcome.id,
                    outcome
                        .result
                        .as_ref()
                        .err()
                        .map(|message| message.as_str())
                        .unwrap_or("unknown error"),
                    total_requested,
                    skipped_by_checkpoint,
                    success_count.load(Ordering::Relaxed),
                    failed_count.load(Ordering::Relaxed),
                    &failed_ids.lock().map(|ids| ids.clone()).unwrap_or_default(),
                ));
            }
        }
    }

    let failed_ids = failed_ids.lock().map(|ids| ids.clone()).unwrap_or_default();
    Ok(format_summary(
        req,
        total_requested,
        skipped_by_checkpoint,
        success_count.load(Ordering::Relaxed),
        failed_count.load(Ordering::Relaxed),
        &failed_ids,
    ))
}

fn register_outcome(
    app_handle: &AppHandle,
    outcome: &ItemOutcome,
    total: usize,
    processed_count: &Arc<AtomicUsize>,
    success_count: &Arc<AtomicUsize>,
    failed_count: &Arc<AtomicUsize>,
    failed_ids: &Arc<Mutex<Vec<String>>>,
) {
    match &outcome.result {
        Ok(()) => {
            success_count.fetch_add(1, Ordering::Relaxed);
        }
        Err(_) => {
            failed_count.fetch_add(1, Ordering::Relaxed);
            if let Ok(mut ids) = failed_ids.lock() {
                ids.push(outcome.id.clone());
            }
        }
    }

    let current = processed_count.fetch_add(1, Ordering::Relaxed) + 1;
    let message = match &outcome.result {
        Ok(()) => format!("Processed {} successfully", outcome.id),
        Err(error) => format!("Failed {}: {}", outcome.id, error),
    };
    emit_progress(app_handle, message, true, Some(current), Some(total));
}

async fn process_component(
    args: &Cli,
    api: &EasyedaApi,
    lib_manager: &LibraryManager,
    lcsc_id: &str,
) -> nlbn::Result<()> {
    let component_data = api.get_component_data(lcsc_id).await?;

    if args.symbol || args.full {
        nlbn::symbol_converter::convert_symbol(args, &component_data, lib_manager, lcsc_id)?;
    }

    if args.footprint || args.full {
        nlbn::footprint_converter::convert_footprint(args, &component_data, lib_manager, lcsc_id)?;
    }

    if args.model_3d || args.full {
        nlbn::model_converter::convert_3d_model(args, api, &component_data, lib_manager, lcsc_id)
            .await?;
    }

    append_checkpoint(&args.output.join(".checkpoint"), lcsc_id);
    Ok(())
}

fn build_cli(req: &ExportRequest) -> Cli {
    let mode = normalize_mode(&req.mode);
    let output = PathBuf::from(normalize_output_path(&req.output_path));

    Cli {
        lcsc_id: None,
        batch: None,
        symbol: mode == "symbol",
        footprint: mode == "footprint",
        model_3d: mode == "3d",
        full: mode == "full",
        output,
        lib_name: effective_library_name(req),
        symbol_lib: None,
        footprint_lib: None,
        model_lib: None,
        prompt: false,
        overwrite: req.overwrite,
        project_relative: req.project_relative,
        debug: false,
        continue_on_error: req.continue_on_error,
        parallel: req.parallel.max(1),
    }
}

fn normalize_mode(mode: &str) -> &str {
    match mode.trim().to_ascii_lowercase().as_str() {
        "symbol" => "symbol",
        "footprint" => "footprint",
        "3d" => "3d",
        _ => "full",
    }
}

fn normalize_output_path(path: &str) -> String {
    let trimmed = path.trim();
    if trimmed.is_empty() {
        DEFAULT_OUTPUT_DIR.to_string()
    } else {
        trimmed.to_string()
    }
}

fn effective_library_name(req: &ExportRequest) -> Option<String> {
    let trimmed = req.library_name.trim();
    if trimmed.is_empty() {
        None
    } else {
        Some(trimmed.to_string())
    }
}

fn running_message(req: &ExportRequest, total: usize) -> String {
    format!(
        "Running nlbn {} batch for {} items...",
        mode_label(&req.mode).to_ascii_lowercase(),
        total
    )
}

fn format_summary(
    req: &ExportRequest,
    total_requested: usize,
    skipped_by_checkpoint: usize,
    success_count: usize,
    failed_count: usize,
    failed_ids: &[String],
) -> String {
    let mut lines = vec![format!(
        "nlbn batch complete. Total: {} | Checkpoint skipped: {} | Success: {} | Failed: {}",
        total_requested, skipped_by_checkpoint, success_count, failed_count
    )];

    lines.push(format!(
        "Targets: {} | Parallel: {}",
        mode_label(&req.mode),
        req.parallel.max(1),
    ));

    lines.push(format!(
        "Append: {} | Continue on error: {} | Overwrite: {} | Project relative: {}",
        yes_no(req.append),
        yes_no(req.continue_on_error),
        yes_no(req.overwrite),
        yes_no(req.project_relative),
    ));

    lines.push(format!("Library name: {}", resolved_library_name(req)));
    lines.push(format!(
        "Output directory: {}",
        normalize_output_path(&req.output_path)
    ));

    if !failed_ids.is_empty() {
        lines.push(format!("Failed IDs: {}", failed_ids.join(", ")));
    }

    lines.join("\n")
}

fn format_stopped_error(
    req: &ExportRequest,
    failed_id: &str,
    error: &str,
    total_requested: usize,
    skipped_by_checkpoint: usize,
    success_count: usize,
    failed_count: usize,
    failed_ids: &[String],
) -> String {
    let mut lines = vec![
        "nlbn export failed".to_string(),
        format!("Stopped on {}: {}", failed_id, error),
        format!(
            "Total: {} | Checkpoint skipped: {} | Success: {} | Failed: {}",
            total_requested, skipped_by_checkpoint, success_count, failed_count
        ),
        format!(
            "Targets: {} | Parallel: {}",
            mode_label(&req.mode),
            req.parallel.max(1),
        ),
        format!(
            "Append: {} | Continue on error: {} | Overwrite: {} | Project relative: {}",
            yes_no(req.append),
            yes_no(req.continue_on_error),
            yes_no(req.overwrite),
            yes_no(req.project_relative),
        ),
        format!("Library name: {}", resolved_library_name(req)),
        format!(
            "Output directory: {}",
            normalize_output_path(&req.output_path)
        ),
    ];

    if !failed_ids.is_empty() {
        lines.push(format!("Failed IDs: {}", failed_ids.join(", ")));
    }

    lines.join("\n")
}

fn mode_label(mode: &str) -> &'static str {
    match normalize_mode(mode) {
        "symbol" => "Symbol",
        "footprint" => "Footprint",
        "3d" => "3D",
        _ => "Full",
    }
}

fn resolved_library_name(req: &ExportRequest) -> String {
    effective_library_name(req).unwrap_or_else(|| {
        let output = PathBuf::from(normalize_output_path(&req.output_path));
        output
            .file_name()
            .and_then(|name| name.to_str())
            .filter(|name| !name.is_empty())
            .unwrap_or("nlbn")
            .to_string()
    })
}

fn yes_no(value: bool) -> &'static str {
    if value { "ON" } else { "OFF" }
}