starknet-contract-verifier 0.4.4

Contract class verification tool that allows you to verify your starknet classes on a block explorer.
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
use camino::{Utf8Path, Utf8PathBuf};
use itertools::Itertools;
use log::debug;
use scarb_metadata::{Metadata, MetadataCommand, PackageMetadata};
use serde::Deserialize;
use std::{
    collections::{HashMap, HashSet},
    ffi::OsStr,
    fs,
    path::PathBuf,
};
use thiserror::Error;
use url::Url;
use walkdir::WalkDir;

#[derive(Debug, Error)]
pub enum Error {
    #[error("[E012] Invalid dependency path for '{name}': {path}\n\nSuggestions:\n  • Check that the path exists and is accessible\n  • Use relative paths from the current directory\n  • Verify the path format is correct\n  • Example: path:../my-dependency")]
    DependencyPath { name: String, path: String },

    #[error("[E013] Failed to read metadata for '{name}' at path: {path}\n\nSuggestions:\n  • Check that Scarb.toml exists at the specified path\n  • Verify the Scarb.toml file is valid\n  • Run 'scarb metadata' in the target directory to test\n  • Ensure scarb is installed and accessible")]
    MetadataError { name: String, path: PathBuf },

    #[error("[E014] Path contains invalid UTF-8 characters\n\nSuggestions:\n  • Use only ASCII characters in file paths\n  • Avoid special characters in directory names\n  • Check for hidden or control characters in the path")]
    Utf8(#[from] camino::FromPathBufError),

    #[error("[E025] Failed to parse TOML file '{path}': {error}\n\nSuggestions:\n  • Check TOML syntax is valid\n  • Verify file is not corrupted\n  • Use a TOML validator tool")]
    TomlParseError { path: String, error: String },

    #[error("[E026] I/O error reading file '{path}': {error}\n\nSuggestions:\n  • Check file exists and is readable\n  • Verify file permissions\n  • Ensure disk space is available")]
    IoError { path: String, error: String },

    #[error("[E027] Module not found: '{module}' from '{parent_file}'\n\nSuggestions:\n  • Check that the module file exists\n  • Verify module name spelling\n  • Ensure proper file structure (module.rs or module/mod.rs)")]
    ModuleNotFound { module: String, parent_file: String },
}

impl Error {
    pub const fn error_code(&self) -> &'static str {
        match self {
            Self::DependencyPath { .. } => "E012",
            Self::MetadataError { .. } => "E013",
            Self::Utf8(_) => "E014",
            Self::TomlParseError { .. } => "E025",
            Self::IoError { .. } => "E026",
            Self::ModuleNotFound { .. } => "E027",
        }
    }
}

/// # Errors
///
/// Will return `Err` if it can't read files from the directory that
/// metadata points to.
pub fn gather_packages(
    metadata: &Metadata,
    packages: &mut Vec<PackageMetadata>,
) -> Result<(), Error> {
    let mut workspace_packages: Vec<PackageMetadata> = metadata
        .packages
        .clone()
        .into_iter()
        .filter(|package_meta| metadata.workspace.members.contains(&package_meta.id))
        .filter(|package_meta| !packages.contains(package_meta))
        .collect();

    let workspace_packages_names = workspace_packages
        .iter()
        .map(|package| package.name.clone())
        .collect_vec();

    // find all dependencies listed by path
    let mut dependencies: HashMap<String, PathBuf> = HashMap::new();
    for package in &workspace_packages {
        for dependency in &package.dependencies {
            let name = &dependency.name;
            let url = Url::parse(&dependency.source.repr).map_err(|_| Error::DependencyPath {
                name: name.clone(),
                path: dependency.source.repr.clone(),
            })?;

            if url.scheme().starts_with("path") {
                let path = url.to_file_path().map_err(|()| Error::DependencyPath {
                    name: name.clone(),
                    path: dependency.source.repr.clone(),
                })?;
                dependencies.insert(name.clone(), path);
            }
        }
    }

    packages.append(&mut workspace_packages);

    // filter out dependencies already covered by workspace
    let out_of_workspace_dependencies: HashMap<&String, &PathBuf> = dependencies
        .iter()
        .filter(|&(k, _)| !workspace_packages_names.contains(k))
        .collect();

    for (name, manifest) in out_of_workspace_dependencies {
        let new_meta = MetadataCommand::new()
            .json()
            .manifest_path(manifest)
            .exec()
            .map_err(|_| Error::MetadataError {
                name: name.clone(),
                path: manifest.clone(),
            })?;
        gather_packages(&new_meta, packages)?;
    }

    Ok(())
}

/// # Errors
///
/// Will return `Err` if it can't read files from the directory that
/// metadata points to.
pub fn package_sources(package_metadata: &PackageMetadata) -> Result<Vec<Utf8PathBuf>, Error> {
    package_sources_with_test_files(package_metadata, false)
}

/// # Errors
///
/// Will return `Err` if it can't read files from the directory that
/// metadata points to.
pub fn package_sources_with_test_files(
    package_metadata: &PackageMetadata,
    include_test_files: bool,
) -> Result<Vec<Utf8PathBuf>, Error> {
    debug!("Collecting sources for package: {}", package_metadata.name);
    debug!("Package root: {}", package_metadata.root);
    debug!("Package manifest: {}", package_metadata.manifest_path);

    // Check if this is a Cairo procedural macro package
    if is_cairo_procedural_macro_package(&package_metadata.manifest_path)? {
        debug!(
            "Package {} is a Cairo procedural macro package",
            package_metadata.name
        );

        // Validate Cargo.toml configuration
        let cargo_toml_path = package_metadata.root.join("Cargo.toml");
        if validate_cargo_toml_for_proc_macro(&cargo_toml_path)? {
            debug!("Cargo.toml validation passed for procedural macro package");
            return collect_procedural_macro_rust_files(package_metadata, include_test_files);
        } else {
            debug!("Cargo.toml validation failed - treating as regular Cairo package");
            // Fall through to regular Cairo file collection
        }
    }

    let mut sources: Vec<Utf8PathBuf> = WalkDir::new(package_metadata.root.clone())
        .into_iter()
        .filter_map(std::result::Result::ok)
        .filter(|f| f.file_type().is_file())
        .filter(|f| {
            // Check if this is a test file
            if let Some(path_str) = f.path().to_str() {
                // Check if the path contains test directories but only if it's in src/
                let is_in_src = path_str.contains("/src/");
                let has_test_in_path = path_str.contains("/test") || path_str.contains("/tests/");

                if is_in_src && has_test_in_path {
                    // This is a test file in src/
                    return include_test_files;
                }

                // Exclude test directories outside src/
                if path_str.contains("/tests/")
                    || path_str.contains("/test/")
                    || path_str.contains("/examples/")
                    || path_str.contains("/benchmarks/")
                {
                    return false;
                }
            }

            // Include Cairo files
            if let Some(ext) = f.path().extension() {
                if ext == OsStr::new(CAIRO_EXT) {
                    return true;
                }
                // Only include Rust files if the package has been validated as a procedural macro
                if ext == OsStr::new("rs") {
                    // This will be handled by the procedural macro collection logic above
                    // if this package is a valid procedural macro package
                    return false;
                }
            }

            // Include Scarb.toml files
            if f.file_name() == OsStr::new("Scarb.toml") {
                return true;
            }

            // Only include Cargo.toml if this package is a validated procedural macro
            // (handled by the specialized collection above) or if there's no Cargo.toml
            // indicating this is a pure Cairo package
            if f.file_name() == OsStr::new("Cargo.toml") {
                // Don't include Cargo.toml files from non-procedural macro packages
                // They will be included by the specialized collection if they are valid proc macros
                return false;
            }

            false
        })
        .map(walkdir::DirEntry::into_path)
        .map(Utf8PathBuf::try_from)
        .try_collect()?;

    // Ensure the package's own manifest is included
    if !sources.contains(&package_metadata.manifest_path) {
        sources.push(package_metadata.manifest_path.clone());
    }

    let package_root = &package_metadata.root;

    if let Some(lic) = package_metadata
        .manifest_metadata
        .license_file
        .as_ref()
        .map(Utf8Path::new)
        .map(Utf8Path::to_path_buf)
    {
        sources.push(package_root.join(lic));
    }

    if let Some(readme) = package_metadata
        .manifest_metadata
        .readme
        .as_deref()
        .map(Utf8Path::new)
        .map(Utf8Path::to_path_buf)
    {
        sources.push(package_root.join(readme));
    }

    Ok(sources)
}

pub fn biggest_common_prefix<P: AsRef<Utf8Path> + Clone>(
    paths: &[Utf8PathBuf],
    first_guess: P,
) -> Utf8PathBuf {
    let ancestors = Utf8Path::ancestors(first_guess.as_ref());
    let mut biggest_prefix: &Utf8Path = first_guess.as_ref();
    for prefix in ancestors {
        if paths.iter().all(|src| src.starts_with(prefix)) {
            biggest_prefix = prefix;
            break;
        }
    }
    biggest_prefix.to_path_buf()
}

const CAIRO_EXT: &str = "cairo";

// TOML structures for parsing Cairo procedural macro manifests
#[derive(Debug, Deserialize)]
struct ScarbToml {
    #[serde(rename = "cairo-plugin")]
    cairo_plugin: Option<CairoPlugin>,
}

#[derive(Debug, Deserialize)]
struct CairoPlugin {
    // Empty struct - just need to detect presence of [cairo-plugin] section
}

#[derive(Debug, Deserialize)]
struct CargoToml {
    lib: Option<CargoLib>,
    dependencies: Option<std::collections::HashMap<String, toml::Value>>,
}

#[derive(Debug, Deserialize)]
struct CargoLib {
    #[serde(rename = "crate-type")]
    crate_type: Option<Vec<String>>,
}

/// Check if a package is a Cairo procedural macro package by examining its Scarb.toml
///
/// # Errors
/// Returns an error if the TOML file cannot be read or parsed
fn is_cairo_procedural_macro_package(scarb_toml_path: &Utf8Path) -> Result<bool, Error> {
    debug!("Checking if package is Cairo procedural macro: {scarb_toml_path}");

    let content = fs::read_to_string(scarb_toml_path).map_err(|e| Error::IoError {
        path: scarb_toml_path.to_string(),
        error: e.to_string(),
    })?;

    let scarb_toml: ScarbToml = toml::from_str(&content).map_err(|e| Error::TomlParseError {
        path: scarb_toml_path.to_string(),
        error: e.to_string(),
    })?;

    let is_proc_macro = scarb_toml.cairo_plugin.is_some();
    debug!("Package {scarb_toml_path} is procedural macro: {is_proc_macro}");

    Ok(is_proc_macro)
}

/// Validate that a Cargo.toml is configured correctly for a procedural macro
///
/// # Errors
/// Returns an error if the TOML file cannot be read or parsed
fn validate_cargo_toml_for_proc_macro(cargo_toml_path: &Utf8Path) -> Result<bool, Error> {
    debug!("Validating Cargo.toml for procedural macro: {cargo_toml_path}");

    if !cargo_toml_path.exists() {
        debug!("Cargo.toml not found: {cargo_toml_path}");
        return Ok(false);
    }

    let content = fs::read_to_string(cargo_toml_path).map_err(|e| Error::IoError {
        path: cargo_toml_path.to_string(),
        error: e.to_string(),
    })?;

    let cargo_toml: CargoToml = toml::from_str(&content).map_err(|e| Error::TomlParseError {
        path: cargo_toml_path.to_string(),
        error: e.to_string(),
    })?;

    // Check for crate-type = ["cdylib"]
    let has_cdylib = cargo_toml
        .lib
        .as_ref()
        .and_then(|lib| lib.crate_type.as_ref())
        .map(|types| types.contains(&"cdylib".to_string()))
        .unwrap_or(false);

    // Check for cairo-lang-macro dependency
    let has_cairo_macro_dep = cargo_toml
        .dependencies
        .as_ref()
        .map(|deps| deps.contains_key("cairo-lang-macro"))
        .unwrap_or(false);

    let is_valid = has_cdylib && has_cairo_macro_dep;
    debug!(
        "Cargo.toml validation - cdylib: {has_cdylib}, cairo-lang-macro: {has_cairo_macro_dep}, valid: {is_valid}"
    );

    Ok(is_valid)
}

/// Collect all necessary files for a Cairo procedural macro package
///
/// # Errors
/// Returns an error if files cannot be read or processed
fn collect_procedural_macro_rust_files(
    package_metadata: &PackageMetadata,
    include_test_files: bool,
) -> Result<Vec<Utf8PathBuf>, Error> {
    debug!(
        "Collecting procedural macro files for package: {}",
        package_metadata.name
    );

    let mut required_files = HashSet::new();
    let package_root = &package_metadata.root;

    // Always include Cargo.toml for procedural macros
    let cargo_toml_path = package_root.join("Cargo.toml");
    if cargo_toml_path.exists() {
        debug!("Adding Cargo.toml: {cargo_toml_path}");
        required_files.insert(cargo_toml_path);
    }

    // Always include Scarb.toml (already handled by package_sources_with_test_files)
    // but we need to ensure it's in our list
    if !required_files.contains(&package_metadata.manifest_path) {
        required_files.insert(package_metadata.manifest_path.clone());
    }

    // Include README and license files referenced in manifest
    if let Some(readme) = package_metadata
        .manifest_metadata
        .readme
        .as_deref()
        .map(|r| package_root.join(r))
    {
        if readme.exists() {
            debug!("Adding README: {readme}");
            required_files.insert(readme);
        }
    }

    if let Some(license) = package_metadata
        .manifest_metadata
        .license_file
        .as_ref()
        .map(|l| package_root.join(l))
    {
        if license.exists() {
            debug!("Adding license file: {license}");
            required_files.insert(license);
        }
    }

    // Start with the main library file and recursively collect dependencies
    let lib_rs_path = package_root.join("src/lib.rs");
    if lib_rs_path.exists() {
        debug!("Starting dependency analysis from lib.rs: {lib_rs_path}");
        required_files.insert(lib_rs_path.clone());

        // Recursively collect module dependencies
        collect_rust_module_dependencies(&lib_rs_path, package_root, &mut required_files)?;
    }

    // Find files with procedural macro attributes
    collect_macro_implementation_files(package_root, &mut required_files)?;

    // Include Cargo.lock if it exists (important for reproducible builds)
    let cargo_lock_path = package_root.join("Cargo.lock");
    if cargo_lock_path.exists() {
        debug!("Adding Cargo.lock: {cargo_lock_path}");
        required_files.insert(cargo_lock_path);
    }

    // Convert to Vec and filter based on include_test_files
    let mut sources: Vec<Utf8PathBuf> = required_files.into_iter().collect();

    // Filter test files if not requested
    if !include_test_files {
        sources.retain(|path| !should_exclude_rust_file(path));
    }

    // Sort for consistent output
    sources.sort();

    debug!(
        "Collected {} files for procedural macro package",
        sources.len()
    );
    for source in &sources {
        debug!("  - {source}");
    }

    Ok(sources)
}

/// Recursively collect Rust module dependencies from a source file
///
/// # Errors
/// Returns an error if files cannot be read or parsed
fn collect_rust_module_dependencies(
    file_path: &Utf8Path,
    package_root: &Utf8Path,
    required_files: &mut HashSet<Utf8PathBuf>,
) -> Result<(), Error> {
    debug!("Analyzing module dependencies in: {file_path}");

    let content = fs::read_to_string(file_path).map_err(|e| Error::IoError {
        path: file_path.to_string(),
        error: e.to_string(),
    })?;

    // Parse for module declarations
    let module_declarations = parse_module_declarations(&content);
    debug!(
        "Found {} module declarations in {}",
        module_declarations.len(),
        file_path
    );

    for module_name in module_declarations {
        match resolve_module_file_path(file_path, &module_name, package_root) {
            Ok(module_file) => {
                if module_file.exists() && !required_files.contains(&module_file) {
                    debug!("Adding module dependency: {module_name} -> {module_file}");
                    required_files.insert(module_file.clone());

                    // Recursively collect dependencies of this module
                    collect_rust_module_dependencies(&module_file, package_root, required_files)?;
                }
            }
            Err(Error::ModuleNotFound { .. }) => {
                // Module not found - this might be a conditional compilation module
                // or an external crate. We'll log it but not fail the build.
                debug!("Module not found (might be conditional): {module_name} from {file_path}");
            }
            Err(e) => return Err(e),
        }
    }

    Ok(())
}

/// Parse Rust source code to find module declarations
fn parse_module_declarations(content: &str) -> Vec<String> {
    let mut modules = Vec::new();

    for line in content.lines() {
        let trimmed = line.trim();

        // Skip comments
        if trimmed.starts_with("//") || trimmed.starts_with("/*") {
            continue;
        }

        // Look for module declarations:
        // - mod module_name;
        // - pub mod module_name;
        // - pub(crate) mod module_name;
        if let Some(module_name) = extract_module_name_from_line(trimmed) {
            debug!("Found module declaration: {module_name}");
            modules.push(module_name);
        }
    }

    modules
}

/// Extract module name from a line containing a module declaration
fn extract_module_name_from_line(line: &str) -> Option<String> {
    // Handle various visibility modifiers
    let line = line.trim();

    // Remove pub qualifiers
    let line = if line.starts_with("pub(") {
        // Handle pub(crate), pub(super), etc.
        if let Some(pos) = line.find(')') {
            line[pos + 1..].trim()
        } else {
            return None;
        }
    } else if let Some(stripped) = line.strip_prefix("pub ") {
        stripped.trim()
    } else {
        line
    };

    // Check for "mod" keyword
    if let Some(stripped) = line.strip_prefix("mod ") {
        let rest = stripped.trim();

        // Handle "mod name;" pattern
        if let Some(semicolon_pos) = rest.find(';') {
            let module_name = rest[..semicolon_pos].trim();
            if is_valid_module_name(module_name) {
                return Some(module_name.to_string());
            }
        }

        // Handle "mod name {" pattern (inline module - we still want to track it)
        if let Some(brace_pos) = rest.find('{') {
            let module_name = rest[..brace_pos].trim();
            if is_valid_module_name(module_name) {
                return Some(module_name.to_string());
            }
        }
    }

    None
}

/// Check if a string is a valid Rust module name
fn is_valid_module_name(name: &str) -> bool {
    !name.is_empty()
        && name.chars().all(|c| c.is_alphanumeric() || c == '_')
        && name
            .chars()
            .next()
            .is_some_and(|c| c.is_alphabetic() || c == '_')
}

/// Resolve the file path for a given module name
///
/// # Errors
/// Returns an error if the module file cannot be found
fn resolve_module_file_path(
    parent_file: &Utf8Path,
    module_name: &str,
    package_root: &Utf8Path,
) -> Result<Utf8PathBuf, Error> {
    let parent_dir = parent_file.parent().unwrap_or(package_root);

    // Try module_name.rs first
    let module_rs = parent_dir.join(format!("{module_name}.rs"));
    if module_rs.exists() {
        debug!("Found module file: {module_rs}");
        return Ok(module_rs);
    }

    // Try module_name/mod.rs
    let module_mod_rs = parent_dir.join(module_name).join("mod.rs");
    if module_mod_rs.exists() {
        debug!("Found module mod.rs: {module_mod_rs}");
        return Ok(module_mod_rs);
    }

    Err(Error::ModuleNotFound {
        module: module_name.to_string(),
        parent_file: parent_file.to_string(),
    })
}

/// Find and collect files containing procedural macro implementations
///
/// # Errors
/// Returns an error if files cannot be read or processed
fn collect_macro_implementation_files(
    package_root: &Utf8Path,
    required_files: &mut HashSet<Utf8PathBuf>,
) -> Result<(), Error> {
    debug!("Searching for procedural macro implementation files in: {package_root}");

    let src_dir = package_root.join("src");
    if !src_dir.exists() {
        return Ok(());
    }

    for entry in WalkDir::new(&src_dir) {
        let entry = entry.map_err(|e| Error::IoError {
            path: src_dir.to_string(),
            error: e.to_string(),
        })?;

        if let Some(path_str) = entry.path().to_str() {
            if path_str.ends_with(".rs") {
                let rust_file_path = Utf8PathBuf::try_from(entry.path().to_path_buf())?;

                // Skip files we've already processed
                if required_files.contains(&rust_file_path) {
                    continue;
                }

                // Skip test files and examples
                if should_exclude_rust_file(&rust_file_path) {
                    continue;
                }

                if contains_procedural_macro_attributes(&rust_file_path)? {
                    debug!("Found procedural macro implementation in: {rust_file_path}");
                    required_files.insert(rust_file_path);
                }
            }
        }
    }

    Ok(())
}

/// Check if a Rust file contains procedural macro attributes
///
/// # Errors
/// Returns an error if the file cannot be read
fn contains_procedural_macro_attributes(file_path: &Utf8Path) -> Result<bool, Error> {
    let content = fs::read_to_string(file_path).map_err(|e| Error::IoError {
        path: file_path.to_string(),
        error: e.to_string(),
    })?;

    // Look for procedural macro attributes
    let macro_attributes = [
        "#[inline_macro]",
        "#[attribute_macro]",
        "#[derive_macro]",
        "#[post_process]",
    ];

    for attr in &macro_attributes {
        if content.contains(attr) {
            debug!("Found procedural macro attribute {attr} in {file_path}");
            return Ok(true);
        }
    }

    Ok(false)
}

/// Check if a Rust file should be excluded (tests, examples, etc.)
fn should_exclude_rust_file(file_path: &Utf8Path) -> bool {
    let path_str = file_path.as_str();

    // Exclude test files
    if path_str.contains("/tests/")
        || path_str.contains("/test/")
        || path_str.contains("test_")
        || path_str.ends_with("_test.rs")
    {
        return true;
    }

    // Exclude examples and benchmarks
    if path_str.contains("/examples/")
        || path_str.contains("/benches/")
        || path_str.contains("/doc/")
    {
        return true;
    }

    // Exclude main.rs (not used in cdylib crates)
    if path_str.ends_with("/main.rs") {
        return true;
    }

    false
}

#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
    use super::*;
    use camino::Utf8PathBuf;
    use std::path::PathBuf;
    use tempfile::TempDir;

    #[test]
    fn test_biggest_common_prefix_simple() {
        let paths = vec![
            Utf8PathBuf::from("/root/project/src/lib.cairo"),
            Utf8PathBuf::from("/root/project/src/main.cairo"),
            Utf8PathBuf::from("/root/project/tests/test.cairo"),
        ];
        let first_guess = Utf8PathBuf::from("/root/project/src/lib.cairo");
        let result = biggest_common_prefix(&paths, first_guess);
        assert_eq!(result, Utf8PathBuf::from("/root/project"));
    }

    #[test]
    fn test_biggest_common_prefix_no_common() {
        let paths = vec![
            Utf8PathBuf::from("/root/project1/src/lib.cairo"),
            Utf8PathBuf::from("/root/project2/src/main.cairo"),
        ];
        let first_guess = Utf8PathBuf::from("/root/project1/src/lib.cairo");
        let result = biggest_common_prefix(&paths, first_guess);
        assert_eq!(result, Utf8PathBuf::from("/root"));
    }

    #[test]
    fn test_biggest_common_prefix_exact_match() {
        let paths = vec![Utf8PathBuf::from("/root/project/src/lib.cairo")];
        let first_guess = Utf8PathBuf::from("/root/project/src/lib.cairo");
        let result = biggest_common_prefix(&paths, first_guess);
        assert_eq!(result, Utf8PathBuf::from("/root/project/src/lib.cairo"));
    }

    #[test]
    fn test_error_display() {
        let error = Error::DependencyPath {
            name: "test_package".to_string(),
            path: "/invalid/path".to_string(),
        };
        let error_message = format!("{error}");
        assert!(error_message.contains("[E012]"));
        assert!(error_message.contains("Invalid dependency path"));
        assert!(error_message.contains("test_package"));
        assert!(error_message.contains("/invalid/path"));
        assert!(error_message.contains("Check that the path exists"));
    }

    #[test]
    fn test_cairo_extension_constant() {
        assert_eq!(CAIRO_EXT, "cairo");
    }

    #[test]
    fn test_file_filtering_logic() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = PathBuf::from(temp_dir.path());

        // Create test directory structure
        std::fs::create_dir_all(temp_path.join("src")).unwrap();
        std::fs::create_dir_all(temp_path.join("tests")).unwrap();
        std::fs::create_dir_all(temp_path.join("examples")).unwrap();

        // Create test files
        std::fs::write(temp_path.join("src").join("lib.cairo"), "").unwrap();
        std::fs::write(temp_path.join("src").join("main.cairo"), "").unwrap();
        std::fs::write(temp_path.join("tests").join("test.cairo"), "").unwrap();
        std::fs::write(temp_path.join("examples").join("example.cairo"), "").unwrap();
        std::fs::write(temp_path.join("Scarb.toml"), "").unwrap();
        std::fs::write(temp_path.join("other.txt"), "").unwrap();

        // Test the filtering logic used in package_sources
        let cairo_files: Vec<_> = walkdir::WalkDir::new(&temp_path)
            .into_iter()
            .filter_map(std::result::Result::ok)
            .filter(|f| f.file_type().is_file())
            .filter(|f| {
                // Test the exclusion logic
                if let Some(path_str) = f.path().to_str() {
                    if path_str.contains("/tests/")
                        || path_str.contains("/test/")
                        || path_str.contains("/examples/")
                        || path_str.contains("/benchmarks/")
                    {
                        return false;
                    }
                }

                // Test the inclusion logic
                if let Some(ext) = f.path().extension() {
                    if ext == std::ffi::OsStr::new(CAIRO_EXT) {
                        return true;
                    }
                }

                // Test Scarb.toml inclusion
                if f.file_name() == std::ffi::OsStr::new("Scarb.toml") {
                    return true;
                }

                false
            })
            .collect();

        // Should include cairo files from src, Scarb.toml, but not from tests or examples
        assert!(cairo_files
            .iter()
            .any(|f| f.file_name() == std::ffi::OsStr::new("lib.cairo")));
        assert!(cairo_files
            .iter()
            .any(|f| f.file_name() == std::ffi::OsStr::new("main.cairo")));
        assert!(cairo_files
            .iter()
            .any(|f| f.file_name() == std::ffi::OsStr::new("Scarb.toml")));
        assert!(!cairo_files
            .iter()
            .any(|f| f.path().to_str().unwrap().contains("/tests/")));
        assert!(!cairo_files
            .iter()
            .any(|f| f.path().to_str().unwrap().contains("/examples/")));
        assert!(!cairo_files
            .iter()
            .any(|f| f.file_name() == std::ffi::OsStr::new("other.txt")));
    }

    #[test]
    fn test_procedural_macro_detection() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf()).unwrap();

        // Create Scarb.toml with cairo-plugin section
        let scarb_toml_content = r#"
[package]
name = "test-macro"
version = "0.1.0"

[cairo-plugin]
"#;
        let scarb_toml_path = temp_path.join("Scarb.toml");
        std::fs::write(&scarb_toml_path, scarb_toml_content).unwrap();

        // Test detection
        let is_proc_macro = is_cairo_procedural_macro_package(&scarb_toml_path).unwrap();
        assert!(is_proc_macro);

        // Create Scarb.toml without cairo-plugin section
        let normal_scarb_toml = r#"
[package]
name = "test-normal"
version = "0.1.0"
"#;
        let normal_scarb_path = temp_path.join("normal_Scarb.toml");
        std::fs::write(&normal_scarb_path, normal_scarb_toml).unwrap();

        let is_not_proc_macro = is_cairo_procedural_macro_package(&normal_scarb_path).unwrap();
        assert!(!is_not_proc_macro);
    }

    #[test]
    fn test_cargo_toml_validation() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf()).unwrap();

        // Create valid Cargo.toml for procedural macro
        let valid_cargo_toml = r#"
[package]
name = "test-macro"
version = "0.1.0"

[lib]
crate-type = ["cdylib"]

[dependencies]
cairo-lang-macro = "0.1.0"
"#;
        let cargo_toml_path = temp_path.join("Cargo.toml");
        std::fs::write(&cargo_toml_path, valid_cargo_toml).unwrap();

        let is_valid = validate_cargo_toml_for_proc_macro(&cargo_toml_path).unwrap();
        assert!(is_valid);

        // Create invalid Cargo.toml (missing cdylib)
        let invalid_cargo_toml = r#"
[package]
name = "test-normal"
version = "0.1.0"

[dependencies]
cairo-lang-macro = "0.1.0"
"#;
        let invalid_cargo_path = temp_path.join("invalid_Cargo.toml");
        std::fs::write(&invalid_cargo_path, invalid_cargo_toml).unwrap();

        let is_invalid = validate_cargo_toml_for_proc_macro(&invalid_cargo_path).unwrap();
        assert!(!is_invalid);
    }

    #[test]
    fn test_module_declaration_parsing() {
        let rust_code = r#"
// This is a comment
pub mod utils;
mod macros;
pub(crate) mod helper;
mod inline_module {
    // inline content
}

// More comments
use std::collections::HashMap;
"#;

        let modules = parse_module_declarations(rust_code);
        assert_eq!(modules.len(), 4);
        assert!(modules.contains(&"utils".to_string()));
        assert!(modules.contains(&"macros".to_string()));
        assert!(modules.contains(&"helper".to_string()));
        assert!(modules.contains(&"inline_module".to_string()));
    }

    #[test]
    fn test_module_name_extraction() {
        assert_eq!(
            extract_module_name_from_line("mod test;"),
            Some("test".to_string())
        );
        assert_eq!(
            extract_module_name_from_line("pub mod test;"),
            Some("test".to_string())
        );
        assert_eq!(
            extract_module_name_from_line("pub(crate) mod test;"),
            Some("test".to_string())
        );
        assert_eq!(
            extract_module_name_from_line("mod test {"),
            Some("test".to_string())
        );
        assert_eq!(extract_module_name_from_line("// mod test;"), None);
        assert_eq!(extract_module_name_from_line("use mod_something;"), None);
    }

    #[test]
    fn test_valid_module_name() {
        assert!(is_valid_module_name("valid_name"));
        assert!(is_valid_module_name("test123"));
        assert!(is_valid_module_name("_underscore"));
        assert!(!is_valid_module_name("123invalid"));
        assert!(!is_valid_module_name(""));
        assert!(!is_valid_module_name("invalid-name"));
    }

    #[test]
    fn test_procedural_macro_attribute_detection() {
        let temp_dir = TempDir::new().unwrap();
        let temp_path = Utf8PathBuf::try_from(temp_dir.path().to_path_buf()).unwrap();

        // Create Rust file with procedural macro attributes
        let rust_with_macro = r#"
use cairo_lang_macro::TokenStream;

#[inline_macro]
pub fn my_inline_macro(_input: TokenStream) -> TokenStream {
    // implementation
}

#[attribute_macro]
pub fn my_attribute(_attr: TokenStream, item: TokenStream) -> TokenStream {
    // implementation
}
"#;
        let macro_file_path = temp_path.join("macro_impl.rs");
        std::fs::write(&macro_file_path, rust_with_macro).unwrap();

        let has_macros = contains_procedural_macro_attributes(&macro_file_path).unwrap();
        assert!(has_macros);

        // Create Rust file without procedural macro attributes
        let rust_without_macro = r#"
pub fn regular_function() -> i32 {
    42
}

#[derive(Debug)]
struct MyStruct {
    field: String,
}
"#;
        let normal_file_path = temp_path.join("normal.rs");
        std::fs::write(&normal_file_path, rust_without_macro).unwrap();

        let has_no_macros = contains_procedural_macro_attributes(&normal_file_path).unwrap();
        assert!(!has_no_macros);
    }

    #[test]
    fn test_rust_file_exclusion() {
        assert!(should_exclude_rust_file(&Utf8PathBuf::from(
            "/src/tests/test.rs"
        )));
        assert!(should_exclude_rust_file(&Utf8PathBuf::from(
            "/src/test_utils.rs"
        )));
        assert!(should_exclude_rust_file(&Utf8PathBuf::from(
            "/src/examples/example.rs"
        )));
        assert!(should_exclude_rust_file(&Utf8PathBuf::from("/src/main.rs")));
        assert!(should_exclude_rust_file(&Utf8PathBuf::from(
            "/benches/bench.rs"
        )));

        assert!(!should_exclude_rust_file(&Utf8PathBuf::from("/src/lib.rs")));
        assert!(!should_exclude_rust_file(&Utf8PathBuf::from(
            "/src/macros.rs"
        )));
        assert!(!should_exclude_rust_file(&Utf8PathBuf::from(
            "/src/utils.rs"
        )));
    }
}