In my project, I have two binary crates. One provides the back-end using actix_web, the other one is meant (it's not even nearly finished) to provide the front-end using Dioxus. They communicate over a HTTP API with JSON.
Effectively, I serialize the back-end data into JSON using serde_json and a struct into JSON and the deserialize the JSON into exactly the same struct as the backend used originally. Now, I think it would just be a waste of lines of code to copy the structs from the back-end code to the front-end code. I also imagine sharing the exact same structs could bring more benefits like not accidentally mixing up names of structs, better auto-completion and better type safety.
To achieve this, I considered making a library crate that the two binary crates can both access that will contain the structs.
Now, I do not know how I could share a library crate between two binary crates without having to upload it to crates.io or copy it into both other crates. The binary crates are both in separate directories in one common directory and I considered creating the library crate in the same common directory.
It would be great if somebody could help me out with this ![]()
There are multiple ways you can deal with this. The choice is yours.
Use cargo workspaces, set path in dependencies (i.e. to your git repo), or have your own package repository.
I think in your case workspaces is the way to go, but pick one.
1 Like
You can share a library with your binary crates in different ways. The simplest one, if both binaries are inside src/bin then just place your lib in src/lib.rs and references it. Something like:
āāā Cargo.toml
āāā src/
ā āāā lib.rs
ā āāā bin/
ā āāā first-binary.rs
ā āāā second-binary.rs
You can also use workspaces: in the binary crate Cargo.toml just use path to refer to the library in the same workspace:
[dependencies]
your_library = { version = "x.y.z", path = "../your_library_dir" }
Hey, than you very much! I will probably use the second solution!
Yup, I will use the path-based solution. Thank you!