Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Move from pub to pub(crate) #2778

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

Hofer-Julian
Copy link
Contributor

@Hofer-Julian Hofer-Julian commented Dec 30, 2024

This should be merged after #2778

I've noticed that we use pub for functions in many places, even though pub(crate) would suffice.

I wrote a small Python script that tries to change pub to pub(crate) and see if it cargo check is still happy. So kind of like cargo machete but for functions instead of dependencies. Is it efficient? No. Does it do the job? Yes!

I excluded crates that are dependencies of pixi-build-backends.

In case we want to run this script again in the future:

import subprocess
from pathlib import Path


def run_cargo_check():
    result = subprocess.run(["cargo", "check", "--workspace", "--all-targets"], capture_output=True)
    return result.returncode == 0


def process_file(file_path):
    lines = file_path.read_text().splitlines()

    for i, line in enumerate(lines):
        if "pub fn" in line:
            original_line = line
            # Replace "pub fn" with "pub(crate) fn"
            lines[i] = line.replace("pub fn", "pub(crate) fn")

            file_path.write_text("\n".join(lines) + "\n")

            if run_cargo_check():
                print(f"Change kept in {file_path} at line {i+1}")
            else:
                # Revert the change
                lines[i] = original_line
                file_path.write_text("\n".join(lines) + "\n")


def main():
    directories = ["crates", "src"]

    for directory in directories:
        src_dir = Path(directory)
        for file_path in src_dir.rglob("*.rs"):
            process_file(file_path)


if __name__ == "__main__":
    main()

@ruben-arts
Copy link
Contributor

IIRC this needs to be updated and you and @baszalmstra discussed specifics, so I'll move this to a draft PR for now.

@ruben-arts ruben-arts marked this pull request as draft January 6, 2025 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants