cargo-e 0.3.2

e is for Example. A command-line tool for running and exploring source, examples, and binaries from Rust projects. It will run the first example, if no options are given.
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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
use crate::e_target::TargetKind;
use anyhow::{anyhow, Result};
use glob::glob;
use log::trace;
use std::error::Error;
use std::ffi::OsStr;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fs, io};
use toml::Value;

/// Locate the Cargo.toml by invoking `cargo locate-project --message-format plain`.
/// If `workspace` is true, the `--workspace` flag is added so that the manifest
/// for the workspace root is returned.
/// Locate the Cargo.toml for the project, preferring `src-tauri/Cargo.toml` if it exists.
pub fn locate_manifest(workspace: bool) -> Result<String, Box<dyn Error>> {
    // Attempt to locate the project manifest via Cargo
    let mut args = vec!["locate-project", "--message-format", "plain"];
    if workspace {
        args.push("--workspace");
    }

    let output = Command::new("cargo").args(&args).output()?;
    // Parse Cargo output if successful
    let manifest_from_cargo = if output.status.success() {
        let m = String::from_utf8_lossy(&output.stdout).trim().to_string();
        if !m.is_empty() {
            Some(m)
        } else {
            None
        }
    } else {
        None
    };

    // Fallback: prefer a src-tauri manifest if present
    let fallback_path = Path::new("src-tauri").join("Cargo.toml");
    if fallback_path.exists() {
        return Ok(fallback_path.to_string_lossy().into_owned());
    }

    // Otherwise, use the manifest Cargo found
    if let Some(m) = manifest_from_cargo {
        return Ok(m);
    }

    Err("No Cargo.toml found".into())
}

/// Parses the workspace manifest (in TOML format) to return a vector of workspace member names and
/// their corresponding manifest paths. The workspace manifest is expected to have a \[workspace\]
/// table with a "members" array. Each member is joined with the workspace root directory.
pub fn collect_workspace_members(
    workspace_manifest: &str,
) -> Result<Vec<(String, PathBuf)>, Box<dyn Error>> {
    let manifest_path = Path::new(workspace_manifest);
    let workspace_root = manifest_path
        .parent()
        .ok_or("Cannot determine workspace root")?;
    let manifest_contents = fs::read_to_string(workspace_manifest)?;
    let value: Value = manifest_contents.parse::<Value>()?;
    let mut members = Vec::new();

    if let Some(ws) = value.get("workspace") {
        if let Some(member_array) = ws.get("members").and_then(|v| v.as_array()) {
            for member in member_array {
                if let Some(member_str) = member.as_str() {
                    // If the member path ends with "/*", handle it as a glob pattern
                    if member_str.ends_with("/*") {
                        let dir_pattern = member_str.trim_end_matches("/*");
                        let glob_pattern = format!("{}/**/Cargo.toml", dir_pattern);

                        // Use the glob crate to match the pattern and iterate over the results
                        for entry in glob(&glob_pattern)? {
                            match entry {
                                Ok(path) => {
                                    // Check if it exists and if the path is a valid Cargo.toml
                                    if path.exists()
                                        && path.file_name().and_then(|name| name.to_str())
                                            == Some("Cargo.toml")
                                    {
                                        let member_name = path
                                            .parent()
                                            .and_then(|p| p.file_name())
                                            .map(|p| p.to_string_lossy().into_owned())
                                            .unwrap_or_else(|| "unknown".to_string());
                                        members.push((member_name, path));
                                    }
                                }
                                Err(_) => continue, // Ignore any errors from glob
                            }
                        }
                    } else {
                        // Strip any trailing glob patterns like "*".
                        let member_clean = if member_str.contains('*') {
                            member_str.trim_end_matches("/*")
                        } else {
                            member_str
                        };
                        let member_path = workspace_root.join(member_clean);
                        let member_manifest = member_path.join("Cargo.toml");
                        if member_manifest.exists() {
                            members.push((member_clean.to_string(), member_manifest));
                        }
                    }
                }
            }
        }
    }
    Ok(members)
}

/// Checks whether the manifest at `manifest_path` would trigger the workspace error.
/// If so, it patches the file by appending an empty `[workspace]` table, returning the original content.
/// Otherwise, returns None.
#[allow(dead_code)]
pub(crate) fn maybe_patch_manifest_for_run(manifest_path: &Path) -> Result<Option<String>> {
    // Run a lightweight command (cargo metadata) to see if the manifest is affected.
    let output = Command::new("cargo")
        .args(["metadata", "--no-deps", "--manifest-path"])
        .arg(manifest_path)
        .output()?;
    let stderr_str = String::from_utf8_lossy(&output.stderr);
    let workspace_error_marker = "current package believes it's in a workspace when it's not:";

    if stderr_str.contains(workspace_error_marker) {
        // Read the original manifest content.
        let original = fs::read_to_string(manifest_path)?;
        // If not already opting out, patch it.
        if !original.contains("[workspace]") {
            let patched = format!("{}\n[workspace]\n", original);
            fs::write(manifest_path, &patched)?;
            return Ok(Some(original));
        }
    }
    Ok(None)
}

/// Search upward from the current directory for Cargo.toml, with a fallback to `src-tauri/Cargo.toml`.
pub fn find_manifest_dir() -> io::Result<PathBuf> {
    let mut dir = std::env::current_dir()?;
    loop {
        if dir.join("Cargo.toml").exists() {
            return Ok(dir);
        }
        if !dir.pop() {
            break;
        }
    }

    // Fallback: prefer a src-tauri manifest if present
    let fallback = Path::new("src-tauri").join("Cargo.toml");
    if fallback.exists() {
        return Ok(fallback
            .parent()
            .expect("src-tauri/Cargo.toml has no parent")
            .to_path_buf());
    }

    Err(io::Error::new(
        io::ErrorKind::NotFound,
        "Could not locate Cargo.toml in the current or parent directories.",
    ))
}

/// Searches upward from the given starting directory for a Cargo.toml file,
/// with a fallback to `src-tauri/Cargo.toml` if none is found above.
pub fn find_manifest_dir_from(start: &Path) -> io::Result<PathBuf> {
    let mut dir = start.to_path_buf();
    loop {
        trace!("Checking {}", dir.join("Cargo.toml").display());
        if dir.join("Cargo.toml").exists() {
            return Ok(dir);
        }
        if !dir.pop() {
            break;
        }
    }

    // Fallback: prefer a src-tauri manifest if present
    let fallback = Path::new("src-tauri").join("Cargo.toml");
    if fallback.exists() {
        return Ok(fallback
            .parent()
            .expect("src-tauri/Cargo.toml has no parent")
            .to_path_buf());
    }

    Err(io::Error::new(
        io::ErrorKind::NotFound,
        "Could not locate Cargo.toml in the current or parent directories.",
    ))
}

/// Returns a comma‑separated list of required features for a given target,
/// based on its manifest, target kind, and name. If the target is not found
/// in the given manifest and the manifest is a workspace, its members are searched.
pub fn get_required_features_from_manifest(
    manifest_path: &Path,
    kind: &TargetKind,
    target_name: &str,
) -> Option<String> {
    trace!(
        "Searching for required features in manifest: {}",
        manifest_path.display()
    );
    // Read and parse the manifest file.
    let content = fs::read_to_string(manifest_path).ok()?;
    let value: Value = content.parse().ok()?;

    // Map the TargetKind to the corresponding section in the manifest.
    let section = kind.section_name();
    if section.is_empty() {
        return None;
    }
    // Look for the target in the specified section.
    if let Some(targets) = value.get(section).and_then(|v| v.as_array()) {
        for entry in targets {
            if let Some(name) = entry.get("name").and_then(|v| v.as_str()) {
                if name == target_name {
                    if let Some(req_feats) =
                        entry.get("required-features").and_then(|v| v.as_array())
                    {
                        let feats = req_feats
                            .iter()
                            .filter_map(|f| f.as_str())
                            .collect::<Vec<_>>()
                            .join(",");
                        if !feats.is_empty() {
                            return Some(feats);
                        }
                    }
                }
            }
        }
    }

    // If not found and the manifest has a [workspace] table, check each workspace member.
    if value.get("workspace").is_some() {
        // Convert the manifest_path to a &str.
        if let Some(manifest_str) = manifest_path.to_str() {
            if let Ok(members) = collect_workspace_members(manifest_str) {
                for (member_name, member_manifest_path) in members {
                    trace!(
                        "Checking workspace {}: {}",
                        member_name,
                        member_manifest_path.display()
                    );
                    // Skip if member manifest is the same as the root manifest (prevents infinite recursion)
                    let is_same_manifest = member_manifest_path.canonicalize().ok()
                        == manifest_path.canonicalize().ok();
                    if member_name == "." || is_same_manifest {
                        trace!(
                            "Skipping current workspace member: {}",
                            member_manifest_path.display()
                        );
                        continue;
                    }
                    if let Some(feats) = get_required_features_from_manifest(
                        &member_manifest_path,
                        kind,
                        target_name,
                    ) {
                        return Some(feats);
                    }
                }
            }
        }
    }
    None
}

pub fn find_candidate_name(
    manifest_toml: &Value,
    table_key: &str,
    candidate: &Path,
    manifest_path: &Path,
) -> Option<String> {
    manifest_toml
        .get(table_key)
        .and_then(|v| v.as_array())
        .and_then(|entries| {
            let manifest_parent = manifest_path.parent().unwrap_or_else(|| Path::new(""));
            let candidate_abs = std::fs::canonicalize(candidate).ok();

            // Precompute the file‑stem (filename without extension), if any
            let candidate_stem = candidate.file_stem().and_then(|s| s.to_str());

            // 1) Try explicit path match
            let explicit = entries.iter().find_map(|entry| {
                entry.get("path").and_then(|p| p.as_str()).and_then(|rel| {
                    entry.get("name").and_then(|n| n.as_str()).and_then(|name| {
                        candidate_abs.as_ref().and_then(|abs| {
                            std::fs::canonicalize(manifest_parent.join(rel))
                                .ok()
                                .and_then(|expected| {
                                    if expected == *abs {
                                        Some(name.to_string())
                                    } else {
                                        None
                                    }
                                })
                        })
                    })
                })
            });

            // 2) If no explicit match, try matching the file‑stem to a name
            let stem_match = explicit.clone().or_else(|| {
                candidate_stem.and_then(|stem| {
                    entries.iter().find_map(|entry| {
                        entry.get("name").and_then(|n| n.as_str()).and_then(|name| {
                            if name == stem {
                                Some(name.to_string())
                            } else {
                                None
                            }
                        })
                    })
                })
            });

            // Only explicit or stem match—no default fallback
            stem_match
        })
}

// /// Finds a candidate name from the manifest using the specified table key.
// /// For example, if `table_key` is "bin", it checks for `[[bin]]` entries; if it is "example",
// /// it checks for `[[example]]` entries.
// pub fn find_candidate_name(
//     manifest_toml: &Value,
//     table_key: &str,
//     candidate: &Path,
//     manifest_path: &Path,
// ) -> Option<String> {
//     manifest_toml
//         .get(table_key)
//         .and_then(|v| v.as_array())
//         .map(|entries| {
//             let manifest_parent = manifest_path
//                 .parent()
//                 .unwrap_or_else(|| std::path::Path::new(""));
//             let candidate_abs = std::fs::canonicalize(candidate).ok();
//             // First, try to find an explicit match using the provided "path"
//             entries
//                 .iter()
//                 .find_map(|entry| {
//                     entry
//                         .get("path")
//                         .and_then(|p| p.as_str())
//                         .and_then(|rel_path_str| {
//                             entry.get("name").and_then(|n| n.as_str()).and_then(|name| {
//                                 candidate_abs.as_ref().and_then(|candidate_abs| {
//                                     std::fs::canonicalize(manifest_parent.join(rel_path_str))
//                                         .ok()
//                                         .and_then(|expected_path| {
//                                             trace!(
//                                                 "\nCandidate: {}\nExpected: {:?}\nActual: {:?}",
//                                                 candidate.display(),
//                                                 expected_path,
//                                                 candidate_abs
//                                             );
//                                             if expected_path == *candidate_abs {
//                                                 trace!(
//                                                     "{} Found matching {} with name: {}",
//                                                     candidate.display(),
//                                                     table_key,
//                                                     name
//                                                 );
//                                                 Some(name.to_string())
//                                             } else {
//                                                 None
//                                             }
//                                         })
//                                 })
//                             })
//                         })
//                 })
//                 // If no explicit match is found, use the last entry with no "path" as the default
//                 .or_else(|| {
//                     entries
//                         .iter()
//                         .filter(|entry| entry.get("path").is_none())
//                         .filter_map(|entry| {
//                             entry.get("name").and_then(|n| n.as_str()).map(String::from)
//                         })
//                         .last()
//                 })
//         })
//         .flatten()
// }

/// Returns the runnable targets (bins, examples, benches, and tests) from the Cargo.toml.
/// For tests, it uses `scan_tests_directory` to list integration test files.
use crate::e_target::{CargoTarget, TargetOrigin};

// /// Returns the runnable targets (bins, examples, benches, and tests) from the Cargo.toml.
// /// For examples and tests, it also scans the corresponding directories for files that contain
// /// a main function (for examples) and returns their file paths in the target origin.
// pub fn get_runnable_targets(
//     manifest_path: &Path,
// ) -> Result<(Vec<CargoTarget>, Vec<CargoTarget>, Vec<CargoTarget>, Vec<CargoTarget>), Box<dyn Error>> {
//     // Read and parse the Cargo.toml manifest.
//     let content = fs::read_to_string(manifest_path)?;
//     let value: Value = content.parse()?;

//     // Determine the project root from the manifest's parent directory.
//     let project_root = manifest_path.parent().ok_or("Unable to determine project root")?;

//     // Determine if the manifest is inside an "examples" folder.
//     let is_extended = project_root
//         .parent()
//         .and_then(|p| p.file_name())
//         .map(|s| s.to_string_lossy().eq_ignore_ascii_case("examples"))
//         .unwrap_or(false);

//     // For targets discovered in an extended (examples) context,
//     // use ExtendedExample instead of Binary or Example.
//     let mut bin_kind = if is_extended { TargetKind::ExtendedBinary } else { TargetKind::Binary };
//     let example_kind = if is_extended { TargetKind::ExtendedExample } else { TargetKind::Example };

//     // --- Binaries ---
//     // Start with any explicit [[bin]] targets defined in the manifest.
//     let mut bins: Vec<CargoTarget> = value
//     .get("bin")
//     .and_then(|v| v.as_array())
//     .map(|arr| {
//         arr.iter()
//             .filter_map(|entry| {
//                 // Get the target name.
//                 let name = entry.get("name").and_then(|n| n.as_str())?;
//                 // Use the "path" from the TOML if provided.
//                 let relative_path = entry.get("path").and_then(|p| p.as_str()).unwrap_or("");
//                 Some((name, relative_path))
//             })
//             .map(|(name, relative_path)| {
//                 // Compute the full path to the binary file.
//                 let full_path = if !relative_path.is_empty() {
//                     project_root.join(relative_path)
//                 } else {
//                     // Fallback: assume "src/{name}.rs" if no path is specified.
//                     project_root.join("src").join(format!("{}.rs", name))
//                 };
//                 // Check for Tauri and Dioxus configuration.
//                 let tauri_folder = project_root.join("src-tauri");
//                 let tauri_config = project_root.join("tauri.conf.json");
//                 let dioxus_config = project_root.join("Dioxus.toml");
//                 let mut target_kind = bin_kind; // by default, use bin_kind.
//                 if manifest_path.parent()
//                     .and_then(|p| p.file_name())
//                     .map(|s| s.to_string_lossy().eq_ignore_ascii_case("src-tauri"))
//                     .unwrap_or(false)
//                 {
//                     target_kind = TargetKind::ManifestTauri;
//                 } else if tauri_folder.exists() || tauri_config.exists() {
//                     target_kind = TargetKind::ManifestTauri;
//                 } else if dioxus_config.exists() {
//                     target_kind = TargetKind::ManifestDioxus;
//                 }
//             // Read the file's contents.
//             let file_contents = fs::read_to_string(&full_path).unwrap_or_default();
//             // Determine the target kind based on the file contents.
//             target_kind = if file_contents.contains("LaunchBuilder::new") ||  file_contents.contains("dioxus::LaunchBuilder") || file_contents.contains("dioxus::launch") {
//                 TargetKind::ManifestDioxus
//             } else {
//                 target_kind
//             };

//                 CargoTarget {
//                     name: name.to_string(),
//                     display_name: name.to_string(),
//                     // We keep the manifest_path as the package's manifest.
//                     manifest_path: manifest_path.to_path_buf(),
//                     kind: target_kind,
//                     extended: is_extended,
//                     origin: Some(TargetOrigin::SingleFile(full_path)),
//                 }
//             })
//             .collect::<Vec<_>>()
//     })
//     .unwrap_or_default();

//     // Check for the default binary.
//     // First try "src/main.rs", then fallback to "main.rs" at the project root.
//     let default_bin_path = if project_root.join("src").join("main.rs").exists() {
//         project_root.join("src").join("main.rs")
//     } else if project_root.join("main.rs").exists() {
//         project_root.join("main.rs")
//     } else {
//         PathBuf::new()
//     };
//     if !default_bin_path.as_os_str().is_empty() {
//         if let Some(pkg) = value.get("package") {
//             if let Some(pkg_name) = pkg.get("name").and_then(|v| v.as_str()) {
//             // Determine the default target kind.
//             let tauri_folder = project_root.join("src-tauri");
//             let tauri_config = project_root.join("tauri.conf.json");
//             let dioxus_config = project_root.join("Dioxus.toml");

//             // Start with fallback kind.
//             let mut default_kind = bin_kind;

//             // If the parent directory of the manifest is named "src-tauri", use ManifestTauri.
//             if manifest_path.parent()
//                 .and_then(|p| p.file_name())
//                 .map(|s| s.to_string_lossy().eq_ignore_ascii_case("src-tauri"))
//                 .unwrap_or(false)
//             {
//                 default_kind = TargetKind::ManifestTauri;
//             } else if tauri_folder.exists() || tauri_config.exists() {
//                 default_kind = TargetKind::ManifestTauri;
//             } else if dioxus_config.exists() {
//                 default_kind = TargetKind::ManifestDioxus;
//             }

//             // Read the file's contents.
//             let file_contents = fs::read_to_string(&default_bin_path).unwrap_or_default();
//             // Determine the target kind based on the file contents.
//             default_kind = if file_contents.contains("LaunchBuilder::new") || file_contents.contains("dioxus::LaunchBuilder") || file_contents.contains("dioxus::launch") {
//                 TargetKind::ManifestDioxus
//             } else {
//                 default_kind
//             };

//             // Add the default target if it isn’t already in the bins vector.
//             if !bins.iter().any(|t| t.name == pkg_name) {
//                 bins.push(CargoTarget {
//                     name: pkg_name.to_string(),
//                     display_name: pkg_name.to_string(),
//                     manifest_path: manifest_path.to_path_buf(),
//                     kind: default_kind,
//                     extended: is_extended,
//                     origin: Some(TargetOrigin::DefaultBinary(default_bin_path)),
//                 });
//             }
//             }
//         }
//     }

//     // Also scan the src/bin directory for additional binary targets.
//     let bin_dir = project_root.join("src").join("bin");
//     if bin_dir.exists() && bin_dir.is_dir() {
//         for entry in fs::read_dir(&bin_dir)? {
//             let entry = entry?;
//             let path = entry.path();
//             if path.is_file() {
//                 if let Some(ext) = path.extension() {
//                     if ext == "rs" {
//                         if let Some(stem) = path.file_stem() {
//                             let bin_name = stem.to_string_lossy().to_string();

//                             // Read the file's contents.
//                             let file_contents = fs::read_to_string(&path).unwrap_or_default();
//                             // Determine the target kind based on the file contents.
//                             bin_kind = if file_contents.contains("LaunchBuilder::new") || file_contents.contains("dioxus::LaunchBuilder") || file_contents.contains("dioxus::launch") {
//                                 TargetKind::ManifestDioxus
//                             } else {
//                                 bin_kind
//                             };

//                             // Only add it if a target with the same name isn't already present.
//                             if !bins.iter().any(|t| t.name == bin_name) {
//                                 bins.push(CargoTarget {
//                                     name: bin_name.clone(),
//                                     display_name: bin_name,
//                                     manifest_path: manifest_path.to_path_buf(),
//                                     kind: bin_kind,
//                                     extended: is_extended,
//                                     origin: Some(TargetOrigin::SingleFile(path)),
//                                 });
//                             }
//                         }
//                     }
//                 }
//             }
//         }
//     }

//     // --- Examples ---
//     // Get any explicit [[example]] targets from the manifest.
//     let mut examples: Vec<CargoTarget> = value
//     .get("example")
//     .and_then(|v| v.as_array())
//     .map(|arr| {
//         arr.iter()
//             .filter_map(|entry| {
//                 // Get the target name.
//                 let name = entry.get("name").and_then(|n| n.as_str())?;
//                 // Use the "path" field if provided; otherwise assume "examples/{name}.rs"
//                 let relative_path_str = if let Some(p) = entry.get("path").and_then(|p| p.as_str()) {
//                     p.to_string()
//                 } else {
//                     format!("examples/{}.rs", name)
//                 };
//                 let full_path = project_root.join(&relative_path_str);
//                 // Read the file's contents (if the file exists).
//                 let file_contents = fs::read_to_string(&full_path).unwrap_or_default();
//                 // Start with the default example kind.
//                 let mut target_kind = example_kind;
//                 // Check for Dioxus markers.
//                 if file_contents.contains("dioxus::LaunchBuilder")
//                     || file_contents.contains("dioxus::launch")
//                 {
//                     target_kind = TargetKind::ManifestDioxusExample;
//                 } else {
//                     // Check for Tauri configuration in the workspace root.
//                     let tauri_folder = project_root.join("src-tauri");
//                     let tauri_config = project_root.join("tauri.conf.json");
//                     if tauri_folder.exists() || tauri_config.exists() {
//                         target_kind = TargetKind::ManifestTauri;
//                     }
//                 }
//                 Some(CargoTarget {
//                     name: name.to_string(),
//                     display_name: name.to_string(),
//                     manifest_path: manifest_path.to_path_buf(),
//                     kind: target_kind,
//                     extended: is_extended,
//                     origin: Some(TargetOrigin::SingleFile(full_path)),
//                 })
//             })
//             .collect::<Vec<_>>()
//     })
//     .unwrap_or_default();

//     // Scan the examples/ directory for example targets.
//     let scanned_examples = crate::e_discovery::scan_examples_directory(manifest_path)?;
//     for ex in scanned_examples {
//         if !examples.iter().any(|t| t.name == ex.name) {
//             // If our manifest is inside an examples directory, mark as extended.
//             let mut target = ex;
//             if is_extended {
//                 target.kind = TargetKind::ExtendedExample;
//                 target.extended = true;
//             }
//             examples.push(target);
//         }
//     }

//     // --- Benches ---
//     let benches: Vec<CargoTarget> = value
//         .get("bench")
//         .and_then(|v| v.as_array())
//         .map(|arr| {
//             arr.iter()
//                 .filter_map(|entry| entry.get("name").and_then(|n| n.as_str()))
//                 .map(|name| CargoTarget {
//                     name: name.to_string(),
//                     display_name: name.to_string(),
//                     manifest_path: manifest_path.to_path_buf(),
//                     kind: TargetKind::Bench,
//                     extended: false,
//                     origin: None,
//                 })
//                 .collect::<Vec<_>>()
//         })
//         .unwrap_or_default();

//     // --- Tests ---
//     let scanned_tests = crate::e_discovery::scan_tests_directory(manifest_path)?;
//     let mut tests: Vec<CargoTarget> = Vec::new();
//     for test_name in scanned_tests {
//         let test_path = project_root.join("tests").join(format!("{}.rs", test_name));
//         tests.push(CargoTarget {
//             name: test_name.clone(),
//             display_name: test_name,
//             manifest_path: manifest_path.to_path_buf(),
//             kind: TargetKind::Test,
//             extended: false,
//             origin: Some(TargetOrigin::SingleFile(test_path)),
//         });
//     }

//     Ok((bins, examples, benches, tests))
// }

/// Returns the runnable targets (bins, examples, benches, and tests) from the Cargo.toml.
/// This version uses the new associated constructors on CargoTarget:
/// - `from_source_file`: builds a target from a candidate file (that contains "fn main" and/or special markers).
/// - `from_folder`: builds a target by scanning a folder for a candidate source file.
#[allow(clippy::type_complexity)]
pub fn get_runnable_targets(
    manifest_path: &Path,
) -> anyhow::Result<(
    Vec<CargoTarget>,
    Vec<CargoTarget>,
    Vec<CargoTarget>,
    Vec<CargoTarget>,
)> {
    // Read and parse the Cargo.toml manifest.
    let content = fs::read_to_string(manifest_path)?;
    let value: Value = content.parse()?;

    // Determine the project root from the manifest's parent directory.
    let project_root = manifest_path
        .parent()
        .ok_or(anyhow!("Unable to determine project root"))?;

    // Determine if the manifest is inside an "examples" folder.
    let is_extended = project_root
        .parent()
        .and_then(|p| p.file_name())
        .map(|s| s.to_string_lossy().eq_ignore_ascii_case("examples"))
        .unwrap_or(false);

    // --- Binaries ---
    let mut bins = Vec::new();
    if let Some(bin_array) = value.get("bin").and_then(|v| v.as_array()) {
        for entry in bin_array {
            if let Some(name) = entry.get("name").and_then(|v| v.as_str()) {
                // If a "path" field is provided, use it.
                let target_opt = if let Some(path_str) = entry.get("path").and_then(|v| v.as_str())
                {
                    let candidate = project_root.join(path_str);
                    if candidate.is_file() {
                        CargoTarget::from_source_file(
                            OsStr::new(name),
                            &candidate,
                            manifest_path,
                            false,
                            false,
                        )
                    } else if candidate.is_dir() {
                        CargoTarget::from_folder(&candidate, manifest_path, false, true)
                    } else {
                        None
                    }
                } else {
                    // Fallback: assume the file is at "src/{name}.rs"
                    let candidate = project_root.join("src").join(format!("{}.rs", name));
                    CargoTarget::from_source_file(
                        OsStr::new(name),
                        &candidate,
                        manifest_path,
                        false,
                        false,
                    )
                };
                if let Some(target) = target_opt {
                    bins.push(target);
                }
            }
        }
    }

    // Default binary: if no explicit bin exists with the package name.
    if let Some(pkg) = value
        .get("package")
        .and_then(|v| v.get("name"))
        .and_then(|v| v.as_str())
    {
        if !bins.iter().any(|t| t.name == pkg) {
            // Candidate: try "src/main.rs", then "main.rs".
            let candidate = if project_root.join("src").join("main.rs").exists() {
                project_root.join("src").join("main.rs")
            } else if project_root.join("main.rs").exists() {
                project_root.join("main.rs")
            } else {
                PathBuf::new()
            };
            if !candidate.as_os_str().is_empty() {
                let candidate = fs::canonicalize(&candidate).unwrap_or(candidate.to_path_buf());
                if let Some(mut target) = CargoTarget::from_source_file(
                    OsStr::new(pkg),
                    &candidate,
                    manifest_path,
                    false,
                    false,
                ) {
                    // Mark this as a default binary.
                    // target.name = pkg.to_string();
                    // target.display_name = pkg.to_string();
                    target.origin = Some(TargetOrigin::DefaultBinary(candidate));
                    bins.push(target);
                }
            }
        }
    }

    // Also, scan the "src/bin" directory for additional binaries.
    let bin_dir = project_root.join("src").join("bin");
    if bin_dir.exists() && bin_dir.is_dir() {
        for entry in fs::read_dir(&bin_dir)? {
            let entry = entry?;
            let path = entry.path();
            if path.is_file() && path.extension().and_then(|s| s.to_str()) == Some("rs") {
                if let Some(stem) = path.file_stem() {
                    let name = stem.to_string_lossy().to_string();
                    if !bins.iter().any(|t| t.name == name) {
                        if let Some(target) =
                            CargoTarget::from_source_file(stem, &path, manifest_path, false, false)
                        {
                            bins.push(target);
                        }
                    }
                }
            }
        }
    }

    // --- Examples ---
    let mut examples = Vec::new();
    if let Some(example_array) = value.get("example").and_then(|v| v.as_array()) {
        for entry in example_array {
            if let Some(name) = entry.get("name").and_then(|v| v.as_str()) {
                let target_opt = if let Some(path_str) = entry.get("path").and_then(|v| v.as_str())
                {
                    let candidate = project_root.join(path_str);
                    CargoTarget::from_source_file(
                        OsStr::new(name),
                        &candidate,
                        manifest_path,
                        true,
                        false,
                    )
                } else {
                    let candidate = project_root.join(format!("examples/{}.rs", name));
                    CargoTarget::from_source_file(
                        OsStr::new(name),
                        &candidate,
                        manifest_path,
                        true,
                        false,
                    )
                };
                if let Some(target) = target_opt {
                    examples.push(target);
                }
            }
        }
    }
    // Scan the examples directory for additional example targets.
    let scanned_examples = crate::e_discovery::scan_examples_directory(manifest_path, "examples")?;
    for ex in scanned_examples {
        if !examples.iter().any(|t| t.name == ex.name) {
            let mut t = ex;
            if is_extended {
                t.kind = TargetKind::ExtendedExample;
                t.extended = true;
            }
            examples.push(t);
        }
    }
    let scanned_examples =
        crate::e_discovery::scan_examples_directory(manifest_path, "experiments")?;
    for ex in scanned_examples {
        if !examples.iter().any(|t| t.name == ex.name) {
            let mut t = ex;
            if is_extended {
                t.kind = TargetKind::ExtendedExample;
                t.extended = true;
            }
            examples.push(t);
        }
    }

    // --- Benches ---
    let mut benches = Vec::new();
    if let Some(bench_array) = value.get("bench").and_then(|v| v.as_array()) {
        for entry in bench_array {
            if let Some(name) = entry.get("name").and_then(|v| v.as_str()) {
                benches.push(CargoTarget {
                    name: name.to_string(),
                    display_name: name.to_string(),
                    manifest_path: manifest_path.to_path_buf(),
                    kind: TargetKind::Bench,
                    extended: false,
                    toml_specified: false,
                    origin: None,
                });
            }
        }
    }

    // --- Tests ---
    let mut tests = Vec::new();
    let scanned_tests = crate::e_discovery::scan_tests_directory(manifest_path)?;
    for test_name in scanned_tests {
        let candidate = project_root.join("tests").join(format!("{}.rs", test_name));
        tests.push(CargoTarget {
            name: test_name.clone(),
            display_name: test_name,
            manifest_path: manifest_path.to_path_buf(),
            kind: TargetKind::Test,
            extended: false,
            toml_specified: false,
            origin: Some(TargetOrigin::SingleFile(candidate)),
        });
    }

    Ok((bins, examples, benches, tests))
}