touchstone 0.12.1

Touchstone (s2p, etc.) file parser, plotter, and more
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
use std::env;
use std::process;

// this cannot be crate::Network because of how Cargo works,
// since cargo/rust treats lib.rs and main.rs as separate crates
use crate::file_operations;
use crate::open;
use crate::plot;
use crate::Network;

/// Command-line interface configuration and entry point.
pub struct Config {}

impl Config {
    /// Parse command-line arguments and execute the appropriate action.
    pub fn run(args: &[String]) -> Result<Config, &'static str> {
        if args.len() < 2 {
            return Err("not enough arguments");
        }

        if args.len() > 2 && args[1] != "cascade" {
            return Err("too many arguments, expecting only 2, such as `touchstone filepath`");
        }

        // Check for special flags
        match args[1].as_str() {
            "--version" | "-v" => {
                print_version();
                process::exit(0);
            }
            "--help" | "-h" => {
                print_help();
                process::exit(0);
            }
            "cascade" => {
                // Parse arguments for --name or -n
                let mut output_name: Option<String> = None;
                let mut file_paths = Vec::new();

                let mut i = 2;
                while i < args.len() {
                    match args[i].as_str() {
                        "--name" | "-n" => {
                            if i + 1 < args.len() {
                                output_name = Some(args[i + 1].clone());
                                i += 2;
                            } else {
                                return Err("missing argument for --name");
                            }
                        }
                        _ => {
                            file_paths.push(args[i].clone());
                            i += 1;
                        }
                    }
                }

                if file_paths.len() < 2 {
                    return Err(
                        "cascade requires at least 2 files, e.g. `touchstone cascade file1 file2`",
                    );
                }

                let mut networks = Vec::new();
                for path in file_paths.iter() {
                    networks.push(Network::new(path.clone()));
                }

                let mut result = networks[0].clone();
                // Cascade remaining networks
                for network in networks.iter().skip(1) {
                    result = result * network.clone();
                }

                // Determine output path
                let output_s2p_path = if let Some(name) = output_name {
                    // If name is provided, use it.
                    // If it doesn't have an extension, add .s2p?
                    // Let's assume user provides full filename or we just use it as is.
                    // But we should probably ensure it ends in .s2p for consistency?
                    // The prompt says "output s2p file", so let's trust the user or append if missing?
                    // Let's just use it as is for now.
                    name
                } else {
                    // Default behavior: first file directory, "cascaded_result.html" (but we need s2p now)
                    let first_file = &file_paths[0];
                    let path = std::path::Path::new(first_file);
                    let parent = path.parent().unwrap_or(std::path::Path::new("."));
                    parent
                        .join("cascaded_result.s2p")
                        .to_string_lossy()
                        .to_string()
                };

                // Save S2P file
                if let Err(e) = result.save(&output_s2p_path) {
                    tracing::error!("Failed to save S2P file: {}", e);
                    // Continue to plot generation? Or return error?
                    // Let's return error.
                    return Err("Failed to save S2P file");
                }
                tracing::info!("Saved cascaded network to {}", output_s2p_path);

                // Generate plot
                // Plot should be named based on the output s2p file
                // e.g. output.s2p -> output.s2p.html
                let output_html_path = format!("{}.html", output_s2p_path);

                generate_plot(&[result], output_html_path.clone());
                open::plot(output_html_path);

                return Ok(Config {});
            }
            _ => {
                if args.len() > 2 {
                    return Err(
                        "too many arguments, expecting only 2, such as `touchstone filepath`",
                    );
                }
            }
        }

        // cargo run arg[1], such as cargo run files/ntwk1.s2p
        // touchstone arg[1], such as touchstone files/ntwk1.s2p
        let file_argument = args[1].clone();

        parse_plot_open_in_browser(file_argument.clone());

        Ok(Config {})
    }
}

/// Print the crate version to stdout.
pub fn print_version() {
    println!("touchstone {}", env!("CARGO_PKG_VERSION"));
}

/// Print a formatted error message to stdout.
pub fn print_error(error: &str) {
    const RED: &str = "\x1b[31m";
    const RESET: &str = "\x1b[0m";
    println!("{}Problem parsing arguments: {error}{}", RED, RESET);
}

/// Print usage help text to stdout.
pub fn print_help() {
    // ANSI color codes
    const BOLD: &str = "\x1b[1m";
    const CYAN: &str = "\x1b[36m";
    const GREEN: &str = "\x1b[32m";
    const YELLOW: &str = "\x1b[33m";
    const RESET: &str = "\x1b[0m";

    println!(
        "📡 Touchstone (s2p, etc.) file parser, plotter, and more - https://github.com/iancleary/touchstone{}",
        RESET
    );
    println!();
    println!("{}{}VERSION:{}", BOLD, YELLOW, RESET);
    println!("    {}{}{}", GREEN, env!("CARGO_PKG_VERSION"), RESET);
    println!();
    println!("{}{}USAGE:{}", BOLD, YELLOW, RESET);
    println!("    {} touchstone <FILE_PATH>{}", GREEN, RESET);
    println!("    {} touchstone <DIRECTORY_PATH>{}", GREEN, RESET);
    println!(
        "    {} touchstone cascade <FILE_1> <FILE_2> ... [--name <OUTPUT_NAME>]{}",
        GREEN, RESET
    );
    println!();
    println!("     FILE_PATH: path to a s2p file");
    println!("     DIRECTORY_PATH: path to a directory containing s2p files");
    println!();
    println!("     The s2p file(s) are parsed and an interactive plot (html file and js/ folder) ");
    println!("     is created next to the source file(s).");
    println!();
    println!("{}{}OPTIONS:{}", BOLD, YELLOW, RESET);
    println!(
        "    {}  -v, --version{}{}    Print version information",
        GREEN, RESET, RESET
    );
    println!(
        "    {}  -h, --help{}{}       Print help information",
        GREEN, RESET, RESET
    );
    println!();
    println!("{}{}EXAMPLES:{}", BOLD, YELLOW, RESET);
    println!("    {} # Single file (Relative path){}", CYAN, RESET);
    println!("    {} touchstone files/measurements.s2p{}", GREEN, RESET);
    println!();
    println!(
        "    {} # Directory (Plot all files in folder){}",
        CYAN, RESET
    );
    println!("    {} touchstone files/data_folder{}", GREEN, RESET);
    println!();
    println!("    {} # Cascade two networks{}", CYAN, RESET);
    println!(
        "    {} touchstone cascade ntwk1.s2p ntwk2.s2p{}",
        GREEN, RESET
    );
    println!();
    println!("    {} # Cascade with custom output name{}", CYAN, RESET);
    println!(
        "    {} touchstone cascade ntwk1.s2p ntwk2.s2p --name result.s2p{}",
        GREEN, RESET
    );
    println!();
    println!("    {} # Bare filename{}", CYAN, RESET);
    println!("    {} touchstone measurement.s2p{}", GREEN, RESET);
    println!();
    println!("    {} # Windows absolute path{}", CYAN, RESET);
    println!(
        "    {} touchstone C:\\Users\\data\\measurements.s2p{}",
        GREEN, RESET
    );
    println!();
    println!("    {} # Windows UNC path (network path){}", CYAN, RESET);
    println!(
        "    {} touchstone \\\\server\\mount\\folder\\measurement.s2p{}",
        GREEN, RESET
    );
    println!();
    println!("    {} # Unix absolute path{}", CYAN, RESET);
    println!(
        "    {} touchstone /home/user/measurements.s2p{}",
        GREEN, RESET
    );
}

fn generate_plot(networks: &[Network], file_path_plot: String) {
    // creates a html file from network
    plot::generate_plot_from_networks(networks, &file_path_plot).unwrap();
    tracing::info!("Plot HTML generated at {}", file_path_plot);
}

fn parse_plot_open_in_browser(file_path: String) {
    let path = std::path::Path::new(&file_path);
    let mut networks = Vec::new();

    if path.is_dir() {
        tracing::info!("Directory detected: {}", file_path);
        tracing::debug!("Plotting all valid network files in directory");
        // Iterate over files in directory
        if let Ok(entries) = std::fs::read_dir(path) {
            for entry in entries.flatten() {
                let path = entry.path();
                if path.is_file() {
                    if let Some(extension) = path.extension() {
                        let ext_str = extension.to_string_lossy().to_lowercase();
                        // Check for s2p, s1p, etc. (s*p)
                        if ext_str.starts_with('s') && ext_str.ends_with('p') && ext_str.len() == 3
                        {
                            tracing::debug!("Found network file: {:?}", path);
                            networks.push(Network::new(path.to_string_lossy().to_string()));
                        }
                    }
                }
            }
        }
        if networks.is_empty() {
            tracing::warn!("No valid network files found in directory.");
        }
        // Output HTML in the directory
        let output_html_path = path
            .join("combined_plot.html")
            .to_string_lossy()
            .to_string();
        generate_plot(&networks, output_html_path.clone());
        open::plot(output_html_path.clone());
    } else {
        // Single file
        tracing::info!("Single file detected: {}", file_path);

        let s2p = Network::new(file_path.clone());
        networks.push(s2p);

        let file_path_config: file_operations::FilePathConfig =
            file_operations::get_file_path_config(&file_path);

        // absolute path, append .html, remove woindows UNC Prefix if present
        // relative path with separators, just append .hmtl
        // bare_filename, prepend ./ and append .html
        // absolute path, append .html, remove woindows UNC Prefix if present
        // relative path with separators, just append .hmtl
        // bare_filename, prepend ./ and append .html
        let output_html_path = if file_path_config.absolute_path {
            let mut file_path_html = format!("{}.html", &file_path);
            // Remove the UNC prefix on Windows if present
            if cfg!(target_os = "windows") && file_path_html.starts_with(r"\\?\") {
                file_path_html = file_path_html[4..].to_string();
            }
            file_path_html
        } else if file_path_config.relative_path_with_separators {
            format!("{}.html", &file_path)
        } else if file_path_config.bare_filename {
            format!("./{}.html", &file_path)
        } else {
            panic!(
                "file_path_config must have one true value: {:?}",
                file_path_config
            );
        };
        generate_plot(&networks, output_html_path.clone());
        open::plot(output_html_path.clone());
    }
}

#[cfg(test)]
mod tests {
    use std::fs;

    use super::*;
    use std::path::PathBuf;

    fn setup_test_dir(name: &str) -> PathBuf {
        let mut path = std::env::temp_dir();
        path.push("touchstone_tests");
        path.push(name);
        path.push(format!(
            "{}",
            std::time::SystemTime::now()
                .duration_since(std::time::UNIX_EPOCH)
                .unwrap()
                .as_nanos()
        ));
        std::fs::create_dir_all(&path).unwrap();
        path
    }

    #[test]
    fn test_config_build() {
        let test_dir = setup_test_dir("test_config_build");
        let s2p_path = test_dir.join("test_cli_config_build.s2p");
        fs::copy("files/test_cli/test_cli_config_build.s2p", &s2p_path).unwrap();

        let args = vec![
            String::from("program_name"),
            s2p_path.to_str().unwrap().to_string(),
        ];
        let _cli_run = Config::run(&args).unwrap();

        // Cleanup is optional as it's in temp dir, but good practice if we want to check it doesn't fail
        // let _remove_file = fs::remove_file(s2p_path.with_extension("s2p.html"));
        // let _remove_js_folder = fs::remove_dir_all(test_dir.join("js"));
    }

    #[test]
    fn test_config_build_not_enough_args() {
        let args = vec![String::from("program_name")];
        let result = Config::run(&args);
        assert!(result.is_err());
    }

    #[test]
    fn test_help_flag() {
        // Help flag test - verifies the flag is recognized
        // Note: In actual execution, this would exit the process
        // This test just documents the expected behavior
        let help_flags = vec!["--help", "-h"];
        for flag in help_flags {
            assert!(flag == "--help" || flag == "-h");
        }
    }

    #[test]
    fn test_version_flag() {
        // Version flag test - verifies the flag is recognized
        // Note: In actual execution, this would exit the process
        // This test just documents the expected behavior
        let version_flags = vec!["--version", "-v"];
        for flag in version_flags {
            assert!(flag == "--version" || flag == "-v");
        }
    }

    #[test]
    fn test_version_output_format() {
        // Test that version string is in correct format
        let version = env!("CARGO_PKG_VERSION");
        assert!(!version.is_empty());
        // Version should be in format X.Y.Z
        let parts: Vec<&str> = version.split('.').collect();
        assert_eq!(parts.len(), 3, "Version should be in X.Y.Z format");
    }

    #[test]
    fn test_run_function() {
        // test relative file
        let test_dir_rel = setup_test_dir("test_run_function_rel");
        // Create a "files" subdir to match the relative path structure expected if needed,
        // or just use the file in the temp dir.
        // The original test used "files/test_cli_run_relative_path.s2p".
        // parse_plot_open_in_browser handles relative paths.

        let s2p_path_rel = test_dir_rel.join("test_cli_run_relative_path.s2p");
        fs::copy(
            "files/test_cli/test_cli_run_relative_path.s2p",
            &s2p_path_rel,
        )
        .unwrap();

        parse_plot_open_in_browser(s2p_path_rel.to_str().unwrap().to_string());
        // Output should be next to it
        assert!(s2p_path_rel.with_extension("s2p.html").exists());
        assert!(test_dir_rel.join("js").exists());

        // test bare filename
        // This MUST run in CWD because we pass a bare filename.
        // We can't easily isolate this without changing CWD.
        // But since we moved other tests out of "files/", this test (using root) shouldn't conflict
        // with them, UNLESS another test uses root.
        // The only other test using root is this one.
        // So we keep it as is, but maybe add a lock if we add more root tests.

        let _bare_filename_copy = fs::copy(
            "files/test_cli/test_cli_run_bare_filename.s2p",
            "test_cli_run_bare_filename.s2p",
        );
        let bare_filename = String::from("test_cli_run_bare_filename.s2p");
        parse_plot_open_in_browser(bare_filename);
        let _bare_filename_remove_file_s2p = fs::remove_file("test_cli_run_bare_filename.s2p");
        let _bare_filename_remove_file_html =
            fs::remove_file("test_cli_run_bare_filename.s2p.html");
        let _bare_filename_remove_dir = fs::remove_dir_all("js");

        // test absolute path
        let test_dir_abs = setup_test_dir("test_run_function_abs");
        let s2p_path_abs = test_dir_abs.join("test_cli_run_absolute_path.s2p");
        fs::copy(
            "files/test_cli/test_cli_run_absolute_path.s2p",
            &s2p_path_abs,
        )
        .unwrap();

        let path_buf = std::fs::canonicalize(&s2p_path_abs).unwrap();
        let absolute_path: String = path_buf.to_string_lossy().to_string();

        parse_plot_open_in_browser(absolute_path);

        assert!(s2p_path_abs.with_extension("s2p.html").exists());
        assert!(test_dir_abs.join("js").exists());
    }

    #[test]
    fn test_cascade_command() {
        let test_dir = setup_test_dir("test_cascade_command");
        let s2p1 = test_dir.join("ntwk1.s2p");
        let s2p2 = test_dir.join("ntwk2.s2p");

        fs::copy("files/ntwk1.s2p", &s2p1).unwrap();
        fs::copy("files/ntwk2.s2p", &s2p2).unwrap();

        // Test default output
        let args = vec![
            String::from("program_name"),
            String::from("cascade"),
            s2p1.to_str().unwrap().to_string(),
            s2p2.to_str().unwrap().to_string(),
        ];

        let _cli_run = Config::run(&args).unwrap();

        let expected_output_s2p = test_dir.join("cascaded_result.s2p");
        let expected_output_html = test_dir.join("cascaded_result.s2p.html");
        assert!(expected_output_s2p.exists());
        assert!(expected_output_html.exists());
        assert!(test_dir.join("js").exists());

        // Test with --name
        let output_name = test_dir.join("custom_output.s2p");
        let args_named = vec![
            String::from("program_name"),
            String::from("cascade"),
            s2p1.to_str().unwrap().to_string(),
            s2p2.to_str().unwrap().to_string(),
            String::from("--name"),
            output_name.to_str().unwrap().to_string(),
        ];

        let _cli_run_named = Config::run(&args_named).unwrap();

        assert!(output_name.exists());
        assert!(output_name.with_extension("s2p.html").exists());
    }

    #[test]
    fn test_cascade_not_enough_args() {
        let args = vec![
            String::from("program_name"),
            String::from("cascade"),
            String::from("file1"),
        ];
        let result = Config::run(&args);
        assert!(result.is_err());
    }

    #[test]
    fn test_directory_plot() {
        let test_dir = setup_test_dir("test_directory_plot");
        let s2p1 = test_dir.join("ntwk1.s2p");
        let s2p2 = test_dir.join("ntwk2.s2p");
        // Add a non-s2p file to ensure it's ignored
        let txt_file = test_dir.join("readme.txt");

        fs::copy("files/ntwk1.s2p", &s2p1).unwrap();
        fs::copy("files/ntwk2.s2p", &s2p2).unwrap();
        fs::write(&txt_file, "ignore me").unwrap();

        // Pass the directory path
        parse_plot_open_in_browser(test_dir.to_str().unwrap().to_string());

        // Check for combined output
        let expected_output = test_dir.join("combined_plot.html");
        assert!(expected_output.exists());
        assert!(test_dir.join("js").exists());

        // Verify content contains both network names (simple check)
        let content = fs::read_to_string(&expected_output).unwrap();
        // ntwk1.s2p and ntwk2.s2p should be in the content (as part of network names)
        // Note: Network::new uses the filename as name by default usually?
        // Let's check if "ntwk1.s2p" is in the content.
        // The template injects network_names.
        assert!(content.contains("ntwk1.s2p"));
        assert!(content.contains("ntwk2.s2p"));
    }
}