topheman / snake-pipe-rust

🦀 A snake game based on stdin/stdout (+tcp and unix sockets) in rust
MIT License
12 stars 1 forks source link

Remove workspace to allow publishing cli + lib (but not all the libs) #6

Closed topheman closed 8 months ago

topheman commented 8 months ago

This issue blocks #4

Workspace

At the time of writing this issue, the project is organized in workspaces:

crates
├── cli
│   ├── Cargo.toml
│   └── src
│       └── main.rs
└── common
    ├── Cargo.toml
    └── src
        ├── gamestate
        │   ├── game.rs
        │   ├── mod.rs
        │   ├── physics.rs
        │   └── snake.rs
        ├── lib.rs
        ├── render.rs
        ├── stream.rs
        └── throttle.rs

As constraints for publishing, we have:

In a workspace, the submodules of the common crate need to be pub in order to be consumed by the cli crate.

Visibility and Privacy

I found out about Visibility and Privacy:

You can adapt the scope of the pub keyword: pub(in path), pub(crate), pub(super), and pub(self):

In addition to public and private, Rust allows users to declare an item as visible only within a given scope. The rules for pub restrictions are as follows:

  • pub(in path) makes an item visible within the provided path. path must be an ancestor module of the item whose visibility is > - being declared.
  • pub(crate) makes an item visible within the current crate.
  • pub(super) makes an item visible to the parent module. This is equivalent to pub(in super).
  • pub(self) makes an item visible to the current module. This is equivalent to pub(in self) or not using pub at all.

Incompatibility with workspace

I have the same problem as explained on this thread:

Proposed solution

Get rid of workspace ...

This is not a big project, so the compile time won't be a problem and I prefer not to expose private apis.

Adopted solution

The pub(crate) doesn't work, so like most crates, I added a #[doc(hidden)] annotation on private modules.

See implementation in #7

If you have a solution to make it work with workspace, please leave a comment.

Sources: